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.
Using mv file{,.bak} renames "file" to "file.bak". My question is, can I use this syntax safely? I mean, it does seem to achieve what I want, but is it correct, so to say, give that {,.bak} can mean both nothing or ".bak"?
What bothers me in a way is that it does something useless before actually renaming it, namely it tries to rename "file" to "file", and then it achieves what I what.
If you are using the bash shell, then perhaps the following will illustrate what's happening.
This command:
Code:
echo file{.other,.bak}
produces this output:
Code:
file.other file.bak
This command:
Code:
echo file{,.bak}
produces this output:
Code:
file file.bak
So a list is expected, and the list you are giving it is nothing followed by .bak
As a result it is appending nothing to "file" and doing the rename, then appending .bak to file and doing the rename ( i.e. mv ).
If you wish to rename a single file you can simply do:
Code:
mv file file.bak
If instead you wish to rename multiple files, you can do something like this:
Code:
for file_name in *
do
mv $file_name $file_name.bak
done
The above would rename all the files in the current directory, which have names that don't start with a dot. Naturally you can replace the asterisk with a different pattern that matches only selected file names.
1. Yes
2. Yes it's fine to use as this as long as the shell has brace expansion (bash, sh, csh ..)
Mmmmmmmmm, well you might want to keep in mind that a classical Bourne shell sh can do brace expansion somewhat differently than a fully "modern" bash shell. I believe that there's even a mention of that somewhere in the bash manual page. Also, often running something like /bin/sh on Linux will actually result in running a form of bash.
That all is why I tried to stress that what you suggest would work, if you are using the bash shell.
Breaking down a command into its individual elements, and testing them separately, can be a rather simple way to understand what the overall command will do. That's why I illustrated what the brace expansion would be, using echo commands. As keefaz mentioned, the result of the brace expansion is what's then passed to the mv command. You can do the same sort of thing with a variety of patterns/expansions/etc., using the echo command to see what the result of a pattern/expansion/etc will be, before using it in a more complete command. In that sense, the echo command tends to allow for harmless investigation of what would happen, if you used something in the command you ultimately wish to execute.
Well actually I was interested more in the bash shell, of course, although I'm very much interested also if there are exceptions, such as the sh (although I know that in most modern operating system /bin/sh is simply a link to /bin/bash).
Very useful details, indeed, regarding the echo commands. I'll try it out in the future, 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.