Shellscript to rearrange filenames in directories


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

The reason is very simple why you had all these chaos !!

The regex you used is greedy !! So make it non greedy and check.

Code:
for file in $(find /home/sathiya/t1/ -type f -name "*.pdf")
do
  echo $file 
  newfile="$(echo $file | sed 's/\(.*\)\(.*?\)_\(.*?\)_\(.*?\).pdf$/\4\3_\2_\1.pdf/')"
  echo $newfile
done

Tested, works as expected !

Let us know whether it helps. Also it is the solution for the previous problem you are facing, it will not move all the files to the script where the find is placed, it will rename all the files in the appropriate directories itself as with your mv command.
# 9  
Old 08-19-2009
Code:
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-zinc_AMDL_2009.pdf
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/Benzoic-zinc_AMDL_2009.pdf
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/folder2/folder3/Bolsch-fing_TMDLL_2007.pdf
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/folder2/folder3/Bolsch-fing_TMDLL_2007.pdf
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/folder2/Tandge-Gh-lol_AMDLIKO_2008.pdf
/home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/folder2/Tandge-Gh-lol_AMDLIKO_2008.pdf

The above is the output, which means the files are not being renamed, was facing same issue earlier, when I sorted this the mv copied files at one location, not in directories.
So help required, and thanks for the response!

Below is the script i ran, did not put the mv command since file and newfile are same files.
Code:
for file in $(find /home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/ -type f -name "*.pdf")
do
  echo $file 
  newfile="$(echo $file | sed 's/\(.*\)\(.*?\)_\(.*?\)_\(.*?\).pdf$/\4\3_\2_\1.pdf/')"
  echo $newfile
done

# 10  
Old 08-19-2009
Hopefully this worked ....

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

Does it works for you also, as you expected ?!?!
# 11  
Old 08-19-2009
its woking, will try on more files and let ypu know, thanks!!!!

---------- Post updated at 02:51 PM ---------- Previous update was at 12:59 PM ----------

The script is working, but I wanted to know since i want to rename the file from NAME_DAME_2008, is there any way I can restrict the script to change files only ending with a year.
Example.
If the name of a file is Benzene_AMBL_pod.pdf
The script should not work
but if name is Benzene_AMBL_2008.pdf
the output should be 2008_AMBL_Benzene.pdf

thanks
# 12  
Old 08-19-2009
I hope this is what u wanted....

Code:
for file in $(find /home/smriti/Aquino_2008/Important_script_final_dec2008/rearrange_pdf_names/ -type f -name "*.pdf")
do
  echo $file
    newfile="$(echo $file | sed 's/\(.*\)\/\(.*\)_\(.*\)_\([0-9]...\).pdf$/\1\/\4_\3_\2.pdf/')"
  echo $newfile
done

# 13  
Old 08-19-2009
yeah thanks a lot

Last edited by deaddevil; 08-20-2009 at 06:36 AM.. Reason: just
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.data>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 script.sh ... (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