Shellscript to rearrange filenames in directories

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shellscript to rearrange filenames in directories
# 1  
Old 08-17-2009
Shellscript to rearrange filenames in directories

below is the script to rename filenames ending with .pdf extension.
I want the script to enter directories and search for all pdf and then if it is in the format file_amb_2008.pdf , then change it to 2008_amb_file.pdf, and this script should work only for .pdf files.

help required to make the script run in all directories. Right now it works for only the directory is runs in.

The script is:
for file in $(ls -1)
newfile="$(echo $file | sed 's/\(.*\)_\(.*\)_\(.*\)..../\3_\2_\1.pdf/')"
mv $file $newfile

Also i want it to rename files only in the format file_amb_2008.pdf to 2008_amb_file.pdf and ignore the .txt or other files.
plz help.
# 2  
Old 08-17-2009
You can do this using the 'find' command. Just replace the 'ls -1' part with 'find . -type f -name "*.pdf"'.

If you feel more adventurous, take a look at find's -exec option to do everything in one command instead.
# 3  
Old 08-17-2009
thanks for the reply,
i modified the script as per your suggestion to:
for file in $(find /home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/ -type f -name "*.pdf")
  newfile="$(echo $file | sed 's/\(.*\)_\(.*\)_\(.*\)..../\3_\2_\1.pdf/')"
  mv $file $newfile

But am getting error:
$ sh
mv: cannot move `/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-ben_MMCL_2009.pdf' to `2009_MMCL_/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-ben.pdf': No such file or directory

what do you suggest?
# 4  
Old 08-17-2009
The pattern match in sed is a bit too agressive, try changing it to:
sed 's%(.*)/(.*)_(.*)_(.*)\.pdf$%\1/\4_\3_\2.pdf%'

(ie I've added another match to catch everything up to the last / in the filename)
Not tested but it should do the job.
# 5  
Old 08-18-2009
its giving the error below
sed: -e expression #1, char 44: invalid reference \4 on `s' command's RHS
mv: missing file argument
Try `mv --help' for more information.

Also when I try the script below:

for file in $(find /home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names -type f -name "*.pdf")
  newfile="$(echo $file | sed 's/\(.*\)_\(.*\)_\(.*\)..../\3_\2_\1.pdf/')"
  mv $file $newfile

I get following error:
mv: cannot move `/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-ben_MMCL_2009.pdf' to `2009_MMCL_/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-ben.pdf': No such file or directory

but what I expect is:
moved `/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-ben_MMCL_2009.pdf' to `/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/2009_MMCL_Benzoic-ben.pdf'

Check the final file names in both above cases.

---------- Post updated 08-18-09 at 11:53 AM ---------- Previous update was 08-17-09 at 01:47 PM ----------

bump for the issue

Last edited by deaddevil; 08-18-2009 at 04:22 AM.. Reason: added more info.
# 6  
Old 08-18-2009
just a little modification to ur code and it is working for me...

for file in $(find /home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/  -type f -name "*.pdf")
  newfile="$(echo ${file##*/} | sed 's/\(.*\)_\(.*\)_\(.*\)..../\3_\2_\1.pdf/')"
  mv $file $newfile
  echo "Moved"

# 7  
Old 08-19-2009
hey thanks, the script is working fine, but just noticed one undesirable result.

if you noticed , In the find command I specified the location of where the pdb's may be located, but I may have multiple folders with many pdb's in them, now the above script is moving them from their location renaming them and placing all of them in the directory specified in the find command.
I want all of them to be replaced in their original locations itself, not move them to another location.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A cleaner way to rearrange column

Hello, I have some tab delimited text data, index name chg_p chg_m 1 name,1 1 0 2 name,2 1 1 3 name,3 1 0 4 name,4 1 0 5 name,5 1 1 I need to duplicate the "index" column, call it "id" and insert it after the... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

Rearrange Lines with awk

I need to rearrange the lines in the input file in the example below: Input: LG1 R500 A-170 F1:81 F1:22 F2:32 F1:71 LG1 R700 A-203 F2:17 E2:18 LG1 R700 B-224 E1:9 LG2 R500 C-235 E2:9 F2:17 Output: LG1 R500 A-170 F1:81 LG1 R500 A-170 F1:22 LG1 R500 A-170 F2:32 LG1 R500 A-170... (2 Replies)
Discussion started by: aydj
2 Replies

3. Shell Programming and Scripting

Using awk to rearrange fields

Hi, I am required to arrange columns of a file i.e make the 15th column into the 1st column. I am doing awk 'begin {fs=ofs=","} {print $15,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14}'>ad.csv the problem is that column 15 gets to column 1 but it is not comma separated with the... (10 Replies)
Discussion started by: seddoubt
10 Replies

4. Shell Programming and Scripting

Rearrange the text file

Gents, I have a large file and each line of the file contains more than 200 bytes.Please let me a way to have the new line to start when the word "FIT" appears. I was trialling with 'tr' command but i am not sure how to get it based on bytes and so it wasn't working... Current... (3 Replies)
Discussion started by: appu2176
3 Replies

5. Shell Programming and Scripting

script to rearrange data.

Hello. I have data in the following format (the spaces at the beginning of lines are included): 1 2 2 0.39621 0.00000 1 2 2 0.00000+-0.0000 * 1 2 ... (5 Replies)
Discussion started by: andersgs
5 Replies

6. Shell Programming and Scripting

Remove Duplicate Filenames in 2 very large directories

Hello Gurus, O/S RHEL4 I have a requirement to compare two linux based directories for duplicate filenames and remove them. These directories are close to 2 TB each. I have tried running a: Prompt>diff -r data1/ data2/ I have tried this as well: jason@jason-desktop:~$ cat ... (7 Replies)
Discussion started by: jaysunn
7 Replies

7. Shell Programming and Scripting

Rearrange data from 2 files

Dear All, I have the below files A file contains 1473 1649 1670 1758 1767 1784 B file contains 1242 1246 1264 1268 1284 (3 Replies)
Discussion started by: yahyaaa
3 Replies

8. Shell Programming and Scripting

rearrange a file

hi! in awk, i have a file like this: Trace1: WRIT,Trace2: BLAN,Trace3: BLAN, -47.2120018005371,,,39815.4809027778 -46.3009986877441,,,39815.4809027778 -46.277000427246,,,39815.4809143519 -46.7389984130859,,,39815.4809259259 -46.3460006713867,,,39815.4809259259... (10 Replies)
Discussion started by: riderman
10 Replies

9. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

10. UNIX for Dummies Questions & Answers

Help rearrange the content of the file.

Hi all, I need to rearrange the content of the file. I need to move line 1 to 4, 2 to 3, 3 to 2, and 4 to 1. IPlease help. I was thinking about using head and tail command. Here is the original file. aa bb cc dd Here is what I need it to be. dd cc bb aa Thanks. (6 Replies)
Discussion started by: sirrtuan
6 Replies
Login or Register to Ask a Question