LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-26-2016, 01:20 PM   #1
layala23
LQ Newbie
 
Registered: Oct 2016
Posts: 2

Rep: Reputation: Disabled
CP question - Grab file name from first argument and rename that to the same filename.bak


I need to figure out how to copy a file name by the first argument to a file with the same name with .bak appended to it.

I've gotten to here, but it won't work?

cp $1 $1.bak
But I received the following error:

cp: missing destination file operand after ‘.bak’
 
Old 10-26-2016, 01:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,988

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
Please show complete example, ie. what is the value of $1?
 
Old 10-26-2016, 01:58 PM   #3
layala23
LQ Newbie
 
Registered: Oct 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Please show complete example, ie. what is the value of $1?
So for instance I'm using a filename with a space in it.

bash bakctrol help me

bakctrol being my script which I believe to have working, possibly. with the one caveat that it does not play nicely with spaced filenames, which is something I would like to accomplish if run regularly.

Output of bakctrol

#!/bin/bash
#This will copy the filename called in the argument and append a .bak to it, will be used for version control in this environment.
cp "$1" "${1}".bak
 
Old 10-26-2016, 02:27 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,988

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
If the name of the file has a space in it then it must be quoted. At the moment the value of $1 will be 'help'
 
Old 10-26-2016, 05:01 PM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
Quote:
Originally Posted by layala23 View Post
bash bakctrol help me
should be

bash bakctrol "help me"

- or, you could modify your script thusly:

cp "$@" "$@".bak (not tested)
 
Old 10-26-2016, 05:31 PM   #6
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by layala23 View Post
I need to figure out how to copy a file name by the first argument to a file with the same name with .bak appended to it.
Something like this?
Code:
cp /etc/{fstab,fstab.bak}
 
Old 10-26-2016, 09:47 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
cp -v file file.bk
 
Old 10-27-2016, 02:49 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,635

Rep: Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145Reputation: 1145
You must quote a file name with spaces also when you are calling your script
Code:
bash bakctrol "help me"
If you add a loop
Code:
#!/bin/bash
#This will copy the filename called in the argument and append a .bak to it, will be used for version control in this environment.
#loop over all arguments
for arg
do
  cp "$arg" "$arg".bak
done
Then you can run it with several arguments (file names)
Code:
./bakctrol "help me" file2 file3

Last edited by MadeInGermany; 10-27-2016 at 05:00 AM. Reason: (file names)
 
Old 10-28-2016, 12:23 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
Quote:
it does not play nicely with spaced filenames,
Actually, that's normal/std for just about every *nix cmd: spaces are treated as param separators unless specially dealt with, as you've discovered ...

Basically, do not create filenames with spaces, AND if any are given to you to process, I always rename (or take a copy) with underscores instead of spaces and then (if reqd) put spaces back at the end.
Trust me, you'll thank me later ...
 
Old 10-28-2016, 09:01 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by chrism01 View Post
Actually, that's normal/std for just about every *nix cmd: spaces are treated as param separators unless specially dealt with, as you've discovered ...

Basically, do not create filenames with spaces, AND if any are given to you to process, I always rename (or take a copy) with underscores instead of spaces and then (if reqd) put spaces back at the end.
Trust me, you'll thank me later ...
He is right ya know. Spaces can be a bitch, and them specail characters. Don't get this guy talking about them.
Trust me, you'll thank me later ...

Last edited by BW-userx; 10-28-2016 at 09:02 AM.
 
Old 10-28-2016, 09:17 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,542

Rep: Reputation: 870Reputation: 870Reputation: 870Reputation: 870Reputation: 870Reputation: 870Reputation: 870
Filenames with spaces look beautiful under icons in gui file manager
 
Old 10-28-2016, 01:14 PM   #12
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
and the winner of this codegolf tournament is...
Quote:
Originally Posted by Habitual View Post
Something like this?
Code:
cp /etc/{fstab,fstab.bak}
 
Old 10-28-2016, 01:18 PM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by ondoho View Post
and the winner of this codegolf tournament is...
I thought that was pseudo code, yes. and you tried running that?
Quote:
cp /etc/{fstab,fstab.bak}
 
Old 10-28-2016, 03:56 PM   #14
LennyUsesManjaro
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Rep: Reputation: Disabled
A shorter version

Code:
cp /etc/fstab{,.bak}
If you put echo in front of it you can get a preview of what this brace expansion will expand to.

Code:
echo cp /etc/fstab{,.bak}
cp /etc/fstab /etc/fstab.bak
If a file has an extension, you can do something like this

Code:
cp file.txt{,.bak}
will expand to this.

cp file.txt file.txt.bak

Last edited by LennyUsesManjaro; 10-28-2016 at 04:11 PM.
 
Old 10-31-2016, 02:20 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052Reputation: 6052
Quote:
Originally Posted by LennyUsesManjaro View Post
A shorter version
Code:
cp /etc/fstab{,.bak}
cosequently i should announce that you are the new winner!

disclaimer:
if someone finds an even shorter version, they can congratulate themselves!
 
  


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
Rename rar file based on filename contained in it rahlquist Linux - Newbie 4 09-03-2013 10:18 PM
Why my ifcfg-eth1 file have the extension .bak (ifcfg-eth1.bak) cmx08 Linux - Networking 4 08-23-2008 08:28 AM
Advanced VI Question - Grab IP Addresses out of log file? tbeehler Linux - Software 9 08-27-2007 10:47 AM
Filename rename jarod_123 Programming 4 03-21-2006 03:40 PM
Grab Filename on the end of a (Download) URL hitman_et Programming 3 12-02-2005 05:52 AM

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

All times are GMT -5. The time now is 09:22 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