pwd
The pwd command prints the working directory to the screen. In other words, it returns the directory that you’re currently in.
whoami
The whoami command returns the username of the current user.
The ls command displays the names of the files and directories in the current working directory.
-a option lists all files including hidden files starting with '.'
-l option lists files with long format including file/directory permissions
-la option lists files with long format including file/directory permissions and hidden files
The cd command navigates between directories.
The cat command displays the content of a file.
The head command displays just the beginning of a file, by default 10 lines. The head command can be useful when you want to know the basic contents of a file but don’t need the full contents. Entering head updates.txt returns only the first 10 lines of the updates.txt file.
Pro Tip: If you want to change the number of lines returned by head, you can specify the number of lines by including -n
The tail command does the opposite of head. This command can be used to display just the end of a file, by default 10 lines. Entering tail updates.txt returns only the last 10 lines of the updates.txt file.
Pro Tip: You can use tail to read the most recent information in a log file.
The less command returns the content of a file one page at a time. For example, entering less updates.txt changes the terminal window to display the contents of updates.txt one page at a time. This allows you to easily move forward and backward through the content.
Once you’ve accessed your content with the less command, you can use several keyboard controls to move through the file:
Space bar: Move forward one page
b: Move back one page
Down arrow: Move forward one line
Up arrow: Move back one line
q: Quit and return to the previous terminal window
The grep command can be used to search a specified file and returns all lines in the file containing a specified string. In this search, the grep command takes two arguments: a specific string to search for and a specific file to search through.
grep "fruit" fruits.txt
Below is the list of options and flags that can be used with the grep command.
-r: search recursively
-w: match the whole word
-n: only in line number
-e: match pattern
--include and --exclude: include and exclude files in the search
--include-dir and --exclude-dir: include or exclude directories in the search
In addition, the grep command can be used to find files that contain a specific string. For example, the below command searches the files that have the word "vacation" in the directory /home/user/Downloads.
grep -rw /home/user/Downloads -e "vacation"
The pipe command is accessed using the pipe character (|). Piping sends the standard output of one command as standard input to another command for further processing. For example, the following command can be used to list through a directory called /home/ben/Documents and search for the word "important" in the filenames in that directory.
ls /home/ben/Documents | grep important
The find command searches for directories and files that meet specified criteria. There’s a wide range of criteria that can be specified with find. For example, you can search for files and directories that
Contain a specific string in the name,
Are a certain file size, or
Were last modified within a certain time frame.
The mkdir command creates a new directory.
mkdir /home/user/Pictures/DirectoryName
The rmdir command removes, or deletes, an empty directory.
rmdir /home/user/Pictures/DirectoryName
The touch command creates a new file.
touch file_name.txt
The rm command removes or deletes a file. Also, the rm command removes or deletes a directory with its subdirectories and files when it is used with the -r flag.
rm -r /home/user/Pictures/
The mv command moves a file or directory to a new location.
mv /home/user/Movies/movies.txt /home/user/Pictures
The cp command copies a file or directory into a new location.
If we want to copy a directory with its files, we have to recursively copy over the directory to get all the contents.
The flag for recursive copy is -r.
cp -r /home/user/source_file /home/user/duplicates/target_file
Nano is a command-line file editor that is available by default in many Linux distributions.
nano fileName.txt
echo test1 > file_name.txt
The >> operator is used to append text to an existing file using two subsequent greater-than signs.
echo text >> file_name.txt
This redirecting of input is done using the "<" (less-than symbol) operator. For example, the below command uses input redirection with the cat command to display the content of a file.
cat < file_name.txt
The below command is used to redirect the errors to a file other than the screen.
ls /dir/fake_directory 2> error_output.txt
sudo dpkg -i atom-amd64.dep
The dpkg command is used with the r or remove flag to remove a package.
sudo dpkg -r atom
The dpkg command is also used with the l flag to search installed Debian packages.
dpkg -l | grep atom
The passwd command is used to change and manage passwords of users.
The history command shows the previously executed commands.
The clear command clears the terminal.