rename files after reading source file
Hi everyone,
I have a directory full of over a thousand files and the names are bit long (and no pattern found in the naming style). I would like to simplify the filenames. For eg: Bioimage_23335989_Data_22317866_22317867_20140723_1002.pdb Bioimage_22335990_Data_22318490_22318491_20140723_1009.pdb Bioimage_23835970_Data_22317866_22317867_20140723_1005.pdb .....and so on ! I have made a simple text file (filelist.txt) which reads like this: 1.pdb Bioimage_23335989_Data_22317866_22317867_20140723_1002.pdb 2.pdb Bioimage_22335990_Data_22318490_22318491_20140723_1009.pdb 3.pdb Bioimage_23835970_Data_22317866_22317867_20140723_1005.pdb .....and so on ! Can anybody provide me with a script (awk, sed , etc) which would read the source file and rename the files accordingly? I could get similar scripts else where but nothing seems to work for me. Any kind of help would be highly appreciated. |
You don't need the text file unless the numbers actually mean something. If you just need them to be sequential, do this:
Code:
ls -1 | cat -n | while read n f; do mv "$f" "$n.txt"; done Get rid of the space delimiter in your new file name listing: Code:
cat file.txt | tr ' ' ':' > new.txt Code:
for f in $(< new.txt) ; do mv $(echo $f | cut -f 2 -d":") $(echo $f | cut -f 1 -d":") ; done Yeah there's certainly a more elegant way to do it with awk, but that is what popped into my head and it works :-) |
If you simply want to rename the all files of a directory in numerical order, you do not need text file. A simple for loop can do this.
Suppose you have a directory which contains only files like Code:
Bioimage_23335989_Data_22317866_22317867_20140723_1002.pdb Code:
1.pdb Code:
j=0; for i in *.pdb; do j=`expr $j + 1`; mv $i $j.pdb; done If there are all types of files in a directory like jpg, txt, pdb and many more and you want to rename all in numerical order with their respective file extension, First enter into the directory using cd and run the command Code:
j=0; for i in *; do ext=$(echo $i |awk -F . '{if (NF>1) {print "."$NF}}'); j=`expr $j + 1`; mv $i $j$ext; done But if you want that every file should be renamed with the name mentioned in text file in same row. First enter into the directory using cd and run the command. Path of text file should be correct. Code:
IFS=$'\n'; lst=$(cat /path/of/filelist.txt); for i in $lst; do mv `echo $i | awk '{print $2}'` `echo $i | awk '{print $1}'`; done |
Hi,
assuming bash, zsh or similar, no need for `` and expr try Code:
i=1 ; for f in *.pdb ; do echo mv $f $((i++)).pdb ; done Code:
i=1 ; for f in *.pdb ; do echo mv $f $(printf "%05d.pdb" $i) ; ((i++)) ; done PS. The "echo" is in there for testing. |
OP told he has thousands of files. Using backtick in the solution will fork new process, all of these tips will fork several thousand processes. That looks very inefficient for me.
Code:
instead of: |
Thank you all for the kind answers. I did want my filenames to be simplified but also wanted to keep track of "who is who", ie, simply renaming them (randomly) 1.pdb, 2.pdb, 3.pdb... doesn't work for me ! Therefore, I would always need to use the source file to let the program know what new name has to be assigned. So, codes provided by notKlaatu and pan64 work very well for me. But I appreciate you all for your time and kindness !
|
Appending numbers with file extension to each line in a textfile
Hi everybody,
I have a directory full of over thousand files. For eg.- Hello.pdb, Image.pdb, Biology.pdb, ....etc. I would like to enlist all of them in a file in a manner like this: Hello.pdb Image.pdb Biology.pdb .....and so on ! and I would like to get a file which would read: 1.pdb:Hello.pdb 2.pdb:Image.pdb 3.pdb:Biology.pdb .....and so on ! It looks easy but I am not able to get a suitable code for this. Any help would be highly appreciated ! |
you need to modify only the solution you got from evo2:
Code:
was: |
Quote:
1.pdb *.pdb |
This variation works for me
Code:
i=1 ; for f in *.pdb ; do echo "${i}.pdb:${f}" >>pdb.lis; ((i++)) ; done |
Quote:
1.pdb:*.pdb |
Assuming we are talking bash here, looks like you may have pattern matching turned off.
Do you have a the env variable GLOBIGNORE set? Do you have a -f or -o noglob bash options set anywhere? (like in an alias?) |
I merged your two closely related threads and ask that you stick to one thread per topic.
Thanks. |
Hi,
Quote:
Evo2. |
Thank you everybody for answering and I apologise for replying so late. I have no idea what was going wrong but when I tried the same codes on another computer, it worked ! With little modifications I could run each of your codes, successfully. Could be some silly mistake from my side, but couldn't figure out so far. @Tinkster: Noted. Thanks !
|
All times are GMT -5. The time now is 01:36 AM. |