Need script to move files based on name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script to move files based on name
# 8  
Old 10-25-2010
Quote:
Originally Posted by ctsgnb
This code assume :
1) your shell support ${##|%%} expansion
2) all your initial file name have the same form tNNNNNN_NNNNN-NNNNN
the lower case letter t followed by any number of figures separated by a _ and a -

Code:
{ while read t ; do a=${t##*t} ; d=${t%/*} ; echo mv "$t $d/${a%%_*}-${t##*-}" ; done } <inputfile

run it and if ok remove the echo

Code:
[ctsgnb@shell ~]$ cat myt
/.catalog1/t76038_842-01
/.catalog1/t76038_842-366
/.catalog1/t76038_543-366
[ctsgnb@shell ~]$ { while read t ; do a=${t##*t} ; d=${t%/*} ; echo mv "$t $d/${a%%_*}-${t##*-}" ; done }<myt
mv /.catalog1/t76038_842-01 /.catalog1/76038-01
mv /.catalog1/t76038_842-366 /.catalog1/76038-366
mv /.catalog1/t76038_543-366 /.catalog1/76038-366
[ctsgnb@shell ~]$

Note that you might want to run it directly in /.catalog1 directory, then
Code:
cd /.catalog1
ls -1 t*_*-* | { while read t ; do a=${t##*t} ; echo mv "$t ${a%%_*}-${t##*-}" ; done }

This would give :

Code:
[ctsgnb@shell ~]$ ls -1 t*_*-* | { while read t ; do a=${t##*t} ; echo mv "$t ${a%%_*}-${t##*-}" ; done }
mv t76038_842-01 76038-01
mv t76038_842-366 76038-366
mv t76038_543-366 76038-366
[ctsgnb@shell ~]$

if your are happy with what is displayed just remove the echo and run it
I tried this. I created the text input file and each line contains:

Code:
/.catalog1/t####_842-###

But the output when running the command is:

Code:
/.catalog1/76038-68  8_842-68

Any ideas what's going wrong?

Thanks again...

Brian
# 9  
Old 10-26-2010
In my code, once i have read t i don't modify it, $t will always contains the initial scanned name.
So if your output show /.catalog1/76038-68 as being $t then it means you have modified it. I may be wrong, but i think you may have done some misstyping or typo error.

By the way, i did my test on FreeBSD. But i still think the my code should work on shells that can handle de ${##|%%} syntax.

Please show :
- your input file
- what you did exactly type
- in which directory were you when you did your test

You can also try the Klashx elegant solution that should work fine as well but you should run it from within the directory that hold the t*_*-* files
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move files with a certain suffix based on how many files are in another folder

Hello, First time poster. I am looking for a way to script or program the process of moving files from one folder to another, automatically, based on the count of files in the destination folder. I was thinking a shell script would work, but am open to the suggestions of the experts... (6 Replies)
Discussion started by: comtech
6 Replies

2. Shell Programming and Scripting

Move files based on status

Hi UNIX Experts, Kindly help me in this requirement where i want to move files based on its status. For Eg: I have 3 dir's --FileBox Dir this directory will have all the files which i need to process (in informatica ETL Tool) --Succeeded Dir this directory will have files which... (4 Replies)
Discussion started by: kumarinfa
4 Replies

3. UNIX for Dummies Questions & Answers

Need script to move files based on month

Hi , I need a script which moves files based on month. Example : Apr 29 03:16 log4.txt Apr 29 03:16 log5.txt May 4 09:17 log1.txt May 4 09:17 log2.txt Move Apr files into Apr2015(Folder) Move May files into May2015(Folder). This is urgent requirement , if you can help me... (5 Replies)
Discussion started by: rockingvj
5 Replies

4. Solaris

Move files into different folders based on its month

Hi All, I want to move the files in to different folders based on the files month in the file timestamp. For example All the september files in the directory should moves into the folder "sep_bkp_files" , August files in to aug_bkp_files folder... Please help me to achive the above... (10 Replies)
Discussion started by: velava
10 Replies

5. Shell Programming and Scripting

Help in Logic - How to move files based on a condition

Hi All, I have 2 LINUX scripts (Lets say ScriptA & ScriptB) which copies files (lets say 'date_abc.txt , it goes to a while loop & checks for the files between 2 date varialbles) to directory X. (as Shown Below) These 2 scripts are called from 2 different Autosys schedules & runs at the... (3 Replies)
Discussion started by: dsfreddie
3 Replies

6. Shell Programming and Scripting

Move files to another directory based on name

Hi Folks, I have different type of file in my current directory. From my current directory i need to move the file which is start with csp_rules and if the file is having the string payg , then I need to move all this files to another directory /output/record. Please help me how to do this? ... (3 Replies)
Discussion started by: suresh01_apk
3 Replies

7. Shell Programming and Scripting

Unix Script To Move Files Based On Grep

I am looking for advice on how to write a script that will rename and/or move files to a different directory based upon the results of a grep. Let's say I have ten files in a directory. Some of them - not all - contain the text 'HELLO'. I would like to be able to grep the files for that text,... (3 Replies)
Discussion started by: rjhjr64
3 Replies

8. Shell Programming and Scripting

Script to move files based on Pattern don't work

Hi people, i need you help on this if you can. I have a script that does the move command when it searches for a that doesn't match the pattern. This Pattern is on a list. When it run's and doesn´t found no files but it haves 2 more folders it moves the folders too. Ex:... (1 Reply)
Discussion started by: osramos
1 Replies

9. UNIX for Dummies Questions & Answers

Move files based on year

Hi All, I have a directory which has crores of files since from 2003. I want to move ony the 2003 files to another directory. Please help (9 Replies)
Discussion started by: IHK
9 Replies

10. UNIX for Dummies Questions & Answers

Move files based on year

I have a directory which has crores of files since from 2003. I want to move only the 2003 files to another directory. Please help in doing this. Thanks (1 Reply)
Discussion started by: IHK
1 Replies
Login or Register to Ask a Question