Linux | Shell Basics
Some of the most common Linux commands and their usages listed in the table below. Commands are sorted based on popularity whereas shortcuts are sorted alphabetically.
1. Basic Symbols
Symbol
Usage
(Slash) /
Used to denote following directories in the root tree
/parent/child
directs to child folder inside the parent folder/parent/child/file1.txt
directs to file1 text file inside the child folder, etc.
(Tilde) ~
Shortcut for the home directory - /home/username
~/Desktop
means/home/username/Desktop
(Dot) .
Stands for the current directory you are located in
Say you are in
dir1/dir2/dir3
directory.
representsdir1/dir2/dir3
(current dir)
(2 Dots) ..
Stands for the parent directory (one directory up in the tree)
..
representsdir1/dir2
directory (1 dir up)../..
represents/dir1
directory (2 dir up)
(Dash) -
Stands for the recent directory located (or flags see below)
-
represents/dir1
(the most recent directory)
(Backward Slash) \
Used to skip characters if the name includes a space
Either use
\
or put inside a string to denote a new including a blank in the path name/dir1/new\ file
or"/dir1/new file.txt"
is used to denote the "new file.txt" text file in the dir1 folder
(Asterisk) *
Used as a wildcard during searches - matches one of more occurrences of any character, including no character
ls -l a*
lists all files whose name is starting with als -l A*
lists different files as it is case sensitive
(Question Mark) ?
Used as a wildcard to represent an anonymous character
ls e?d
lists all 3-character files starts with e and dPossible results end, eid, etc.
(Brackets) [chars]
Used as a wildcard to represent a character between brackets
ls l[aeoi]st
lists all 4-character files starts with l ends with st if [aeoi] is one of the middle character.Possible results last, lost, list, etc.
<command> -options
Characters after a dash symbol following a command stands for options (flags) for that command to be executed
Example:
ls
lists files in the current folder whereasls -a
lists all - including hidden onesOptions (flags) for each command can be found in manual - use
man <command>
to see details
/home/username/.../file
Absolute path of a file from the root folder - always works!
../dir1/dir2/file.txt
Relative path to a file relative to the current directory
Say you are in
~/Desktop/dir1/dir2/file.txt
Absolute path of Desktop folder is:
/home/username/Desktop
or~/Desktop
Relative path of Desktop to dir2 folder is:
../.. (..
directs to~/Desktop/dir1)
2. Navigation Commands
Command
Usage
pwd
Prints working directory (absolute path)
cd <dir>
Changes directory to dir (folder)
cd .
moves to current dir (output ofpwd
)cd ..
moves 1 directory upcd ../..
moves 2 directory up, etc.cd -
prints and moves to the recent directorycd ~
moves back to home directory
ls <dir>
Lists all (excluding hidden) files in a given / current dir
Use
-a
flag to list hidden files (.hiddenfile
)Use
-l
to list files in the tabular (long) formatSome flags can be combined - Example:
ls -al
man <command>
Opens the user manual for the command
<command> --help
Opens the help page for the command
time <command>
Prints the time it takes to execute the command
history
Prints the last commands typed on the terminal
clear
Clears the terminal page (Ctrl+L)
cat
Concatenate - Multiple purpose
file <filename>
Displays the type of file given with filename
locate <file>
Locates (searches for) the file in the Linux system
shutdown <time>
Shuts down the system at a given time
shutdown -h now
- Shuts down the system immediatelyshutdown -r now
- Reboot the system immediately
reboot
Stops and restarts the system
reset
Resets the terminal screen
exit
Logs out of terminal (Ctrl+D)
zip
Zipping files and folder - (password protected)
zip -er namezipfile.zip filepath
3. Displaying System Information
Command
Usage
w
Displays the online user information
whoami
Displays the current username
date
Prints the full date with other details
(Default):
Day Month #Day hh:mm:ss TimeZone yyyy
Example:
Mon Jan 25 14:18:52 CST 2021
cal
Displays the monthly calendar
finger <user>
Displays user information
du
Shows directory space usage
df
Shows a report on the disk space usage
whereis <app>
Displays the location of the application
which <app>
Displays the default version of the app
4. Displaying File Contents
Command
Usage
echo <"string">
Prints the string
cat <file>
Prints the contents of the file
open <file>
Opens the file in the given path
less <file>
Displays the contents of the file
Can navigate forward and backward through file
more <file>
Displays the contents of the given file
Allows users to scroll up & down through pages
head # <text>
Displays the first #
lines of the text file
tail # <text>
Displays the last #
lines of the text file
grep <content> <text>
Searches (filters) for the content in the text file
sort <file>
Sorts the contents of the file
wc <file>
Displays the number of words (word count) in the file
5. Manipulating Files
Command
Usage
mkdir <name>
Makes directory with the given name
mv <file> <dir>
Moves the given file to the given directory
Can be used to rename files ass well
mv <file_name> <new_name>
rm <file>
Removes the given file - cannot be undone
Cannot remove directories unless used with
-r
optionrm -r <dir>
- Recursively deletes all files in dirrm -rf <dir>
- Does not ask for permit
rmdir <dir>
Removes the given directory if it is empty
touch <file1> <file2>
Creates a new file or files - file 1 (and file 2)
cp <file> <new_name>
Copies the given file with a given new name
Can copy directories recursively with
-r
optioncp -r <dir> <new_name>
cut <options> <file>
Cuts a specific part of the file
ln <source> <link>
Creates a physical / symbolic (-s
option) links between files
In order to remove the symbolic links use either:
unlink <link>
orrm <link>
file1 | file2
Redirects (STDOUT) output of file 1 to (STDIN) input of file 2
file1 > file2
Saves/redirects the (STDOUT) output of file 1 to file 2
Will overwrite the final output each time used
file1 >> file2
Appends (STDOUT) output of file 1 to file 2 - w/o overwriting
file1 2> file2
Redirects the (STDERR) error message of file 1 to file 2
command < file2
Passes the contents of the file 2 (STDIN) to a program
6. Terminal Shortcuts
Shortcut
Usage
CTRL + A
or HOME
Moves the cursor to the beginning of the line
CTRL + B
or ⇦
Moves the cursor back one character at a time
CTRL + C
Terminates the current running process
CTRL + D
Exits the bash shell - same with Exit
command
Also removes the character under the cursor while typing
CTRL + E
or END
Moves the cursor to the ending of the line
CTRL + F
or ⇨
Moves the cursor forward one character at a time
CTRL + K
Removes all the text from the cursor until the end of the line
CTRL + L
Clears the terminal (Moves the previous commands up)
CTRL + Q
Unfreezes the terminal (doesn't work in MacOS)
CTRL + S
Freezes the terminal (doesn't work in MacOS)
CTRL + X + Backspace
Removes all the text from the cursor to the beginning
Doesn't work in some Macs as there is no backspace
Option + Delete
in MacOS
CTRL + Z
Pauses the current running process
CTRL +
⇦ or ALT + B
Moves the cursor back one word at a time
CTRL +
⇦ or ALT + C
Moves the cursor forward one word at a time
TAB
Use TAB
to autocomplete the path/file names
⇧ | ⇩
Use arrows to display previously typed commands
References:
0) https://ubuntu.com/tutorials/command-line-for-beginners#1-overview
1) The Linux Command Line by William Shotts
2) https://www.pluralsight.com/guides/beginner-linux-navigation-manual
3) https://ryanstutorials.net/linuxtutorial/
4) https://maker.pro/linux/tutorial/basic-linux-commands-for-beginners
5) https://www.tecmint.com/linux-command-line-bash-shortcut-keys/
Last updated