268

I got a file on desktop, file name is ded.html. To copy the file, I click the file and press cmd+c.

Now how would I do the same thing using terminal ?

2

11 Answers 11

388

If I'm understanding the question right, what you're after is pbcopy and pbpaste.

Open a terminal and run:

cat ~/Desktop/ded.html | pbcopy

The file is now in your clipboard.

To put it somewhere else (i.e. paste it) run:

pbpaste > ~/Documents/ded.html

Now you should have a copy of ded.html sitting in ~/Documents.

6
  • 9
    This is great for working with text files, but will fail when you try to perform the task with binaries. Try this out with an image to see what I mean. Jun 5, 2011 at 11:51
  • 9
    after typing $ cat ~/Desktop/ded.html | pbcopy I can not use cmd+v to paste the file. Althought $ pbpaste > ~/Documents/ded.html did the job.
    – angry kiwi
    Jun 5, 2011 at 13:02
  • 4
    You can also view the file contents in your OSX clipboard by going through the Finder menu Edit > Show Clipboard (tested in Yosemite).
    – Dannid
    Aug 11, 2015 at 19:37
  • 7
    This copies the contents of the file, but not the file itself.
    – vy32
    Mar 8, 2019 at 3:24
  • 3
    pbcopy < ~/Desktop/ded.html is OK
    – DawnSong
    Jul 15, 2019 at 5:27
32

Lri’s answer is headed in the right direction, but it has a couple of flaws: there is no need to use Finder (the clipboard is part of the StandardAdditions OSAX), and giving a run handler is a much more reliable way to pass arguments from the command line (since 10.4). Making both of these changes greatly simplifies the “escaping” that needs to be done to enter the program in a shell.

Here is my version (wrapped in a shell function—you could put this in (e.g.) your .bashrc to make it available in your shells):

file-to-clipboard() {
    osascript \
        -e 'on run args' \
        -e 'set the clipboard to POSIX file (first item of args)' \
        -e end \
        "$@"
}

file-to-clipboard ~/Desktop/ded.html

A file that has been put on the clipboard with this script can then be pasted in Finder to copy the file to another folder.

osascript can also be used as a hash-bang interpreter (since 10.5). Put this in a file (e.g. file-to-clipboard)

#!/usr/bin/osascript
on run args
  set the clipboard to POSIX file (first item of args)
end

Make the file executable (chmod +x /path/to/where/ever/you/put/file-to-clipboard). Then run it like so:

/path/to/where/ever/you/put/file-to-clipboard ~/Desktop/ded.html

If it is stored in a directory in the PATH, then you can omit the path to the “script” file.

7
  • @Lri: Finder does not understand the obvious set the clipboard to {one,two} (though another AppleScript program can successfully extract the list). The StandardAdditions clipboard commands may be limited to single items. Jun 8, 2011 at 23:10
  • 1
    Would it work for multiple files?
    – VitalyB
    Mar 2, 2016 at 23:25
  • 2
    Note: The path given has to be an absolute path for the script to work.
    – Leo
    Sep 14, 2019 at 1:22
  • Works for any kind of file, not just text files (unlike pbcopy/bypase)! Convenient if you need to copy an image file.
    – 0xZ3RR0
    Oct 21, 2020 at 10:04
  • 1
    Hi, I tried this answer, the picture can't paste to microsoft word, but can be paste into finder,(in normal,copy a image can either paste to word, a textEdit or finder),can you help more?
    – user956609
    Apr 7, 2021 at 14:49
15

There is no way to achieve what you are trying to do using the command line. While Apple offers the pbcopy and pbpaste tools to allow basic copying of text, you cannot use these tools to copy a file in the sense you're looking for.

Without going in to too much technical detail, when you 'copy' a file in the Finder using C, you're not actually copying the file itself, just making a reference to the file on the clipboard and marking it as a file reference. When an application receives this reference when you paste, it has the responsibility of sorting things out, figuring out what you pasted, and ultimately, working with the file as it sees fit. In essence, when you copy a file in the Finder, it saves the path to the file in a certain way to a certain clipboard, and when you paste, it receives that file path and knows to create a new file using the contents of the old one (copy a file to the clipboard, delete it, try to paste it somewhere else, and see what happens, for instance).

In this sense of copying and pasting, the tools available at hand are not enough to do what you need. As boehj suggested, you can try to copy the contents of the old file into a new one, but this will only work well for text files. Any binary files you get will be corrupted (try doing this with an image – it becomes corrupted).

The traditional command line will fail you in this way, but you can take a look if you'd like into doing this in AppleScript, then invoking that through the command line with osascript.

0
14

Executing pbcopy < filename command in the Terminal copies the contents of the file named filename.

