shell question: the "mv" command with multiply files
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.
shell question: the "mv" command with multiply files
hi
I have the following folder structure:
folder1
subfolder1.1
subfolder1.2
subfolder1.3
folder2
subfolder2.1
subfolder2.2
subfolder2.3
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?
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.
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.
$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
done
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
like:
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).
Or:
for i in $dirs; do
cd ${i}
mv index_new.html index.html
cd -;
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.