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
# 1  
Old 10-22-2010
Need script to move files based on name

Hi folks,

I'm new here and appreciate greatly any help.

I have a bunch of files that need be moved and renamed. Fortunately, they are all in sequence...

Present filename and path: /.catalog1/t76038_842-01
Move to: /.catalog1/76038-01

So, we need to drop the "t" and "_842" and increment the "01"

The final file would be /.catalog1/76038_842-366 and will get moved to
/.catalog1/76038-366

Currently we do this manually and it is monotonous! I am using bash.

Thank you so much!

Brian
# 2  
Old 10-22-2010
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

Last edited by ctsgnb; 10-22-2010 at 07:52 PM..
# 3  
Old 10-22-2010
I appreciate the quick reply, but I'm a little confused...

I do not script at all so please excuse my ignorance. But where do we set the loop start and end point? ie. start at /.catalog1/t76038_842-05 and end at /.catalog1/t76038_842-366? Also, what is <inputfile? Not sure what we need there.

Thanks.
# 4  
Old 10-22-2010
go in your directory :

Code:
cd /.catalog1

Then list the file you want to rename and make sure that this list contains only the file you want to rename and that they fit the name t*_*-*

Code:
ls -1 t*_*-*

the -1 option is to display on 1 column
if the list is ok, you have 2 choices :

choice 1) you branch it directly to the script using a pipe | :
Code:
ls -1 t*_*-* | { while read t ; do a=${t##*t} ; echo mv "$t ${a%%_*}-${t##*-}" ; done }

choice 2) you redirect this list into a file :
Code:
ls -1 t*_*-* >inputfile

and then you give this file to feed the script
Code:
{ while read t ; do a=${t##*t} ; echo mv "$t ${a%%_*}-${t##*-}" ; done } <inputfile


Last edited by ctsgnb; 10-22-2010 at 08:15 PM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 10-23-2010
Thanks alot. I will give that a try Monday. Very much appreciated!

Brian
# 6  
Old 10-23-2010
Another way:
Code:
ls t*|sed -e 'p;s/^t//;s/...\-//'|xargs -n2 mv

This User Gave Thanks to Klashxx For This Post:
# 7  
Old 10-23-2010
Nice one Klashx !
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