LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-23-2016, 05:39 PM   #1
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 514

Rep: Reputation: 48
renaming file using braces


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.
 
Old 07-23-2016, 07:00 PM   #2
rigor
Member
 
Registered: Sep 2011
Posts: 213

Rep: Reputation: Disabled
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.

Last edited by rigor; 07-23-2016 at 07:03 PM.
 
2 members found this post helpful.
Old 07-23-2016, 09:04 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Quote:
Originally Posted by vincix View Post
namely it tries to rename "file" to "file", and then it achieves what I what.
No, mv command sees only file and file.bak as arguments. The bash shell did the brace expansion before calling the mv command
 
1 members found this post helpful.
Old 07-24-2016, 07:56 AM   #4
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 514

Original Poster
Rep: Reputation: 48
Thanks for the reply, rigor. Very helpful.

@keefaz
So what you're saying is that file{,.bak} means "file" and "file.bak", so that it is equal to "mv file file.bak", right?

Would the command, therefore, be fine to use it as it is? Am I taking any chances if I use it in a different context?
 
Old 07-24-2016, 10:05 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
1. Yes
2. Yes it's fine to use as this as long as the shell has brace expansion (bash, sh, csh ..)
 
1 members found this post helpful.
Old 07-24-2016, 06:20 PM   #6
rigor
Member
 
Registered: Sep 2011
Posts: 213

Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
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.
 
1 members found this post helpful.
Old 07-25-2016, 09:54 AM   #7
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 514

Original Poster
Rep: Reputation: 48
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.
 
  


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
File Renaming Help powerplyer Programming 4 05-10-2014 04:17 PM
bash: renaming file extension based on actual file type alekone Linux - General 9 12-28-2008 10:12 AM
file renaming computer_tom Linux - General 3 02-18-2008 01:27 PM
File renaming amushtaq Linux - Software 2 01-13-2006 06:40 AM
renaming or converting a text file to a dat file... tangaz Linux - Software 1 10-24-2003 07:57 AM


All times are GMT -5. The time now is 04:31 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration