1

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

/opt/very/long/path/file1

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

4 Answers 4

4

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
1
  • I noticed only one disadvantage: filenames autocomplete isn't working. When I type { I have to type whole filename. When my filename is very long I can type path/ and press [TAB] so it will autocomplete to path/fileA. Next I press arrow left many times to insert {, so I have: path/{fileA. Next I press [END] type , and write manually fileB (or select with mouse and copy-paste if filename is really very long), and at the end type }.
    – mkczyk
    Sep 3, 2019 at 14:30
0

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

e.g

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

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

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
1
  • In this scenario, that still requires them to type /opt/other/very/very/long/path twice...
    – Jeff Schaller
    Feb 7, 2019 at 18:33
0

You can type:

cp /opt/other/very/very/long/path/fileA !#:1:h/fileB

it will expand in history to:

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

Explanation:

  • !# - get current line
  • :1 - get first argument
  • :h - get folder

(thanks for this article)

Disadvantage is that:

  • you can't press up arrow to get command you typed, because it saved expanded in history,
  • second filename autocomplete isn't working.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .