LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 12: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, 12:37 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Please show complete example, ie. what is the value of $1?
 
Old 10-26-2016, 12: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, 01:27 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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, 04:01 PM   #5
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,876

Rep: Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053
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, 04:31 PM   #6
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 7,809
Blog Entries: 11

Rep: Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104Reputation: 2104
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, 08:47 PM   #7
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,329

Rep: Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575
Code:
cp -v file file.bk
 
Old 10-27-2016, 01:49 AM   #8
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 277

Rep: Reputation: 111Reputation: 111
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 04:00 AM. Reason: (file names)
 
Old 10-27-2016, 11:23 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,295

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
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, 08:01 AM   #10
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,329

Rep: Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575
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 08:02 AM.
 
Old 10-28-2016, 08:17 AM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Filenames with spaces look beautiful under icons in gui file manager
 
Old 10-28-2016, 12:14 PM   #12
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,876

Rep: Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053Reputation: 1053
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, 12:18 PM   #13
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 3,329

Rep: Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575Reputation: 575
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, 02: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 03:11 PM.
 
Old 10-31-2016, 01:20 AM   #15
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,876

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


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