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 ?

  • 1
    Do you mean copy the file or copy the contents of the file so that the contents can be pasted? – Mark Jun 9 '11 at 22:51
up vote 201 down vote accepted

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

Open a terminal and do:

$ cat ~/Desktop/ded.html | pbcopy

Now that file is in the clipboard.

To put it somewhere else, that is, to paste it, do:

$ pbpaste > ~/Documents/ded.html

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

  • 3
    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. – Itai Ferber Jun 5 '11 at 11:51
  • 7
    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 '11 at 13:02
  • 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 '15 at 19:37
  • Thanks, any idea how I read it and pass that to the next command? e.g. cat ~/.emulator_console_auth_token | auth <read_content>? – Dominic Jul 27 '17 at 14:20

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.

  • @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. – Chris Johnsen Jun 8 '11 at 23:10
  • 1
    Would it work for multiple files? – VitalyB Mar 2 '16 at 23:25

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.

  • You're right about images, etc. I didn't think of that. I guess I've misunderstood the question, because although pbcopy and pbpaste will work with an .html file, that's only because it just happens to be a text file. – boehj Jun 5 '11 at 12:18
  • No, you understood the question correctly, it's just that you assumed these tools worked as they should, which they do not. They're buggy, and aren't even meant to work that way, though Apple has done a really poor job on documentation. If you type in pbcopy -help, you get a non-production-worthy NSLog() line just repeating 'pbcopy -help'. It helps to be a programmer to understand this, which is why I felt it necessary to chime in. – Itai Ferber Jun 5 '11 at 12:30
  • 1
    Yep, no problem. ;) – Itai Ferber Jun 5 '11 at 12:38
  • 1
    Anyone who wants pbcopy to get some attention might get a free developer account and file some bugs - bugreport.apple.com - no guarantee it will help, but it gives the engineers leverage to get time to work on these things if enough people still care. – bmike Jun 5 '11 at 14:33
  • 1
    Can the OP achieve what they want with AppleScript on the CLI? – user588 Jun 5 '11 at 21:16

Edit: this script now uses a run handler and doesn't rely on Finder. I also added a hack to make it work with relative paths.

copy() {
    osascript - "$@" <<'END' 2> /dev/null
on run {a}
set the clipboard to POSIX file (POSIX path of ((POSIX file a) as alias))
end
END
}

I don't know any way to copy multiple files though.

When a relative path is converted to an alias, there is a warning like CFURLGetFSRef was passed this URL which has no scheme in 10.8. It can be just ignored, so the script redirects STDERR.

  • 2
    This one actually works! – Bemmu Mar 3 '16 at 0:36
  • Yes, indeed. Thanks a lot, mate! – Keeprock Aug 26 at 9:01

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

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

See man cp for more information.

  • This is not an answer to the question asked which references the clipboard as the target location. – kellyfj Oct 26 '16 at 19:03

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

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"
}

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

copy originfile destinationfile on windows.

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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