3

Context: I am navigating around in folders, and finding files I need. I would like to copy my files somewhere to place them somewhere else. But I don't want to have to deal with trying to figure out relative paths for the source and destination.

Basically I want something like what I can do in file explorers (select files, copy, paste somewhere else). So a workflow like...

cd some/crazy/directory/with/too/long/a/name
copy file 
cd ~/some/other/place
paste 
3
  • not really a solution - but have you considered pushdand popd. You might do something like mv `popd`/* . as alternative to cut/paste
    – bdecaf
    Feb 20, 2013 at 9:07
  • Is this about Linux or Windows?
    – gronostaj
    Feb 20, 2013 at 9:13
  • This is for Linux
    – rtpg
    Feb 20, 2013 at 9:20

3 Answers 3

1

There is no built-in command that does this in bash. However, there is a script (one function and one alias) that you can put into your .bashrc which gives you the functionality.

These are detailed here: http://www.webupd8.org/2010/10/copy-paste-files-from-command-line-bash.html

And are:

ccopy(){ for i in $*; do cp -a $i /tmp/ccopy.$i; done }
alias cpaste="ls -d /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./%"

As the description on the site says, just add these two lines to your .bashrc, and you can then use ccopy to add files to the list of files to copy, then cpaste to paste them to the destination. You can also 'manage' the list of files by looking at /tmp/ccopy*.

NOTE - this script is limited to regular files with 'normal' names. It explicitly doesn't cope with 'spaces' in the filename, and also won't copy/paste folders. The linked article has a link to a version which does work with folders, but I don't know if that version can handle spaces or non-regular characters in the file/folder names. I do most of my (linux) work from the command line so this version has met my use-case.

2
  • Warning: this answer will not cope with spaces or quotes (or possibly other meta-characters) in file names. Feb 20, 2013 at 8:45
  • @AdrianPronk Thanks - the article I linked to covers that - and also that it doesn't handle folders (neither are needed for my use-case) - but I'll edit my answer to note both restrictions.
    – PJC
    Feb 20, 2013 at 8:51
2

Even if you don't want to use the script suggested by @PJC, you can use its main idea: first copy file(s) to /tmp, then take from there:

cd some/crazy/directory/with/too/long/a/name
cp file /tmp
cd ~/some/other/place
cp /tmp/file .

It doesn't solve the problem completely: you have to re-enter file name, but at least without its path. I'm using it when I'm feeling lazy.

1

You could assign the filename to a variable. readlink -f will give you the /absolute/path/to/file.

cd some/crazy/directory/with/too/long/a/name
copy="$(readlink -f file)"
cd ~/some/other/place
cp "$copy" newfile

This will only work for a single file at a time, of course.

You must log in to answer this question.

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