Skip to content

Learning the Terminal on the Mac - Part 3 - Basic File Operations

By now you hopefully have a good understanding about how to reference files with paths and list files using ls, and move around with cd. Now we’re going to go over basic file operations including:

  • Create
  • Copy
  • Move
  • Delete

There is a lot of information to cover this time around, but I want to make one quick note about the notation being used before we begin. Like the man pages, we’ll use an underline to denote a placeholder for an option or argument to a command to describe its general form. For example, the general way to describe how to change to a directory is:

cd path

Here path is just a placeholder. You would replace it with a path to an actual directory.

Creating Files and Directories

There are actually many, many ways to create files on your Mac using commands in the shell, but I’m going to demonstrate a few quick ways so you can easily experiment with copy, move, and delete operations.

touch

The touch (man page) is a simple command that “touches” a file, meaning it updates its last modification date to the current time.

touch file

A nifty feature of touch is that it will create the file it it doesn’t exist. This command is the easiest way to create a new file to play with. For example to create a file called “Bananas!”:

touch Bananas!

And the same command, in the terminal:

lt3-touch.png

echo

The simplest way to create a file that has some contents is with the echo (man page) command, and output redirection. We’ll cover output redirection in detail soon. For now just follow the form:

echo "some text" > filename

An example:

lt3-echo.png

We snuck in another command there too: cat (man page). cat is used for concatenating (hence its name) files, though it’s most often used for dumping the contents of a text file to the screen.

mkdir

Finally, directories can be created with mkdir (man page).

mkdir new directory

To make a directory called “Stuff” in my home directory:

mkdir ~/Stuff

A simple example, creating the same “Stuff” directory, but using a relative path:

lt3-mkdir1.png

Copying: cp

Copying files is done with the cp command (man page). This command takes at least two arguments; the first being the source and the second being the destination.

cp source destination

Copying a file from my Downloads folder to my Desktop looks like this:


computer:~ demo$ cd Downloads/

computer:~ demo$ cp file.dmg ~/Desktop/

A real example:

lt2-cp1.png

The cp command can also be used to copy several files to a single destination:

cp source 1 source 2 source n destination

For example:


computer:~ demo$ cd Downloads/

computer:Downloads demo$ cp file1.dmg file2.dmg ../Desktop/

Moving: mv

The mv command (man page) command is used for moving files to a different location, or renaming files. It’s usage is very similar to cp:

mv source destination

mv source 1 source 2 source n destination

For example:


computer:~ demo$ cd Downloads/

computer:Downloads demo$ mv file1.dmg file2.dmg ~/.Trash/

Here is a real example of moving a file from my Downloads folder to my Desktop, and then renaming that file:

lt2-mv1.png

Also, mv can be used for moving entire directories. Here’s an example that shows moving a folder called “Stuff” from the Desktop to the home directory:

lt2-mv3.png

Deleting: rm

Files and directories can be deleted (or removed) with the rm command (man page). A word of caution: rm will remove files immediately, skipping the Trash Can. Use it carefully.

rm file 1 file 2 file n

The following is an example using touch to create several files and rm to delete them:

lt3-rm1.png

Recursion

For more information see recursion.

Ok, we got that lame computer science joke out of the way. Recursion isn’t all fun and games, though. It is an option that is found in many shell commands, including some of the ones we just learned! In this context, recursion basically means to include all files including sub-directories, sub-sub-directories, etc. Both cp and rm can be instructed to act recursively with the -r option.

cp -r

With the -r option, cp can be used to copy a directory and all its contents to another location.

cp -r source dir destination

For example, if I wanted to copy my “Documents” folder to a “Backup” volume:

cp -r Documents /Volumes/Backup/

Note that there is no trailing / after the source directory, Documents. This is an important subtlety when using cp -r. If the trailing / is omitted, the directory and all its contents will be copied. However, if the trailing / is included, only the contents of the directory will be copied to the destination, not the directory itself. Here are two examples showing the differences between the two commands:

With a trailing /, only the contents will be copied:

lt3-cp-r2-annotated.png

And now without the trailing /, the entire directory will be copied:

lt3-cp-r3-annotated.png

rm -r

If you thought rm was dangerous before, you ain’t seen nuttin’ yet. Just like with cp, the -r option allows you to remove a directory and all its contents. It is typically paired with the force option, -f, which swallows certain errors and skips all prompts and warnings.

rm -rf stuff you never want to see again

Example:

lt3-rm-rf.png

Be especially careful with -f. There is no undo.

Wildcards

If you haven’t guessed already, bash supports basic wildcards for matching files and paths. The real term for this functionality is globbing, which we’ll cover in more detail in the future. For now, we’ll cover two special characters which you’re probably already familiar with from other pieces of software: * and ?.

Match many characters:*

The asterisk (*), or sometimes called “star”, is the standard “match everything” wildcard. For example, say you want to copy all DMGs from the Downloads directory to /Volumes/Backup, but not the other files.


computer:~ demo$ cd Downloads/

computer:Downloads demo$ cp *.dmg /Volumes/Backup

This would match any file (or directory) that ends with .dmg. However, some DMGs are also zipped and end with .gz or .zip. We can make sure we get all the DMGs but using two asterisks:


computer:Downloads demo$ cp *.dmg* /Volumes/Backup

Most commands that take a path as an argument, including mv and rm will also work with *. But again, be very careful with rm and *.

Here is how you could clean out your ~/Library/Caches folder by moving al its contents to the Trash. If you actually do this, you should reboot after the operation is complete.

lt3-mv-caches-to-trash.png

Match one character: ?

The ? wildcard isn’t used as often as * but it is still fairly common. It differs from * in that it only matches one character. It’s similar to the blank tile in Scrabble. If you many digital photos downloaded from your camera, and they all have names like IMG_0020.jpg and IMG_0164.jpg, you could move them all like this:


computer:New Photos demo$ mv IMG_????.jpg ~/Pictures

The ? only matches a single character, so to match a four-digit number, we need to place four ?’s in a row.

Files and Directories Revisited

You may have noticed that the same commands can be used to copy, move, and delete files and directories (though not create, sorry).

This is because UNIX-based operating systems treat directories as a special kind of file. This means that many of the commands used for manipulating files can also be used for manipulating directories. UNIX actually goes beyond just directories. It also handles things like devices and network sockets as files as well, but that is a bit beyond the scope of our current article.

Wrap-up

The information covered in these last three tutorials should give you a very powerful toolbox. You can now do many of the basic file management tasks you would normally do in the Finder in the shell instead. This article didn’t discuss much Mac-specific information, but the tentative topic for the next article is how to bring the Finder and the Terminal together.

As always, I appreciate your feedback about the current material and future directions. What would you like to see next?

2 Comments

  1. doug

    Hi,

    Great set of articles. Helped me learned alot in a few minutes. I had a question?

    I can’t seem to change directories when the directory name is made up of two word. Is there a way around this? (Ex want to cd into a directory called my pictures)?

    Doug

    Posted on 02-Aug-08 at 3:42 pm | Permalink
  2. Hey Doug,

    You need to quote or escape spaces in files or directories. For example:

    cd “my pictures”

    Escaping is discussed in some detail in article #2:

    http://mrox.net/blog/2008/07/08/learning-the-terminal-on-the-mac-part-2-the-shell-and-paths/#escaping

    Since I posted these articles as blog entries, they actually show up in reverse order. I’m looking to re-organize it soon to make it more clear.

    Posted on 02-Aug-08 at 4:12 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*