Navigating the Linux Command Line Interface (CLI)
A quick crash course to using the Linux command line interface (CLI)
I will show you how to do the following:
- Navigate the file system
- Read files
- Manage the file system
- Filter content
- Get help in Linux
Navigating the file system
The following commands are used to navigate the Linux file system;
cd - change directory
cd reportsnavigates from the current working directory to its subdirectory reports
cd \home\cale\reportsnavigates to the reports directory; the full pathname is required when reports is not a subfirectory of the current working directory
cd ..Navigates to the directory that is one level above the current working directory
ls - displays the names of files and directories
ls \home\cale\reportsDisplays the names of the files and directories in the reports directory; providing an argument that specifies the path to a directory is necessary to display the contents of a directory other than the user’s current working directory.
ls -aDisplays hidden files when displaying the names of files and directories inside the current working directory.
ls -lDisplays permissions to files and directories in the current working directory; also displays other additional information such as owner name, group, file size and the time of last modification.
ls -ladisplays permissions to files and directories in the current working directoy; including hidden files. Also displays other additional information. This argument is a combination of both -a and -l and combines the output of both.
pwd - prints the working directory to the screen
pwdPrints the current working directory to screen, such as \home\cale
whoami - returns the username of the current user
whoamireturns the current username, such as cale or admin.
Reading files
The following details commands that are helpful when reading files
cat - displays the contents of a file
cat updates.txtdisplays the content of the updates.txt file
head - displays the first 10 lines of a file
head updates.txtdisplays the first 10 lines of the updates.txt file
head -n 5 updates.txtdisplays only the first 5 lines of the file. the -n option allows the user to specify the number of lines to return.
less - returns the content of a file one page at a time
less updates.txtreturns the content of updates one page at a time; the less command changes the terminal window to a display which allows users to easily move forward and backward through content.
tail - displays the last 10 lines of a file
tail updates.txtdisplays the last 10 lines of the updates.txt file
tail -n 5 updates.txtreturns the last 5 lines of updates.txt. The -n option allows the user to specify the number of lines to return.
Manage the file system
The following are useful when managing the file system.
cp - copies a file or directory into a new location; the file will not be removed from the previous location
cp permissions.txt /home/cale/logscopies the permissions.txt from the users current working directory to the logs directory.
mkdir - make directory
mkdir networkcreates a new directory named network in the users current working directory.
mkdir /home/analyst/logs/networkcreates a new directory named network in the logs directory. The full path is required when the target directory is not a subdirectory of the current directory.
mv - moves a file or directory to a new location; the file is also removed from the previous location.
mv permissions.txt /home/cale/logsmoves the permissions.txt file from the users current working directory to the logs directory.
mv permissions.txt perm.txtmoves the permissions.txt file from the users current working directory to the new file name perm.txt in the users current working directory; this results in renaming the permissions.txt file as perm.txt.
nano - opens or creates a file in the nano CLI file editor.
nano permissions.txtOpens an existing permissions.txt file in the nano file editor, or creates permissions.txt in the nano file editor if it doesn’t already exist in the current working directory.
rm - remove a file
rm permissions.txtremoves the permissions.txt file from the users current working directory
rm home/cale/reports/permissions.txtremoves the permissions.txt file from the reports directory; the fill path is required is the users current working directory is not reports.
rmdir - remove a directory
rmdir networkremoves the empty network subdirectory of the users current working directory from the file system.
rm /home/cale/logs/networkremoves the empty network directory from the file system; the full path is required when network is not a subdirectory of the current directory.
touch - creates a new file
touch permissions.txtcreates a new file named permissions.txt in the users current working directory
touch /home/cale/reports/permissions.txtcreates a new file named permissions in the reports directory; the full path is required if the user wants to create permissions.txt in any directory other than the current working directory.
Filter content
These commands are useful for filtering content.
find - searches for directories and files that meet the specified criteria
find \home\cale\projectssearches for all files starting at the projects directory
find \home\cale\projects -iname "*log*"searches for all files in the projects directory that contain the work log in the filename. -iname searches for a specified string and is not case-sensitive; * is a wildcard and can represent zero or more unknown characters.
find \home\cale\projects -mtime -3Searches for all files in the projects directory that have been modified within the past 3 days. -mtime bases its search for files or directories that were modifed within the specified number of days.
find \home\cale\projects -nmin -15Searches for all files in the projects directory that have been modified within the past 15 minutes. -nmin bases its search for files or directories that were modified within the specified number of minutes.
grep - searches within a specified file and returns all lines containing a specified string.
grep OS updates.txtsearches the updates.txt file and returns all lines containing the string OS
| (piping) - sends the output of one command as the input of another command for further processing.
ls \home\analyst\reports | grep usersredirects the standard output of ls \home\cale\reports to be standard input for the grep users command, meaning the grep users identifies files and subdirectories in the \home\cale\reports directory that contain the string users within their filename.
There is a lot that you can do with piping, It is crucial to understand the basic concept as we will be using this later.
Get help in Linux
The following commands are useful when getting help in Linux.
apropos - searches the manual page descriptions for a specified string
apropos passwordreturns the manual pages of commands that contain the keyword password
apropos -a graph editorreturns the manual pages of commands that contain both keywords graph and editor. -a species to return commands that only contain all specified strings.
man - displays a manual page for the specified command
man chowndisplays detailed information about chown and how it works.
whatis - displays a description of a command on a single line
whatis nanoDisplays a description of nano on a single line.