Jump to content

quick Linux cp command question


Guest

Recommended Posts

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!

Link to comment
Share on other sites

Yep, that works :wink: 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 by Guest
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...