Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-06-2005, 11:31 AM   #1
Registered: Mar 2005
Posts: 54

Rep: Reputation: 15
shell question: the "mv" command with multiply files


I have the following folder structure:



where is "subfolder1.1" is a subfolder under "folder1"
I have 2 files in each subfolder: "index.html", and "index_new.html"

I would like to rename "index_new.html" to "index.html", and by doing so to overwrite "index.html", and be left with only the "index.html" file (which content is what was in "index_new.html") in one command line.

this is basically what the "mv" command is used for.

why isn't this working for it:
mv folder[0-9]/subfolder*/index_new.html folder[0-9]/subfolder*/index.html

should I be using the "$0" var or am I completely off track with this?

Old 04-06-2005, 12:22 PM   #2
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
If the mv command is given several arguments (filenames) the last one has to be a directory name. You are trying to move all the index__new.html files to one file.

Try 'find' or 'foreach'

Old 04-06-2005, 01:44 PM   #3
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
john is right. the reason is that one of the first things the shell does is expand the command line, so file[1-3] is expanded to file1 file2 file3, the mv command is then passed a long list of filenames. i actually have written a small script that allows you to do what your looking for. if you have a file containing all the old filenames, and onother containing all the new filenames, it will go down the list and move one to the other. i dont have time right now, but if you want i can post the script later.
Old 12-19-2005, 06:41 AM   #4
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Try the following:

dirs=`find folder1 folder2 -type d`;

$dirs now contains al list of all subfolders in folder1 and folder2 (note: also
subfolders of subfolders).
So, we must now try to issue a "mv" command for all subfolders in $dirs

In Bash, this is something like:

for i in $dirs; do
mv ${i}/index_new.html ${i}/index.html

In this case, it's easy to verify if your "mv" commands are correct before actually
executing them. You can accomplish this by enclosing the "mv" command in an "echo" command
echo "mv ${i}/index_new.html ${i}/index.html"
and running that in the same loop as above.
If the "mv" commands that are printed to your screen seem OK to you, execute the loop
as it is written above (without the echo).


for i in $dirs; do
cd ${i}
mv index_new.html index.html
cd -;
Old 12-19-2005, 08:42 AM   #5
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
or this in the root directory

rename index_new index */*/*.html
...if this thread is still alive.


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
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
Reconfigure command in "perl -MCPAN -e shell" asciimonster Linux - Software 1 09-16-2004 09:48 AM
"make" command not working in shell unasriseth Linux - Newbie 6 05-25-2004 10:08 AM
equivalent of "what" shell command in Linux raees Linux - General 4 03-16-2004 09:59 AM
"how do I extract a number from a text file using shell command?" sdandeker Linux - Networking 3 02-12-2004 09:54 AM > Forums > Linux Forums > Linux - Newbie

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

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