Linux Terminal Tutorial - Navigating a Linux File System
Introduction
This section will demonstrate the commands you can use to navigate a Linux file system using the Terminal. For a primer to some of the shorthand notation that makes life easier when navigating the Linux file system, see this section
Establishing your current directory location with pwd
To print your current working directory by using the following command:
$ pwd
This should print the current directory path, which is your home directory (You are placed within your home directory upon logging in).
Change your current working directory using cd
Change your current working directory (which is currently your home directory) to the directory Tutorial:
$ cd Tutorial
You may use pwd to confirm that you have successfully changed your directory.
Tip
If you use cd
without specifying a directory, you will be placed in your home directory
View the contents of the current directory using ls
Use ls to view the contents of the Tutorial directory:
$ ls #This is equivalent to using ls .
You should see three main directories listed:
- md_expr_1
- npt_expr_1
- nvt_expr_1
List the contents of a directory
List the contents of the nvt_expr_1 directory:
$ ls nvt_expr_1
A long list of files should be listed.
List the contents of a directory which includes hidden files by using the -a option
File and directory names that start with a period (.) are hidden and omitted by default when using ls. To show hidden files and directories, use the -a option:
$ ls -a nvt_expr_1
You should now see the .this_is_a_hidden_file hidden file (Hidden files in Linux start with a period)
Get a more detailed file list by using the -l option
To get a file list with additional information on the files ( such as owner information and permissions), issue ls with the -l option:
$ ls -l nvt_expr_1
Get a detailed file list including the size of files by using the -lh option (File size is indicated in column 5)
Sometimes it is necessary to get the size of files in a directory. Using the -h together with the -l option for ls will include the human readable size of all the files in the list:
$ ls -lh nvt_expr_1
This is equivalent to:
$ ls -l -h nvt_expr_1
Combine multiple options to get a detailed file list which includes file size and hidden files
$ ls -lha nvt_expr_1
Show the list in reversed order using the -r order
#First do a normal *ls* to be able to compare
$ ls nvt_expr_1
#Now do the reverse order *ls*
$ ls -r nvt_expr_1
Get a file list, including files in the sub-directories, recursively
It may be necessary to view the contents of directories and their sub-subdirectories. Using the -R option will systematically go into each sub-directory in the supplied directory and print out a list of files in each:
$ ls -R .
Get a file list, ordered by modification date
Sometimes it can be useful to see a list of files and directories ordered according to when they were modified. In the example below, the newest file/directory will be at the bottom:
#First touch nvt_expr_1 so it is the latest modified directory
$ touch nvt_expr_1
#Now print the list
$ ls -lrt
Find Files and Directories using find
Find all trajectory files (.trr) in the Tutorial directory (if you are in the Tutorial directory, cd up one level by using ..):
$ cd ..
$ find Tutorial -name "*.trr"
This should print the paths of the three trajectory files in the Tutorial directory.