Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

This page covers the basics of navigating directories and manipulating files using the Unix command line. This includes terminals in any Linux or macOS environment, both of which are derived from Unix. It also includes Linux environments enabled within Windows via Windows Subsystem for Linux.

Manual ("man") pages

Most Unix commands have a manual explaining their function and how they should be run, as well as a list of all optional arguments. It is a good idea to read a command's manual before attempting to run it for the first time. To open the manual for a given command, use the man command, followed by the name of the command in question. For example:

Use the up and down arrow keys to scroll through the manual, and press the q key to quit.

Manuals are also hosted online on sites like die.net. You can search these sites for manuals if you would prefer to read them in a separate window from your terminal. Note that in some rare cases, different versions of a command will have slightly different syntax and options, and a manual viewed online may not correspond exactly to the version installed in your terminal.

Basic concepts and command syntax

Each Unix command has its own specific options, its own input requirements, and its own output. However, most common commands use the same basic syntax, which looks like this:

command -options input

"Command" refers to the specific process that you would like to run.

"Options" refers to any number of optional arguments that modify the default command, which are provided following a hyphen ( - ) and which can often be used in combination with one another.

"Input" refers to any input file, folder, or string which the command will process or act upon. Some commands require an explicit input, while others have a default value that will be used if no input is provided.

Unix is entirely case sensitive, meaning command names, options, and input files or folders must be capitalized correctly. Commands are generally called using all lower case letters, to make them easier to type and remember, and it is a good idea to make directory and filenames all lower case as well.

Navigating directories

When using the terminal, you navigate directories (i.e. folders) just like you would in Windows Explorer or macOS Finder. Just like Explorer and Finder, you will need to understand how directories and files are nested in order to find the files you want to work with. Navigating directories is all done using typed commands, rather than clicking on folder icons as in Explorer and Finder. Directories and files in a Unix file path are separated using forward slashes ( / ), unlike Windows, which uses backslashes ( \ ).

Identifying the current working directory (cwd)

Unlike Explorer or Finder, the terminal can only have one directory "open" at a time. This is called the current working directory, or cwd. The terminal always displays the current working directory between the active user's name (to the cwd's left, separated by a colon) and the cursor (to the cwd's right, separated by the $ symbol). Some terminals will display each element in a different color to make distinguishing them easier:

The pwd (print working directory) command can also be used to output the cwd.

Listing the contents of a directory (ls command)

Use the ls (list) command to output the contents of the cwd:

Add the -1 (hyphen followed by the number 1) option to the right of the command to display the output as one line per item:

Add the -l (lowercase L) option to display additional information about each item in the output, such as permissions and date of last modification:

If no explicit input is provided, the ls command will display the contents of the cwd. If a specific directory path is provided as input, the contents of that directory will be output instead.

The root and home directories

Unlike Windows, which separates files among different drives and assigns each drive a unique letter (such as C:), Unix has a unified file system, meaning every directory and file on every disk is nested below a single directory, which is called the root.  The root directory is represented by a single forward slash ( / ).

The root directory contains a number of system directories, one of which is the home directory. The home directory will contain subdirectories for each user on the system, and when a user launches the terminal the cwd will be set to their individual home directory by default.

Changing directories (cd command)

You can change the cwd to a new directory using the cd (change directory) command, followed by the name of the directory you would like to navigate to. Use cd and ls commands in conjunction with one another to list the contents of the cwd and navigate to subdirectories.

When typing the name of a file or directory in the terminal, you can use the Tab key to auto-complete the file or directory's name based on what you have already entered. For example, if the cwd contains a directory named "temp" and no other directories that start with "te", you can type "cd te" and hit the Tab key to auto-complete "cd temp". Since the terminal knows there is only one possible target beginning with "te" within the cwd, it can insert it for you. This is especially useful as a way of avoiding needing to type very long directory names. It also reduces opportunities for typos. If the Tab key is not auto-completing a directory or filename the way you expect, use the ls command to double-check that the file or directory you are targeting is spelled the way you think.

Remember that the terminal is case sensitive: if the directory is named "temp" (lowercase), the Tab key will not auto-fill it if you type "cd Te" (with a capital T)

The cwd's parent directory is represented using .. (two periods). To navigate "up" a directory, type "cwd ..":

You can always jump directly from the cwd to any directory on the system (not just subdirectories of the cwd) by typing cd followed by the target directory's full path, also called an absolute path. Because an absolute path always starts at the root directory, it will always begin with a forward slash:

Finding, copying, moving, and renaming files

Finding files with ls

In addition to listing the contents of a directory, the ls command can also be used to search for specific files or file types. To list all the files in a directory that begin with a given string of characters, type ls followed by the string and an asterisk ( * ). The asterisk is a "wildcard" symbol that the system reads as "any character or number of characters". For example, to list all the files in the cwd that begin with "cardenal", type "ls cardenal*":

You can also use the asterisk wildcard at the beginning of a string to list all files that have a particular file extension:

Finding files with find

The ls command will only search for files found in a single directory. To perform a search for files with a given name or format that crawls through subdirectories, use the find command.

Use the -name option followed by the search string (in quotes) to find all matching file or directory names:

The period at the beginning of each file path in the results represents the cwd.

You can use asterisk wildcard characters just like with the ls command:

Copying files (cp, scp, and rsync commands)

There are several different ways of copying files, depending on your needs. The most straightforward method is the cp (copy) command, which copies a source file to a target directory, using the following syntax:

cp /path/to/source/file.abc /path/to/target/directory/

By default, cp can copy individual files from a source location to a target directory, but it cannot copy an entire directory. To copy a directory, add the -r (recursive) option. The following command will move the entire "source" directory into "target":

cp -r /path/to/source /path/to/target/

To copy the contents of a source directory to a target directory (but not the source directory itself, add a slash to the end of the source directory path:

cp -r /path/to/source/ /path/to/target/

The scp (secure copy) command is basically identical to the cp command, but uses ssh encryption and is more suited to transfers between different machines. You should use scp rather than cp if you will be copying from one server or computer to another.

The rsync (recursive sync) command is similar to cp -r or scp -r. It is used to synchronize two copies of a directory that are stored in two separate locations, and can be used to copy an entire directory from one location to another. Like cp and scp, rsync requires a source and target:

rsync /path/to/source /path/to/target/

rsync is very flexible and has many options for how files are copied and how progress is displayed during the process. See the rsync manual for more information about these options.

Moving and renaming files (mv command)

Calculating directory size (du command)

Calculating disk usage (df command)

Searching within files (grep command)

Terminal multiplexer (tmux command)

Piping commands (stdin, stdout, and the tee command)

Looping through a file (while read method)

  • No labels