LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   CP question - Grab file name from first argument and rename that to the same filename.bak (https://www.linuxquestions.org/questions/linux-newbie-8/cp-question-grab-file-name-from-first-argument-and-rename-that-to-the-same-filename-bak-4175592282/)

layala23 10-26-2016 12:20 PM

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’

grail 10-26-2016 12:37 PM

Please show complete example, ie. what is the value of $1?

layala23 10-26-2016 12:58 PM

Quote:

Originally Posted by grail (Post 5623271)
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

grail 10-26-2016 01:27 PM

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'

ondoho 10-26-2016 04:01 PM

Quote:

Originally Posted by layala23 (Post 5623283)
bash bakctrol help me

should be

bash bakctrol "help me"

- or, you could modify your script thusly:

cp "$@" "$@".bak (not tested)

Habitual 10-26-2016 04:31 PM

Quote:

Originally Posted by layala23 (Post 5623264)
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}

BW-userx 10-26-2016 08:47 PM

Code:

cp -v file file.bk

MadeInGermany 10-27-2016 01:49 AM

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

chrism01 10-27-2016 11:23 PM

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 ... :)

BW-userx 10-28-2016 08:01 AM

Quote:

Originally Posted by chrism01 (Post 5623959)
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 ... :) :D :D

keefaz 10-28-2016 08:17 AM

Filenames with spaces look beautiful under icons in gui file manager :(

ondoho 10-28-2016 12:14 PM

and the winner of this codegolf tournament is...
Quote:

Originally Posted by Habitual (Post 5623380)
Something like this?
Code:

cp /etc/{fstab,fstab.bak}


BW-userx 10-28-2016 12:18 PM

Quote:

Originally Posted by ondoho (Post 5624162)
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}

LennyUsesManjaro 10-28-2016 02:56 PM

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

ondoho 10-31-2016 01:20 AM

Quote:

Originally Posted by LennyUsesManjaro (Post 5624237)
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!
;)


All times are GMT -5. The time now is 10:03 PM.