Navigate directories

Changing directory and listing the contents of directories.

The working directory is the directory where the Bash session is currently located, and relative to which commands typed in a Terminal are executed.

The pwd command prints the current working directory.

pwd

Displaying the working directory.

When you log into the CCB cluster, the working directory is initially set to the user’s home directory.

List directory contents

Listing the contents of directories is essential to identify files that are available for use, as well as directories that can be navigated into.

The ls command prints the list of files in a given directory.

Examples

Alone, the ls command prints the list of files and directories in the working directory.

ls

Listing file and directories in the current directory.

Given the path to an existing directory, the ls command prints the list of files in that particular directory.

ls /       # absolute path (starts with '/')
ls ~/.ssh  # relative path  (does not start with '/')

The ls command also accepts a number of options. Most commonly, the -l option is used to display detailed information about each file, including permissions, file size, and the timestamp of the latest update to each file.

ls -l

Listing file and directories with detailed information.

Often, the -h option is added to display file sizes in human-readable format, adding units (e.g., K - kilobyte, M - megabyte, G - gigabyte). Multiple options can be combined under the same - symbol. The two forms below are equivalent.

ls -lh
ls -l -h

Listing file and directories with file size in human-readable units.

The -t option can be used in combination with the -l option to sort files by modification time (newest first).

ls -lt

Listing file and directories from the most recently edited to the least recently edited.

The -a option can be used to reveal and include hidden files and directories.

ls -a

Listing hidden files and directories.

Finally – to clarify – options and paths can be combined, to list the contents of a particular directory with specific options.

ls -ltah ~/.ssh

Listing the contents of a specific directory using a number of options.

Cheatsheet

Common options for the ls command are listed below, in alphabetical order of the option flag.

OptionLong optionDescrition
-a--allDisplay all files including hidden files.
-d--directoryDisplay information about a directory instead of listing the contents of that directory. Usually combined with option -l.
-F--classifyAppend an indicator to the end of each listed name (e.g., / for a directory)
-h--human-readableCombine with option -l to display file sizes in human readable format rather than bytes.
-lDisplay information in long format.
-r--reverseDisplay results in reverse order. See also options -S and -t
-sSort by file size.
-tSort by latest modification time.

Change directory

Changing the working directory is often essential to run commands in the appropriate working directory.

Given the path to an existing directory, the cd command changes the working directory to that directory.

cd /       # absolute path (starts with '/')
cd ~/.ssh  # relative path  (does not start with '/')

Changing directory using a relative path.

Alone, the cd command changes the working directory to the user’s home directory.

cd

Changing directory to the user home directory.

The .. shorthand refers to the parent directory of each directory on the system. This can be used to move out of a directory. The shorthand can be combined multiple times in the same path to move out multiple levels at once. Some examples are given below.

cd ..
cd ../..
cd ../../another_directory

Final advice

Use Autocompletion!

Briefly, the tabulation key (TAB) may be pressed after typing the first few characters of the name of a valid directory, allowing the Bash session to predict and automatically complete the name of the directory.

Autocompletion is automatically available for file paths on the CCB cluster, saves a lot of manual typing, and avoid typographical errors that often go unnoticed and raise errors when executed.

This process can be repeated multiple times within the same command, even within the same path: type a few characters, press TAB, press type a few more characters, press TAB, etc.

There are two main reasons why nothing might happen when you press TAB while autocompleting the path to a directory or file:

  1. The path that you have typed so far does not exist.
  2. The path that you have typed so far is ambiguous.

The easiest way to diagnose which scneario you are in is to press TAB a second time.

If a number of options appear, matching the characters that you typed so far, it means that the path that you have typed so far is ambiguous. You need to type more characters manually to disambiguate the path before attempting to use autocompletion again.

Using autocompletion to predict directory names.

If nothing appears after having pressed TAB twice, then you likely are in the first scenario: the path that you have typed so far does not exist. You might want to proofread what you typed so far, but the fastest way to fix the path is to type it all from scratch again (using autocompletion to avoid typographical errors).