Help with Bash Script - Rename Multiple Files
Ok,
I am working on a bash script and I am in need of some major help if anyone is willing to assist! We have a directory with about 17,000 image files in it. The image files are names in the format pic0001.eps, pic0002.eps, etc.. Each of these file names corresponds to a real name such as Red Cross, Sony, etc. What we have in addition to the files, is (1) in a large text file, a sorted alphabetical listing of all of the filenames, and (2) a seperate text file of the real names that the filenames are supposed to be that are listed in the same corresponding order as the text file with the filenames. I am struggling to find a way to get a bash file to enter the directory with the image files and text files and look at the text file with the filenames, select this file and rename the file according to the real name associated with it in the other textfile with the real names while keeping the same file extention. Anyone have ideas? |
Hi,
If I understand correctly then this should do what you want: Code:
#!/bin/bash The first three variables (REALNAME, WANTEDNAME and PICDIR) need to point to the appropriate files/dirs. You need to change this before you start testing/trying. sdiff -t ${REALNAME} ${WANTEDNAME} This merges the file holding the actual names with the file holding the wanted names. I do undestand that these files correspond, this needs to be so for the correct working of this 'script'. The -t option expands tabs to spaces, which will make life a bit easier for what comes next. Part of this output: Code:
pic0001.esp | Sony Part of this output: Code:
pic0001.esp|Sony LOL This might need a longer explenation all by it self, which I will not do..... What it does? It creates a legal move statement (mv x y). It also surrounds both source and destination with double quotes (mv "x" "y"), this to take care of special chars (spaces being the obvious one). Part of this output: Code:
mv "/path/to/picture/directory/pic0001.esp" "/path/to/picture/directory/Sony.esp" 1The one that is 'active' at the moment ) Put the output in a file (/tmp/rename-this) and parse the file (. /tmp/rename-this). 2) pipe the output of the awk command to bash ( awk ... | bash). I like the second one better. Hope this gets you going again. |
I have a perl solution :
PHP Code:
|
@keefaz : Nice!
@embsupafly : Use keefaz' solution. Much more elegant and resource friendly. |
Multiple dots in a file name
The first example works great on files with only one dot. Ex: picture1.jpg. But what if I need to do this on files with multiple dots such as picture.1.jpg
I would like to change the filename except for the final dot and extension. Any ideas? The second example written in Perl does not leave the extension intact. Thanks |
Quote:
Is there one file with the current file name and another with the new file name? Is there a line-to-line correspondence between the files? Do the files contain the full names of the files, or just the part before the suffix ("extension" in Windows-speak)? I would use something like this, but without more details on the format of the files, I cannot tell whether it will work: Code:
exec 3<textfile1 |
It works
The solution given by cfaj works!
Code: exec 3<textfile1 exec 4<textfile2 cd /path/to/directory/with/images while read file <&3; read name <&4 do mv "$file" "$name" done Thanks |
Assuming both files checked just place after name of script separated by space:
Code:
#!/usr/bin/awk -f |
Thanks to "grail". This awk script works too. However the NR in the script seems to be reduntant i.e. it works without the NR in the script code. Also when executing either as an executable script or just code typed in fro command line it goes through the renaming sequence twice (it is harmless though)
|
Yeah I am not sure what I was thinking there (looking back at it) :$
Although as I am still on the learning side of awk i have also discovered the variable assignment can be done on one line too: Code:
getline out1 < file1 |
Also ( I think) why it goes on repeating the "action" after renaming of files is completed, is because awk repeats the test for total nos lines of input. In this case two files are input. So it repeats for total nos lines for both files. The renaming is complete when it goes through the first file and balance actions are redundant. If you give it three parameters eg file1 file2 file2, it repeats action for total nos lines for all three files. This can be verified by adding
END{ print "NR is - " NR } at end of code. Thanks and Best Regards |
So like I said before ... always learning :)
Code:
#!/usr/bin/awk -f |
Thanks grail. You are a Master. Total problem solved.
Best Regards |
Can't keep a good man down (although I fear there are true gurus around who can do better):
Code:
#!/usr/bin/awk -f |
Thanks to grail. It took time to "digest" the compact code. To work you have to add "|bash" to the end of the command however.
Best Regards |
Sorry my bad there, I was just checking I had the correct output, but the easy option is:
Code:
print | "mv \"" $0 "\" \"" out2 "\"" |
Hi,
one more solution: Code:
$ ls -1 Oleg |
All times are GMT -5. The time now is 09:36 AM. |