2
  • 1
    10x easier than the accepted answer Dec 7, 2019 at 18:56
  • 2
    Yes, but as mentioned—does not work for images or binaries.
    – luckman212
    Feb 19, 2021 at 19:24
12

The function below only works with a single file and not with multiple files. It requires GNU readlink which you can install by running brew install findutils.

copy(){ osascript -e{'on run{a}','set the clipboard to posix file a',end} "$(greadlink -f -- "$1")";}

Edit: I removed the tell app "Finder" block.

Edit 2: Reverted back to a version which supports relative paths.

1
  • 2
    This one actually works!
    – Bemmu
    Mar 3, 2016 at 0:36
4

To copy a file to a destination, use the cp command.

cp /path/to/file /path/to/destination

See man cp for more information.

2
  • 7
    This is not an answer to the question asked which references the clipboard as the target location.
    – kellyfj
    Oct 26, 2016 at 19:03
  • @kellyfj I disagree. The subject and the question don't match. It's a terribly worded question. This answers the text in the question body. Quite frankly I can't believe that it got so many upvotes, as far as I'm concerned it should have been closed with "Needs detail or clarity".
    – tink
    Dec 14, 2021 at 1:06
2

Here's a simple Shell/Bash script. Just paste it in your .bashrc file (or the like):

file-to-clipboard() {
    if [ -z "$1" ]; then
        echo "file path/name:"
        read FILE
    else
        FILE="$1"
    fi
    pbcopy < "$FILE"
}
2

Thanks to @Lri and @Chris Johnsen. Another option for you:

#!/usr/bin/osascript
on run args
  set abs_path to do shell script "/usr/local/bin/greadlink -f -- " & (first item of args)
  set the clipboard to POSIX file abs_path
end

You can put this into a script, and place that script in any folder in your PATH. This combined the idea of using osascript in hashbang and using readlink for relative path. This would achieve the same as @Lri's bash function, but has the advantage of not adding anything to your bashrc. You might need to install readline. You can do so with homebrew

brew install readlink
0

With support of relative file:

clip_img() {
    osascript -e 'on run argv' \
        -e 'set currentDir to do shell script "pwd"' \
        -e 'set the clipboard to (read POSIX file (POSIX path of (currentDir as text & (first item of argv) )) as JPEG picture)' \
        -e 'end run' "/$1"
    # osascript -e 'clipboard info'
}

run clip_img path-to-file to copy image to clipboard, and path-to-file could be relative. The file could be jpeg or png files.

0

I'm only 11 years late but I think I have an alternative, albeit, hacky solution. Here's my script:

#!/usr/bin/env bash

fzf -m -e | xargs -n1 open -R

osascript -e 'tell application "System Events" to keystroke "c" using command down'

osascript -e 'tell application "System Events" to keystroke "w" using command down'

open -a "Slack.app"

osascript -e 'tell application "System Events" to keystroke "v" using command down'

osascript -e 'tell application "System Events" to key code 76'

exit 0

A few disclaimers:

  • This assumes a macOS environment.
  • In the script, I'm using a command line tool called fzf which is not needed. It just makes searching for things easier. You can get it from brew install fzf if you're interested.
  • My use case was to find a way to search, find and send gifs quickly in Slack. So I named it 'fzygif'.

fzygif.png (The following description references the line numbers in the image above:)

  • Line no. 3 - fzf -m -e is to allow for multiple files to be selected as well as to search using the exact term used. The result is then piped into xargs -n1 open -R which then applies the open command to each search result, one at a time.

  • Line no. 5 - Next I use the osascript -e command to simulate Cmd+C which is the Apple keyboard shortcut for Copy.

  • Line no. 7 - Next I use the osascript -e command to simulate Cmd+W which is the Apple keyboard shortcut for Close Window (just doing a little housekeeping along the way; this could be omitted).

  • Line no. 9 - Next I open Slack (the intended target for my recently copied gif; open whatever app tickles you).

  • Line no. 11 - Next I use the osascript -e command to simulate Cmd+V which is the Apple keyboard shortcut for Paste.

  • Line no. 13 - Lastly, I use the osascript -e command to simulate "key code 76" which is the Apple key code for Enter. This is to automatically send the gif in Slack after it's pasted (this too could be omitted).

Since a gif is worth a thousand words? Here it is in action:

copy-gif-to-clipboard.gif

Happy hacking!

-2

You can use cat 'originfile' > 'destinationfile' on unix environments;

copy originfile destinationfile on windows.

1
  • 2
    How does this place the file on the clipboard?
    – grg
    Jan 6, 2018 at 22:27

You must log in to answer this question.

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