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’ |
Please show complete example, ie. what is the value of $1?
|
Quote:
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 |
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'
|
Quote:
bash bakctrol "help me" - or, you could modify your script thusly: cp "$@" "$@".bak (not tested) |
Quote:
Code:
cp /etc/{fstab,fstab.bak} |
Code:
cp -v file file.bk |
You must quote a file name with spaces also when you are calling your script
Code:
bash bakctrol "help me" Code:
#!/bin/bash Code:
./bakctrol "help me" file2 file3 |
Quote:
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 ... :) |
Quote:
Trust me, you'll thank me later ... :) :D :D |
Filenames with spaces look beautiful under icons in gui file manager :(
|
and the winner of this codegolf tournament is...
Quote:
|
Quote:
Quote:
|
A shorter version
Code:
cp /etc/fstab{,.bak} Code:
echo cp /etc/fstab{,.bak} Code:
cp file.txt{,.bak} cp file.txt file.txt.bak |
Quote:
disclaimer: if someone finds an even shorter version, they can congratulate themselves! ;) |
All times are GMT -5. The time now is 08:06 AM. |