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 17 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, directory, 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 directories 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. The home directory is represented by a tilde (~).

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)

When you need to move rather than copy files from one location to another, use the mv (move) command. The command syntax is very similar to the cp command, in that you must provide a source file and target destination:

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

The mv command is also used to rename files. When used this way, the mv command "moves" a file from one path to another, even if that path is within the same directory. To rename a file, add the new name to the end of the target destination:

mv /path/to/source/file.abc /path/to/source/newFile.abc

You can combine these two uses of mv to move a file to a new directory and rename it at the same time:

mv /path/to/source/file.abc /path/to/target/directory/newFile.abc

Calculating file and directory size (du command)

The du (disk usage) command calculates the size of a file, directory, or set of directories. To calculate the size of an individual file, simply run du followed by the path to the file:

By default, du displays file size in bytes. Use the -h option to display the size in a more human-readable format:

Running du followed by the path to a directory will calculate the size of all subdirectories within that location:

Add the -a option to calculate the size of subdirectories and files within a given directory:

Use the -s option to calculate the total size of a directory and its contents:

Calculating free disk space (df command)

The df (disk free) command is used to calculate the amount of free space on a given volume. When run without any options or arguments, it will display the free space and disk usage of all mounted volumes, as well as the local disk:

This can be somewhat overwhelming. To display the free space and disk usage of one volume in particular, run df followed by the path to the volume's mount point. The mount point is listed in the final column, under "Mounted on" of the default df output. For example, if you have the dps volume mounted at /dps:

Like du, df displays disk usage in bytes by default. Use the -h option to produce a more human-readable display:

Searching within files (grep command)

The grep command is used to search for a specific string within a plain text or CSV file. grep has many options including case-insensitive search, inverse search, and regular expression search. See /wiki/spaces/utldigitalstewardship/pages/43057645 for a full guide to using grep.

Displaying file contents (cat, less, and head/tail commands)

There are several commands that can be used to display the contents of plain text or CSV files right in the terminal window. Each displays information in slightly different ways.

To output every line from a file, run the cat (concatenate) command, followed by the path to a text or CSV file:

If you want to preview the contents of a file without necessarily outputting every line to the terminal, run the less command, followed by the path to a file. This will fill your terminal window with the contents of the file, stopping once the window is full. You can then scroll down (and back up) through the text file using the down and up arrow keys. Hit the q key to exit the less output screen.

To quickly output only the first lines in a file, run the head command, followed by the path to a file. By default, head will output the first 10 lines of the file, but you can also specify the number of lines to output using the -n option:

The tail command works just like head, but it displays the last lines of the file. Just like head, you can specify the number of lines to output using -n:

Terminal multiplexer (tmux command)

Any time you have to run a process that will take a very long time, it's a good idea to use the tmux (terminal multiplexer) command to avoid accidentally interrupting the process. Using tmux you can begin a process, "detach" from the session to close the terminal window without killing the process, and then "reattach" to the session later to review the results of your process. tmux also allows you to run several commands simultaneously, one per tmux session.

To launch a new tmux session, simply enter tmux. Whenever the active terminal is a tmux session, a green stripe will be visible along the bottom of the terminal window:

The session number is the value shown on the left side of the stripe. The first session launched will be called 0, and each subsequent session will be assigned the next largest number.

To detach from the session without interrupting an active process or erasing the terminal history, enter Ctrl+b, followed by d. This will return you to the main terminal window. To reattach to a previous tmux session, run tmux attach -t, followed by the session number (remember that the first session will be number 0, not 1). If there is only one session open, tmux attach will attach to it. If you aren't sure whether there are any tmux sessions active, run tmux ls:

To scroll up and down through the terminal output of a tmux session, enter Ctrl+b, followed by [ (left square bracket). This will enable the cursor and allow you to scroll using the arrow keys or page up and page down. Press q to return to the last line of the terminal.

To close a tmux session (and kill whatever processes are running within it!), attach to the session and enter Ctrl+d. This cannot be undone, so before doing this, be sure that your process has finished, and that you have saved whatever terminal output you need.

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

(Combine with head or tail)

Looping through a file (while read method)

  • No labels