LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-14-2016, 02:00 AM   #1
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Rep: Reputation: Disabled
Smile Rename All Files from a TextList, How ????


i have 15 files in directory.
/root/

Code:
a1.jpg
a2.jpg
a3.jpg
a4.jpg
a5.jpg
a6.jpg
a7.jpg
image00.jpg
image01.jpg
img1.jpg
img2.jpg
img3.jpg
pic01.jpg
pic02.jpg
pic03.jpg
i have a text file which have 100 names list.

Code:
cat namelist.txt 

duke hyundai bajaj honda speed wallpaper engine
cars wallpaper track fastest speed winner engine
fastest track engine tyres bajaj honda duke
hyundai wallpaper speed bajaj cars tyres rider
track rider speed engine hyundai bajaj duke
speed hyundai engine honda rider fastest wallpaper
tyres fastest engine cars duke honda bajaj
wallpaper cars track winner engine honda hyundai
cars tyres winner track engine wallpaper honda
winner hyundai wallpaper bajaj tyres cars engine
cars rider engine wallpaper fastest speed winner
rider tyres winner wallpaper engine track bajaj
wallpaper bajaj honda tyres track hyundai engine
honda duke speed rider tyres engine hyundai
cars duke wallpaper rider honda tyres bajaj
fastest winner duke hyundai wallpaper track rider
wallpaper honda speed fastest track winner duke
winner tyres fastest duke speed rider engine
bajaj honda cars duke winner tyres rider
engine wallpaper tyres honda hyundai rider fastest
wallpaper rider fastest cars bajaj hyundai speed
bajaj rider fastest speed winner track duke
.
.
.
so on
Those 15 files should be rename like this .
Code:
duke hyundai bajaj honda speed wallpaper engine.jpg
cars wallpaper track fastest speed winner engine.jpg
fastest track engine tyres bajaj honda duke.jpg
hyundai wallpaper speed bajaj cars tyres rider.jpg
track rider speed engine hyundai bajaj duke.jpg
speed hyundai engine honda rider fastest wallpaper.jpg
tyres fastest engine cars duke honda bajaj.jpg
wallpaper cars track winner engine honda hyundai.jpg
cars tyres winner track engine wallpaper honda.jpg
winner hyundai wallpaper bajaj tyres cars engine.jpg
cars rider engine wallpaper fastest speed winner.jpg
rider tyres winner wallpaper engine track bajaj.jpg
wallpaper bajaj honda tyres track hyundai engine.jpg
honda duke speed rider tyres engine hyundai.jpg
cars duke wallpaper rider honda tyres bajaj.jpg
please tell me how it can be done .

Last edited by Navdeep.D2; 12-14-2016 at 02:43 AM. Reason: title edited..
 
Old 12-14-2016, 02:36 AM   #2
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 363Reputation: 363Reputation: 363Reputation: 363
Do you know vi? Maybe research these on the web: http://vimregex.com/

Welcome but LQ req. that you show what you have tried.
Maybe tell us a bit about your 'distro'/PC, and Linux experience/interests/goals.

Best wishes!

Last edited by Jjanel; 12-14-2016 at 02:53 AM.
 
Old 12-14-2016, 02:42 AM   #3
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Original Poster
Rep: Reputation: Disabled
OMG... thats not what i said ..
i want those 15 files to be renamed like that ...


btw i know what vim is . :|

Last edited by Navdeep.D2; 12-14-2016 at 02:44 AM.
 
Old 12-14-2016, 03:17 AM   #4
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 363Reputation: 363Reputation: 363Reputation: 363
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

Last edited by Jjanel; 12-14-2016 at 06:24 PM.
 
Old 12-14-2016, 03:24 AM   #5
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Hi, welcome to LQ.

A small bash script should do the job nicely.

Code:
#!/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.
 
1 members found this post helpful.
Old 12-14-2016, 04:06 AM   #6
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by aragorn2101 View Post
Hi, welcome to LQ.

A small bash script should do the job nicely.

Code:
#!/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.
 
Old 12-14-2016, 04:13 AM   #7
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Jjanel View Post
Did you want a1.jpg to be renamed (`mv`) to exactly, with spaces!:
'duke hyundai bajaj honda speed wallpaper engine.jpg'
?
yes , exactly..its done now ... but thanks , .
 
Old 12-14-2016, 04:46 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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
 
Old 12-14-2016, 05:22 AM   #9
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 12-14-2016, 05:49 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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??
 
Old 12-14-2016, 06:16 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,891
Blog Entries: 3

Rep: Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575Reputation: 3575
Quote:
Originally Posted by grail View Post
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)
 
Old 12-14-2016, 12:06 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
@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.
 
2 members found this post helpful.
Old 12-15-2016, 04:00 AM   #13
Navdeep.D2
LQ Newbie
 
Registered: Dec 2016
Location: India,Punjab
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 02:02 AM
[SOLVED] How to rename files drnick911@yahoo.com Linux - Newbie 6 10-21-2010 04:23 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
get files from ftp and rename those files.. amaulana Linux - Newbie 2 11-15-2008 10:14 PM
how to rename files bkcreddy17 Programming 7 09-14-2008 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration