ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
If I understand correctly then this should do what you want:
Code:
#!/bin/bash
# sorted list with real file names
REALNAME="/path/to/sorted.real.names"
# list with wanted name
WANTEDNAME="/path/to/wanted.names"
# directory that holds pictures/files
PICDIR="/path/to/picture/directory"
sdiff -t ${REALNAME} ${WANTEDNAME} | \
sed 's/ [ ]*| /|/' | \
awk -v PICDIR=${PICDIR} -F"[.|]" '{ print "mv "PICDIR"/"$1"."$2" \""PICDIR"/"$3"."$2"\"" }' > /tmp/rename-this
. /tmp/rename-this
#sdiff -t ${REALNAME} ${WANTEDNAME} | sed 's/ [ ]*| /|/' | \
# awk -v PICDIR=${PICDIR} -F"[.|]" '{ print "mv "PICDIR"/"$1"."$2" \""PICDIR"/"$3"."$2"\"" }' | bash
A little breakdown of the above:
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
pic0002.esp | Red Cross
sed 's/ [ ]*| /|/' This replaces the 'middle part' (spaces | spaces) with just a |
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).
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
Last edited by williamharwell; 02-15-2007 at 09:55 PM.
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?
What do the text files contain?
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
exec 4<textfile2
cd /path/to/directory/with/images
while read file <&3; read name <&4
do
mv "$file" "$name"
done
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:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.