Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Instead of using the following command:

cp {source file} {dest file}

I want to be able to copy a file into the clipboard, and paste it somewhere else, in another directory. something like this:

/usr/local/dir1# cp {source file}
/usr/local/dir1# cd /usr/local/dir2
/usr/local/dir2# paste

Is it possible?

share|improve this question
add comment

3 Answers 3

up vote 13 down vote accepted

I think you should do something like the GUI applications do. My idea for doing this is to write two functions for Copy and Paste, where Copy writes path of files to be copied to a temporary file and Paste reads those paths and simply calls cp command. My implementation (to be put in .bashrc file) is like below:

function Copy {
    touch ~/.clipfiles
    for i in "$@"; do
      if [[ $i != /* ]]; then i=$PWD/$i; fi
      i=${i//\\/\\\\}; i=${i//$'\n'/$'\\\n'}
      printf '%s\n' "$i"
    done >> ~/.clipfiles
}

function Paste {
    while IFS= read src; do
      cp -Rdp "$src" .
    done < ~/.clipfiles
    rm ~/.clipfiles
}

Better scripts could be written for implementing this idea, I tested my own and it works very well for files and folders (I don't know how xclip could work for copying folders!!)


For example:

/usr/local/dir1# Copy a.txt *.cpp
/usr/local/dir1# cd /usr/local/dir2
/usr/local/dir2# Paste

/usr/local/dir1# Copy *.h *.cpp b.txt subdir1
/usr/local/dir1# cd /usr/local/dir2
/usr/local/dir2# Paste

/usr/local/dir1# Copy a.txt b.txt 
/usr/local/dir1# cd /usr/local/dir2
/usr/local/dir2# Copy c.txt d.txt
/usr/local/dir2# cd /usr/local/dir3
/usr/local/dir3# Paste
share|improve this answer
1  
This works like a charm! –  Meysam Feb 4 '12 at 13:55
2  
I like this idea. And with little modifications, you could even implement a "cut" function too. –  Pablo Venturino Feb 4 '12 at 16:03
    
@Gilles Could you please explain why you replaced echo with printf? –  Meysam Feb 5 '12 at 14:53
    
@Meysam To work even under shopt -s xpg_echo. –  Gilles Feb 5 '12 at 16:46
1  
@Meysam printf is portable and safe while echo is not. The Wooledge wiki mentions this briefly. –  jw013 Feb 5 '12 at 21:59
add comment

i suggest xclip. i use it all the time for this. but you don't need a script. just use functions.

i see no need to use these scripts, in particular, because if you are doing what i think you are doing, you will find the scripts already mentioned (on the helpful linux tidbits blog) annoying because they prompt you for input wherever possible, just to use them, contrary to the unix philosophy.

i suggest just writing some functions into your .bashrc:

clip2file () {
  xclip -out -selection clipboard >> $@
  echo -e "\n" >> $@
}

file2clip () {
  cat $@ | xclip -selection clipboard
}

to use these, you need to apt-get install xclip (Debian, Ubuntu, Linux Mint, etc.) or zypper in xclip (openSUSE, other .rpm-based systems) or some equivalent. the program should be available on whatever system you are using.

share|improve this answer
add comment

You can use the xclip command for this. You can download bash scripts from this link and put aliases for bash cp2clip and bash clippaste into your .bashrc file.

share|improve this answer
    
@what do you mean by aliases? –  ixtmixilix Feb 4 '12 at 11:24
    
i meant keep the files in your home directory and put two aliases in your .bashrc alias cp2clip = 'bash /home/<username>/cp2clip.sh' and clippaste='bash /home/<username>/clippaste.sh' so you can use these anywhere. To read more about aliases you can see man alias or linfo.org/alias.html –  harish.venkat Feb 4 '12 at 11:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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