Why bother with the command line?
- You can copy or move files from one location to another without opening windows in the Finder.
- You can copy or move files that are hidden in the Finder. These files, which can contain settings for certain apps or parts of the Mac, contain a dot (.) before their names, and the Finder doesn’t show them.
- You can copy or move multiple files using wildcards.
- You can rename a file quickly.
- If you've lost access to the Finder because your Mac is on the blink, you might be able to use the command line to troubleshoot the problem.
The difference between copying and moving files
mv
and cp
. The first does the same as dragging a file to a new location on the same hard disk; the second does what an Option-drag does, or what happens when you drag a file to a different disk or volume.How to copy files
cp
command is simple. First, launch Terminal (in your /Applications/Utilities folder). Then, use the following syntax to create your command:cp source destination
cp ~/Desktop/MyFile.rtf ~/Documents
cp command
: the -R
or recursive flag. When you use options with commands, this additional letter—always preceded by a hyphen (-)—tells the command to do something a bit differently. The recursive option tells the cp
command to copy every item in the folder: every sub-folder, every file and folder in every sub-folder, and so one, all the way down, to the new location. So you can copy a directory from your Desktop to your Documents folder like this:cp -R ~/Desktop/MyFolder /Documents
How to move files
mv
command works in the same way. But there are two ways you can use the mv
command. The first moves a file to a different disk or volume; remember, just as in the Finder, copying a file to a different volume won’t delete the original, whereas moving will. So you could issue this command to move a file from your Desktop to a folder on a backup disk:mv
command. The syntax is the same, and you don’t need the -R flag as you do with the cp command.:mv ~/Desktop/MyFolder /Volumes/Backup
How to copy or move multiple files
cp ~/Desktop/*.rtf ~/Documents
mv
command to move multiple files.How to rename files
mv
command also lets you quickly rename files. What you do is essentially move a file to the same location, but change its name. If you specify a name for its destination, the mv
command changes the file’s name when it moves the file. You can change a file name like this:![Working Working](/uploads/1/2/6/0/126019639/769718234.jpg)
mv ~/Desktop/MyFile.rtf ~/Desktop/MyFile-old.rtf
cp
and change its name. In this case, you need to specify not just a destination directory, but also a name for the file:cp ~/Desktop/MyFile.rtf ~/Documents/MyFile1.rtf
How to create directories (a.k.a. folders)
mkdir
, the make directory command. This is very useful when you need to make a bunch of folders in one fell swoop, say for a new project you’re starting. First use the cd
(change directories) command to move into the directory where want to create a new directory. Once you’re there, run this command:mkdir MyDirectory
mkdir MyDirectory1 MyDirectory2 MyDirectory3
mv
, cp
, and mkdir
—you’ll be able to copy and move files, as well as create directories to hold files anywhere in your Mac’s file system. As you become proficient with these commands, you’ll see how easy they are to use.I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes, I’m a tech writer. What I’m wondering is if there’s any way to actually copy and past into the Mac system wide copy/paste buffer directly from the command line?
Now this is the kind of question I really dig. And what’s great is that it turns out that yes, there is a very simple pair of utilities in Mac OS X that let you monkey with the copy / paste buffer (it’s actually called the “Clipboard”, though for some reason in the command line version they refer to it as the “pasteboard”, which isn’t something I’ve ever seen before in the Unix/Linux world).
I don’t know how this would work with multi-clipboard utilities like iClip since I don’t use them, but I imagine that it’d simply affect one buffer while leaving all the others alone. If someone has one of these utilities installed, it’d be 90 seconds to test it out…
Anyway, I started my quest with a very useful command: man -k paste, which revealed the following:
paste(1) – merge corresponding or subsequent lines of files
pbcopy(1), pbpaste(1) – provide copying and pasting to the pasteboard (the Clipboard) from command line
pboard(8) – pasteboard server
text(ntcl), tk_textCopy(ntcl), tk_textCut(ntcl), tk_textPaste(ntcl) – Create and manipulate text widgets
XStoreBytes(3), XStoreBuffer(3), XFetchBytes(3), XFetchBuffer(3), XRotateBuffers(3) – manipulate cut and paste buffers
If you want to work on the command line, ignore anything from section 3 of the manual: we’re looking for “(1)” here, and that shows the commands we seek: pbcopy and pbpaste.
They turn out to be really easy to work with. Want to push the output of an “ls -l” into the Clipboard? Here’s how:
There’s no output. So how do you know it worked? Let’s just open up a TextEdit window and use Edit –> Paste to see what happen:
Very cool.
To extract something from the Clipboard and have it output within the world of the command line, use pbpaste. Here’s a fun command pipe that counts how many letters are in the paste buffer:
252
Easy, eh? Let’s test it…
$ pbpaste | wc -c
32
$ pbpaste
taylor ttys000 Dec 28 17:24
$
So there you go. Turns out to be quite easy to copy and paste between Mac OS X GUI apps and the command line. Now, what to do with it… Hmmm….
Here’s one thing I did: I have a script that automatically resizes images for me, spitting out the appropriate HTML for image inclusion in blog posts (like this one). Until I wrote this article and learned more about pbcopy and pbpaste, I’d just manually copy and paste the resultant HTML code. Instead, here’s what I did to tweak my ‘while condition; do’ loop:
…lots of code…
done | tee $tempfile
cat $tempfile | pbcopy
rm -f $tempfile
See what’s happened here? By using the handy “tee” utility, I still see the output on the screen, but a copy is saved in the tempfile defined by the “tempfile” variable, then sent to the “pbcopy” utility and removed. The result: the output looks exactly the same, but it’s auto-copied into the Clipboard. Neat.