I have a file in a very long path, for example:


I want to copy the file in this directory:

cp /opt/very/long/path/file1 /opt/very/long/path/file2

I don't want repeat this long path. I can go to the destination folder and copy:

cd /opt/very/long/path/
cp file1 file2

But I don't want change directory. One of the reason is: if I had many long paths I would have to go to directories every time.

cd /opt/very/long/path/
cp file1 file2
cd /opt/other/very/very/long/path/
cp fileA fileB

Another reasons are: I want to keep context and clear history (every command says what and where was copied).

Therefore, better is not change directory:

cp /opt/very/long/path/file1 /opt/very/long/path/file2
cp /opt/other/very/very/long/path/fileA /opt/other/very/very/long/path/fileB

But I have to repeat paths.

There is a shortcut like this?

cp /opt/very/long/path/file1 ./file2
cp /opt/other/very/very/long/path/fileA ./fileB

But a dot . means "current directory". There is any character that means "destination directory" or "source directory"?

cp /opt/very/long/path/file1 <destination>/file2
cp /opt/other/very/very/long/path/fileA <destination>/fileB

Brace expansion is nice for this:

cp /opt/other/very/very/long/path/{fileA,fileB}

... will expand to:

cp /opt/other/very/very/long/path/fileA /opt/other/very/very/long/path/fileB

when it actually executes.

The command will show up in your history as you typed it, which preserves the paths:

$ history
  # ...
  508  cp /opt/other/very/very/long/path/{fileA,fileB}
  509  history

You can set a variable with the very log path and use that variable instead in cp command.


export dir="/opt/very/long/path" 

cp "$dir"/file "$dir"/file2
  • 2
    note that there's no real need to export dir here – Jeff Schaller Feb 7 at 14:44

You can use rsync if you want to sync between 2 folders.

rsync can be used to sync in local host and remote hosts, too.

For the above scenario, it's used in localhost:

rsync -avzh sourcefolder destinationfolder
  • In this scenario, that still requires them to type /opt/other/very/very/long/path twice... – Jeff Schaller Feb 7 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.