Linux 101 - Navigating a Linux file system within the Terminal

Introduction

The first skill users should develop when working in a Linux terminal is to be able to navigate within the Linux file system. As explained in the User Interfaces section, the user interacts with the computer via text command, thus every navigation action will be coupled to a text command.

In this section the user will be introduced to and provided with examples of all the commands necessary to navigate a Linux file system within the terminal.

Before starting, please note the following:

  • It is recommended that users follow along with the examples by using their own open linux terminal. Note that logging into the UFS HPC is not required, as the examples are general in nature and can be followed within any linux terminal.

  • As a reminder, each command is a program on each own. Thus command and program may be used interchangeably (see the below note).

  • The listed commands have many additional options and thus only the most general options and use cases will be illustrated. Users are encouraged to either use the man pages for the programs highlighted or to consult online resources if they wish to explore the command options further.

The user will be exposed to specific terminology when using the commands associated with navigation within the command-line. The table below describes commonly used terms:

Term Definition Alias
Working Directory The directory in the file system that is attached to a running process. Thus if a process would produce an output file foo.txt and is executed from the directory /home/someuser/foo/, the path in the output file will be: /home/someuser/foo/foo.txt Present Working Directory / Current Working Directory

Some shorthand can be used when working with paths that will make navigation in the terminal more efficient. The table below lists these useful shorthands:

Shorthand Description
/ The root directory
. Present Working Directory
.. Directory above the Present Working Directory in the directory tree
~ The home directory of the current user. For example: The path for user "lategandupreezl" will be /home/lategandupreezl/
- Previous Working Directory (only used with cd)

Establish your current directory location with pwd

When first logging into the UFS HPC or simply opening a terminal, you will be placed within a directory. Usually this directory is your home directory (remember, the shorthand for the home directory is ~). However, this might not always be the case, or if you have been working in the terminal for quite a while you may wish to determine your current location.The command pwd used to do this. Entering the command as is should print out the full path of your present working directory.

Common Use

Basic Syntax

$ pwd

Example

Print the current working directory:

nav_linux_1

List files and directories using ls

To list the files and directories in a directory, the command ls can be used. Note the directory contents will be differentially coloured according to type (file, directory, executable file). This colour scheme is highly variable and can be changed, thus it is only mentioned here.

Many options exist to control the output ls, so only the most commonly used options are shown here.

Common Use

Basic Syntax

$ ls *directory or file path*

Example

Print the contents of the user's home directory:

nav_linux_2
blue = directories, green = executable files, black = normal files, red = rpm package

Show a full listing

Basic Syntax

$ ls -l *directory or file path*

Example

Print a detailed list of the user's Home directory:

nav_linux_3

Show a long listing in human readable file size format (file size is in column 5)

Basic Syntax

$ ls -lh *directory or file path*

Example

Print a detailed list of the user's Home directory and show the file sizes in a human readable format:

nav_linux_4

Pro Tip: To find the file size of a specific file, do an ls -lh and pass the path of the desired file to the command.

Show all files, including hidden files (depicted in Linux by the file name starting with a period)

Basic Syntax

$ ls -a ~

Example

Print a list of the user's Home directory, including hidden files:

nav_linux_5

Show results in a reversed order

Basic Syntax

$ ls -r *directory or file path*

Example

Print a list of the user's Home directory in reverse order:

nav_linux_6

Show sub-directories recursively

Basic Syntax

$ ls -R *directory or file path*

Using this option the ls program will descend into each directory under directory or file path and list out the contents of each sub-directory in a recursive fashion.

Example

Print a recursive list of all sub-directories under the directory testing:

nav_linux_7

In the example the directory names have been highlighted by red boxes. Pay close attention to the order of directories shown, as this indicates how the program is functioning (I.e. It will go into the first directory and continue down it until it has discovered each sub-directory in the starting directory and return to the beginning).

Moving between directories using cd

Moving between directories in the terminal can be achieved by using the command cd

Common Use

Basic Syntax

$ cd *directory path*

Example

Change the working directory from the user's home directory to the directory testing:

nav_linux_8

Pro Tip: Use the shorthand notation for common directories in combination with the cd command to easily navigate between common directories.

Find files and directories using find

Searching for file and directories within a file system is a basic function provided by any OS. This is no different for Linux in the terminal. The find command can be used to traverse a directory structure and search for a particular file or directory name. If files and directories are found matching the search criteria (this can be a specific file name or a regular expression), the file/directory path will be printed to the terminal.

The find command can be used with tools such as grep to combine file path searching and searching within the file itself. However, only the basic functionality of searching for a file or directory in a particular path will be showcased here.

Common Use

Basic Syntax

$ find *path_to_start_search* -*type_of_search* *search_expression*

Example

Search for all files staring with evol1 in the current directory :

search_1

Outcomes for this section

After completion of this section, you need to be able to do the following tasks in a Linux terminal:

  • Determine your present working directory
  • List the contents of any directory where you have read access
  • Move between directories where you have access