LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 04:39 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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, 06:00 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

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 06:03 PM.
 
2 members found this post helpful.
Old 07-23-2016, 08:04 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 06:56 AM   #4
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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, 09:05 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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, 05:20 PM   #6
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

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, 08:54 AM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

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



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

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

All times are GMT -5. The time now is 05:25 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
Open Source Consulting | Domain Registration