Guest Posted February 25, 2016 Share Posted February 25, 2016 Hi all, I like to copy a file to the same directory, but with a different name. The directory where the files are in, is not my current working dir. So the command would be: sudo cp /var/www/html/test1.php /var/www/html/test_backup.php is there a way that I don't have to type the path twice (and without changing my working directory) Thanks! Quote Link to comment Share on other sites More sharing options...
JumboPackets Posted February 25, 2016 Share Posted February 25, 2016 I don't know how much it will save you but, if you're using BASH (not sure abut other shells), you can put a list inside curly braces ant it will "iterate" through them. like: sudo cp /var/www/html/{test1.php,test_backup.php} untested, your milage may very, not responsible, etc. :) Quote Link to comment Share on other sites More sharing options...
Guest Posted February 26, 2016 Share Posted February 26, 2016 (edited) Yep, that works I was looking for some sort of flag to the cp command that does that but it's not in the man pages. But this option also works for other commands like mv. So thanks! Edited February 29, 2016 by Guest Quote Link to comment Share on other sites More sharing options...
cooper Posted February 29, 2016 Share Posted February 29, 2016 I would've tried to cd into the folder and then do the copy. So cd /var/www/html && sudo cp test1.php test_backup.php && cd - Quote Link to comment Share on other sites More sharing options...
Guest Posted February 29, 2016 Share Posted February 29, 2016 That's also a nice one, didn't know the cd - option ;) My point of view on this is: if you're doing something and think: hm, there should be a better way to do this, then there usually is! Quote Link to comment Share on other sites More sharing options...
cooper Posted March 1, 2016 Share Posted March 1, 2016 The danger with relying on shell expansion with {} like is done in the accepted answer is that it's bash-specific and it slightly hides the fact that the name before the comma is the source and the one after is the target. If you add another comma and a file name, you're copying the first 2 files to the last file which hopefully denotes a directory or the command will fail. It's something that's quite easy to overlook and something an uninformed user might try when adapting the command to their own, personal use. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.