[SOLVED] Rename All Files from a TextList, How ????
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Did you want a1.jpg to be renamed (`mv`) to exactly, with spaces!:
'duke hyundai bajaj honda speed wallpaper engine.jpg'
? An article discussing this: http://www.linuxjournal.com/article/10954
#!/bin/bash
# Initializing variables
i=1
NEWNAME=""
# Loop over each file
for FILE in `ls -1 *.jpg`; do
# Retrieving ith line from text file
NEWNAME=`sed -n "${i}p" namelist.txt`
echo "move ${FILE} -> ${NEWNAME}.jpg ..."
# Renaming file; double quotes because of spaces in filename
mv "${FILE}" "${NEWNAME}.jpg"
# Increment i
i=$(( i + 1 ))
done
exit 0
You can put the above in a bash script (e.g. renamejpg.sh) and make it executable. If the
script, namelist.txt and all your jpg files are in the same directory, it should work fine.
Just make sure you have the same number of files as there are lines in namelist.txt.
Cheers
Last edited by aragorn2101; 12-14-2016 at 03:26 AM.
#!/bin/bash
# Initializing variables
i=1
NEWNAME=""
# Loop over each file
for FILE in `ls -1 *.jpg`; do
# Retrieving ith line from text file
NEWNAME=`sed -n "${i}p" namelist.txt`
echo "move ${FILE} -> ${NEWNAME}.jpg ..."
# Renaming file; double quotes because of spaces in filename
mv "${FILE}" "${NEWNAME}.jpg"
# Increment i
i=$(( i + 1 ))
done
exit 0
You can put the above in a bash script (e.g. renamejpg.sh) and make it executable. If the
script, namelist.txt and all your jpg files are in the same directory, it should work fine.
Just make sure you have the same number of files as there are lines in namelist.txt.
Cheers
thnankyou so much @aragorn2101,, it worked perfectly..
here is my full work ..
Code:
.
============this file have keywords=======
root@d2:/home/navdeep/Desktop/rndm# cat srt
cars
wallpaper
fastest
speed
tyres
honda
hyundai
bajaj
duke
engine
track
rider
winner
===========================================
=====this is script which will generate random 7 words title for file=====
root@d2:/home/navdeep/Desktop/rndm# cat rsc
----------------------------------------------------
#!/bin/bash
rm -f namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
sort -R srt | head -n 7 | sed -e :a -e '{N; s/\n/ /g; ta}' >> namelist.txt
cat namelist.txt
===================================================
======the upper script will generate below file===
root@d2:/home/navdeep/Desktop/rndm# cat namelist.txt
tyres rider wallpaper track winner speed cars
hyundai engine bajaj tyres duke rider honda
fastest tyres cars wallpaper speed hyundai engine
speed cars wallpaper rider honda fastest duke
bajaj speed duke honda cars tyres track
cars track honda duke speed winner bajaj
speed tyres duke wallpaper winner rider engine
wallpaper tyres engine honda duke cars hyundai
fastest speed tyres bajaj cars honda duke
tyres bajaj fastest speed hyundai rider wallpaper
cars tyres bajaj track engine speed duke
duke tyres track speed honda cars hyundai
bajaj winner engine wallpaper duke rider fastest
wallpaper engine winner tyres bajaj cars fastest
hyundai cars wallpaper honda track engine winner
cars duke wallpaper hyundai track rider speed
===================================================
#### now we have a file which have 7 words title for renaming files . ####
suppose we have 10 files having name like aa10.jpg aa2.jpg aa4.jpg aa6.jpg aa8.jpg
aa1.jpg aa3.jpg aa5.jpg aa7.jpg aa9.jpg
=====this below script will change these names . the names will be taken from namelist.txt ====
######### this script will change renames #########
#!/bin/bash
# Initializing variables
i=1
NEWNAME=""
# Loop over each file
for FILE in `ls -1 *.jpg`; do
# Retrieving ith line from text file
NEWNAME=`sed -n "${i}p" namelist.txt`
echo "move ${FILE} -> ${NEWNAME}.jpg ..."
# Renaming file; double quotes because of spaces in filename
mv "${FILE}" "${NEWNAME}.jpg"
# Increment i
i=$(( i + 1 ))
done
exit 0
##############################################
==== this is how execution went =====
move aa1.jpg -> tyres rider wallpaper track winner speed cars.jpg ...
move aa2.jpg -> hyundai engine bajaj tyres duke rider honda.jpg ...
move aa3.jpg -> fastest tyres cars wallpaper speed hyundai engine.jpg ...
move aa4.jpg -> speed cars wallpaper rider honda fastest duke.jpg ...
move aa5.jpg -> bajaj speed duke honda cars tyres track.jpg ...
move aa6.jpg -> cars track honda duke speed winner bajaj.jpg ...
move aa7.jpg -> speed tyres duke wallpaper winner rider engine.jpg ...
move aa8.jpg -> wallpaper tyres engine honda duke cars hyundai.jpg ...
move aa9.jpg -> fastest speed tyres bajaj cars honda duke.jpg ...
move aa10.jpg -> tyres bajaj fastest speed hyundai rider wallpaper.jpg ...
================== Done :) ===================
it was just test method , we can put thousands of keywords and it will be all automated.. im gonna merge everything into one script . :) ..
Last edited by Navdeep.D2; 12-14-2016 at 04:09 AM.
You should not use ls because if the files in your directory already had spaces in them it would not all go as planned.
You also 'read' each line of your file into a variable instead of using sed.
In future, it would be nice if you followed the instructions as per Jjanel's post that you are to at least show that you have made an attempt instead of asking others to solve for you.
Also, remember to mark as SOLVED now you have a solution
You should not use ls because if the files in your directory already had spaces in them it would not all go as planned.
You also 'read' each line of your file into a variable instead of using sed.
In future, it would be nice if you followed the instructions as per Jjanel's post that you are to at least show that you have made an attempt instead of asking others to solve for you.
Also, remember to mark as SOLVED now you have a solution
@grail ... with all due respect, im not here to argue, but im sorry if my reply to Jjanel was bad or not good.
but he misunderstand my question.
he thought i have 15 files names in text file.
i know this below tips of vim , Jjanel posted it in his reply, later on he edited. its okay .
:%s/^/rm /
:%s/$/.jpg/
evrybody knows this command will add rm at first and .jpg at last of every line . But it will be only text in a plain file ..
u getting me . ?
he thought i want to change file's inside content but actually i wanted to change file's name.
for record , i didnt try that code because the second i read it , i ignored it. because it was irrelevant. :|
but Jjanel thanks , maybe it will help me in future for some other kind of code.
@grail.. You should not use ls because if the files in your directory already had spaces in them it would not all go as planned.
yea, thanks ,, but my all files will be having NO SPACE. even if they will have , i will bulk rename them like this .. Select all files-->> right click on any file-->rename abc or anything 123-->> all files will be having same name+ascedning digits.. abc.jpg abc(1).jpg abc(2).jpg ..
i copied this code sed -e :a -e '{N; s/\n/ /g; ta}' from internet just to convert my vertical word list to --> horizontal words ..
my output was this ..
1
2
3
4
5
but i needed it like this .
1 2 3 4 5
so i searched online and got that code . thanks for welcoming me to LQ.
i got solution in couple of minutes .
thnkyou guys .
Last edited by Navdeep.D2; 12-14-2016 at 05:32 AM.
Actually, with respect to vim you have missed the point. You could in fact pull all the file names into your existing file and add mv to the start of each line and then run that file as a script.
As for using ls, it is always a bad idea and should not become standard practice. nice that you are currently able to control the file naming but you may not always be able to and falling foul of the ls
issue later may cause issues that you cannot recover from, ie. naming files to something unexpected and then not knowing which file needs to be corrected without examining each file in turn.
I assumed, perhaps incorrectly, that you were looking to learn and improve your skills in scripting. If you are going to go to the extent of right clicking on all files to rename them then why bother with
a script at all?
I am also not sure what your final comment about the sed is referring to as it does not seem to have any relevance to this question? Or is this another question??
Actually, with respect to vim you have missed the point. You could in fact pull all the file names into your existing file and add mv to the start of each line and then run that file as a script.
That's the way I'd approach this one also and have done so many times in the past. It's fast to build a script using regex inside vi or vim and then execute it. However, if a script is needed, my guess would be something like this:
Code:
for name in $(cat names.txt);do
read -u 3 file;
if [ -n "$file" ]; then
echo $file = $name;
fi;
done 3< <(find . -type f -name '*.jpeg' -print)
@Turbocapitalist - unfortunately I would have to disagree with your script as well as OP has advised the file contains spaces, so a while loop would be a better option
You could also stick with the for loop but flip the options:
Code:
for file in *
do
read -u 3 name
echo mv "$file" "$name"
done 3<names.txt
Here the globbing will save the worry of white space in the file names and read will consecutively read from the file.
Actually, with respect to vim you have missed the point. You could in fact pull all the file names into your existing file and add mv to the start of each line and then run that file as a script.
i get it .. now im thinking i didnt even needed this script in first place .. :|
Quote:
As for using ls, it is always a bad idea and should not become standard practice. nice that you are currently able to control the file naming but you may not always be able to and falling foul of the ls
issue later may cause issues that you cannot recover from, ie. naming files to something unexpected and then not knowing which file needs to be corrected without examining each file in turn.
I assumed, perhaps incorrectly, that you were looking to learn and improve your skills in scripting. If you are going to go to the extent of right clicking on all files to rename them then why bother with
a script at all?
because i need random titles for everyfile from a particular keywords list. u wont understand, leave.
yeah ,
Quote:
I am also not sure what your final comment about the sed is referring to as it does not seem to have any relevance to this question? Or is this another question??
no no , i just explained why i used that sed command. thats it . No question.
i get it done what needed to be done. yes, not the correct way, but i learned alot too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.