The terminal in Linux is an interface in which you can type and execute text-based commands. It allows for efficient management of the operating system and software, providing a direct way to interact with the system's kernel and services. Unlike graphical user interfaces (GUIs), the terminal provides a lightweight, more controlled, and scriptable way of interacting with the computer.
Graphical Method: On most Linux desktop environments, you can open the terminal from the applications menu. Look for "Terminal".
Keyboard Shortcut: Many distributions allow you to open a terminal window by pressing Ctrl + Alt + T.
Prints the path of the current working directory.
Changes the working directory, abbreviation for 'change directory'.
cd <folder>: Go into a folder.cd: Go to home directory.cd ..: Go 1 directory up.cd ../..: Go 2 directories up.cd ../../etc: Go 2 directories up and go to theetcfolder.cd ~: Go to your home directory.
Creates a folder inside the working directory.
mkdir dir1: Create a folder nameddir1in the current directory.mkdir dir1 dir2 dir3: Create multiple folders in the current directory.mkdir ~/Desktop/dir1: Create a folder nameddir1on the existing desktop directory.mkdir -p dir1/dir2/dir3: Create parented directories, creating intermediate folders if they don't exist.
Lists files in the working directory.
ls: List files and folders in the current directory.ls -a: List all files, including hidden ones.ls -l: Detailed list view with file information such as permissions and size.ls -lh: List files in a human-readable format.ls dir1: List the contents of thedir1directory.
Creates a new empty file.
touch output.txt: Creates an empty file namedoutput.txt.
Writes output to files:
>: Writes the output of a command to a file, overwriting the file if it exists.pwd > output.txt: Writes the path of the working directory tooutput.txt.
>>: Appends the output of a command to a file.pwd >> output.txt: Appends the path of the working directory tooutput.txt.
Stands for Concatenate. Creates, views, and concatenates files:
cat output.txt: Displays the contents ofoutput.txt.cat file1.txt file2.txt > merged.txt: Concatenates two files intomerged.txt.
Moves or renames files and directories:
mv output.txt dir1/: Movesoutput.txtto thedir1directory.mv dir1/* .: Moves everything fromdir1to the current directory.mv oldname.txt newname.txt: Renames a file.
Copies files and directories:
cp output.txt output2.txt: Copiesoutput.txttooutput2.txt.cp -r dir1 dir2: Recursively copies thedir1directory todir2.
Command that permanently deletes files and directories. There is no "trash" or "recycle bin" by default. Once you delete something with rm, it's usually gone for good.
rm myfile.txt: Removes 1 file.rm file1.txt file2.txt file3.txt: Removes multiple files.rm *.txt: Removes files using wildcards.rm -d my_empty_directory/: Removes an empty directory.rm -r my_directory/: Removes a directory and its contents (recursively).rm -f file.txt: Force deletion.
Command-line interface for the "Trash" or "Recycle Bin" functionality that you typically find in graphical desktop environments (like GNOME). This utility moves files and directories to the Trash: Instead of permanently deleting them, you send them to a temporary holding area from which they can be recovered.
trash-put myfile.txt: Trash a single filetrash-put file1.txt file2.txt: Trash multiple filestrash-put my_directory/: Trash a directory (and its contents)trash-put *.txt: Trash all .txt files in the current directorytrash my_directory: Trash a directory
Prints the path of the current working directory: pwd
Changes the working directory:
cd: Go to the home directory.cd ..: Go one directory up.cd ../..: Go two directories up.cd ../../etc: Go two directories up and into theetcfolder.cd ~: Go to the user's home directory.cd -: Switch back to the previous working directory.
Creates a folder inside the working directory:
mkdir dir1: Create a folder nameddir1in the current directory.mkdir dir1 dir2 dir3: Create multiple folders in the current directory.mkdir ~/Desktop/dir1: Create a folder nameddir1on the desktop.mkdir -p dir1/dir2/dir3: Create parented directories, creating intermediate folders if they don't exist.
Lists files in the working directory:
ls: List files and folders in the current directory.ls -a: List all files, including hidden ones.ls -A: List all files, including hidden ones but without the current and parent directories (.and..)ls -l: Detailed list view with file information such as permissions and size.ls -lh: List files in a human-readable format.ls dir1: List the contents of thedir1directory.
Creates a new empty file:
touch output.txt: Creates an empty file namedoutput.txt.
Writes output to files:
>: Writes the output of a command to a file, overwriting the file if it exists.- Example:
pwd > output.txt: Writes the path of the working directory tooutput.txt.
- Example:
>>: Appends the output of a command to a file.- Example:
pwd >> output.txt: Appends the path of the working directory tooutput.txt.
- Example:
Creates, views, and concatenates files:
cat output.txt: Displays the contents ofoutput.txt.cat file1.txt file2.txt > merged.txt: Concatenates two files intomerged.txt.
Moves or renames files and directories:
mv output.txt dir1/: Movesoutput.txtto thedir1directory.mv dir1/* .: Moves everything fromdir1to the current directory.mv oldname.txt newname.txt: Renames a file.
Copies files and directories:
cp output.txt output2.txt: Copiesoutput.txttooutput2.txt.cp -r dir1 dir2: Recursively copies thedir1directory todir2.
Removes files and directories:
rm output.txt: Removesoutput.txt.rm -rf dir1: Recursively removesdir1and its contents.
Determines the file type of a given file:
file image.jpg: Displays the file type ofimage.jpg.
Displays detailed information about a file or file system:
stat filename.txt: Shows file details including inode, size, and permissions.
Creates links between files.
ln -s source.txt link.txt: Creates a symbolic link namedlink.txtpointing tosource.txt.ln source.txt link.txt: Creates a hard link namedlink.txttosource.txt.
Counts lines, words, and characters in a file:
wc filename.txt: Displays line, word, and character count for the file.wc -l filename.txt: Displays the line count only.ls -A | wc -l: Displays the file count in the current directory.
Stream editor used for filtering and transforming text:
sed 's/old/new/' file.txt: Replaces the first occurrence of "old" with "new" in each line.sed -i 's/old/new/g' file.txt: Replaces all occurrences of "old" with "new" in the file.
Read more about sed here.
A powerful text processing tool:
awk '{print $1}' file.txt: Prints the first column of a file.awk '/pattern/ {print $0}' file.txt: Prints lines matching a pattern.
Read more about awk here.
Cuts specific sections from a file:
cut -d',' -f2 file.csv: Extracts the second column from a CSV file using,as a delimiter.
Displays large files one page at a time:
less largefile.txt: Openslargefile.txtfor paginated viewing.
Displays the first few lines of a file:
head -n 5 file.txt: Displays the first five lines of the file.
Displays the last few lines of a file:
tail -n 5 file.txt: Displays the last five lines of the file.tail -f logfile.txt: Continuously displays new lines added to a file in real time.
Compares the contents of two files line by line:
diff file1.txt file2.txt: Shows the differences line by line.
Sorts the lines in a file:
sort file.txt: Alphabetically sorts the file.sort -n file.txt: Sorts the file numerically.sort -r file.txt: Sorts the file in reverse order.sort -k 2 file.txt: Sorts by the second column.
Opens and edits files:
nano output.txt: Opensoutput.txtfor editing.
Read more about nano here.
Creates, extracts, or manipulates archive files:
tar -cvf archive.tar file1 file2: Creates an archive containingfile1andfile2.tar -xvf archive.tar: Extracts the contents ofarchive.tar.tar -czvf archive.tar.gz dir/: Creates a compressed archive of thedirdirectory.
Compresses and extracts files:
zip myzip.zip file1 file2: Compressesfile1andfile2intomyzip.zip.unzip myzip.zip: Extracts the contents ofmyzip.zip.
Displays files in a tree structure:
tree: Displays the directory structure starting from the current directory.tree -L 1: Displays the directory structure up to a depth of 1.
Searches for files and directories based on various criteria:
find . -name "*.py": Finds all.pyfiles in the current directory and subdirectories.find . -iname "*.py": Performs a case-insensitive search for.pyfiles.find /home/user/projects -type d -name build: Finds all directories namedbuildin/home/user/projects.find /home/user -mtime -7: Finds files modified in the last 7 days in/home/user.find /var/log -size +100M: Finds files larger than 100MB in/var/log.find /home/user/images -name "*.jpg" -exec mv {} /home/user/backup \;: Finds all.jpgfiles in/home/user/imagesand moves them to/home/user/backup.find /path/to/dir -name "*.tmp" -exec rm {} \;: Finds all.tmpfiles and deletes them.
Stands for “global regular expression print”, a powerful command used for searching text using patterns.
apt list | grep firefox: Filters the list of packages for "firefox."grep "word" filename.txt: Searches for a specific word in a file.grep -r "word" /path/to/directory: Recursively searches for a word in all files in a directory.grep -A 3 "pattern" filename.txt: Displays 3 lines after each pattern match.grep -i "pattern" filename.txt: Performs a case-insensitive search.grep -c "pattern" filename.txt: Counts the number of lines matching the pattern.
Outputs the username of the current user.
Displays a string or text passed as an argument:
echo "Hello World": Displays "Hello World" in the terminal.
Clears the terminal screen, making it blank.
Shows the manual page for commands, providing detailed information about command usage and options.
man mkdir: Shows the manual for themkdircommand.
Provides a more detailed guide for commands:
info mkdir: Shows detailed information aboutmkdir.
Displays previously entered commands in the terminal:
history: Shows the full command history.history | tail -n 20: Displays the last 20 commands.history -c: Clears the command history.
Changes file permissions:
chmod 755 file.txt: Grants read, write, and execute permissions for the owner and read/execute for others.
Executes a command with superuser privileges:
sudo apt install gimp: Installs the GIMP software.
Changes file ownership:
chown user file.txt: Assigns ownership offile.txttouser.
Changes the group ownership of a file:
chgrp group file.txt: Changes the group offile.txttogroup.
Sets default file permissions:
umask 022: Ensures new files are created with644permissions.
Switches to another user:
su user: Switches to the specified user.
Adds a new user:
adduser username: Creates a new user account namedusername.
Deletes a user:
deluser username: Removes the userusername.
Displays information about logged-in users:
who: Lists all users currently logged in.
Shows the login history of users:
last: Displays a list of user logins.
An interactive process viewer, offering a detailed overview of system processes and the ability to manage them directly.
htop: Opens a visual overview of system processes.
Terminates processes by PID or name:
kill 533494: Terminates the process with PID533494.killall firefox: Terminates all processes namedfirefox.
Displays all running processes:
ps aux: Lists all running processes.ps aux | grep firefox: Filters the list of processes for "firefox".
Executes a command that continues running in the background, immune to hangup signals.
nohup python script.py &: Runs script.py in the background, ensuring it continues even if the terminal closes.
Suspends a foreground process, moving it to the background in a stopped state. This allows the terminal to return to the prompt for other tasks while keeping the process's state intact.
Lists active or suspended jobs:
bg %1: Resumes job 1 in the background.fg %1: Brings job 1 to the foreground.
Displays disk usage for files and directories:
du -h /path/to/directory: Displays human-readable sizes.du -sh /path/to/directory: Displays the size of the directory.du -a -h /path/to/directory: Displays sizes of all files and directories.du -h --max-depth=1 /path/to/directory: Limits depth to show directory sizes.
Displays available disk space:
df -h: Shows disk space in a human-readable format.df -m: Shows disk space in mega bytes.
Shows memory usage:
free -h: Displays memory usage in a human-readable format.
Displays real-time process activity.
Interactive tool to analyze disk usage.
- Clean up the APT cache:
- List disk usage:
sudo du -sh /var/cache/apt - Clean:
sudo apt clean
- List disk usage:
- Remove packages you no longer need:
sudo apt autoremove - Cleanup journal logs:
- List disk usage:
journalctl --disk-usage - Remove logs from x-time:
sudo journalctl --vacuum-time=7d
- List disk usage:
- Clear thumbnail cache:
- List disk usage:
du -sh ~/.cache/thumbnails - Clear:
rm -rf ~/.cache/thumbnails
- List disk usage:
- Clear temporary files:
- List disk usage:
sudo du -sh /tmp - Clear:
sudo rm -rf /tmp/*
- List disk usage:
Shows system uptime and load averages:
uptime: Displays the current uptime.uptime -s: Shows the last boot time.
Displays system information:
uname -a: Displays all system information.
Displays or sets the system date and time:
date: Shows the current date and time.
Manages system time, date, and time zones:
timedatectl: Displays current time settings.
Safely shuts down or reboots the system.
shutdown now(Shuts down the system immediately.)shutdown -r +10(Reboots the system after a 10-minute delay.)
Reboots the system immediately.
Controls the systemd system and service manager.
systemctl status: Shows the status of all active systemd units.systemctl status NetworkManager: Shows the status of theNetworkManagerservice.systemctl enable <service>: Enables a service to start automatically at boot.systemctl disable <service>: Disables a service from starting at boot.systemctl restart <service>: Restarts a service.
Displays environment variables in the current shell session:
env: Lists all environment variables.
Sets environment variables in the current shell session:
export VAR=value: Sets the value ofVAR.
Unsets environment variables in the current shell session:
unset VAR: RemovesVAR.
Prints the value of a specific environment variable:
printenv PATH: Displays thePATHvariable.
List all PCI devices
lspci: List all PCI devices briefly.lspci | grep -i ethernet: List the ethernet controller.lspci | grep -i network: List the network controller.
Displays detailed information about the CPU architecture.
Lists all USB devices connected to the system.
lsusb: List USB hubs and connected devices.lsusb -t: Show USB devices in a tree view, indicating hierarchy and speed.lsusb -v: Show verbose output for each USB device (may requiresudofor full details).
Lists block devices (hard drives, SSDs, USB drives, etc.) and their partitions.
lsblk: Shows devices, partitions, sizes, and mount points in a tree format.lsblk -f: Includes information about filesystem types, UUIDs, and labels.lsblk -m: Shows permissions and ownership information.
Lists detailed hardware configuration of the machine. Often requires sudo for full details.
lshw: Displays a comprehensive list of all detected hardware (can be very long).lshw -class network: Displays detailed information specifically about network interfaces.lshw -class disk: Displays detailed information about disk drives.lshw -class storage: Displays detailed information about storage controllers.lshw -class display: Displays detailed information about display adapters (graphics cards).lshw -class memory: Displays detailed information about memory (system RAM, caches).
Note
ifconfigis considered deprecated and replaced by theiptoolset.netstatis considered deprecated and replaced by thesstoolset.
List your IP addresses.
Ping to a device.
ping google.com(Pings google.com.)
Displays detailed information about network connections, listening ports, and the programs (processes) using those ports.
Transfers data using various protocols:
curl http://example.com: Fetches the content ofexample.com.curl -o output.html http://example.com: Saves the content tooutput.html.curl -O http://example.com/file.zip: Saves the file with its original name (file.zip).curl -T uploadfile.txt ftp://example.com/upload/: Upload a file to a ftp server.curl -u username:password https://example.com/api: Upload using authenication.curl -X GET https://api.example.com/resource: Send a HTTP GET.curl -X POST -d "key1=value1" https://api.example.com/resource: Send a HTTP POST.
Downloads files from the web:
wget http://example.com/file.zip: Downloadsfile.zip.wget -O my_files.zip http://example.com/file.zip: Downloadsfile.zip.-b: Runs the download in the background.--no-check-certificate: Skips SSL certificate validation.--spider: Checks if a file or URL exists without downloading it.--mirror: Mirrors a website, creating a local copy.
Copies files between systems:
scp file.txt user@remote:/path: Copiesfile.txtto a remote system.scp user@remote:/path/to/file.txt ./: Copiesfile.txtfrom the remote system to the current directory.scp -r localdir user@remote:/path/to/destination: Recursively copieslocaldirto the remote system.
Connects to a remote system securely:
ssh user@hostname: Logs intohostnameasuser.ssh -A user@hostname: Logs intohostnameasuserwith ssh agent forwarding.ssh user@hostname "ls -l /path/to/directory": Runs the command on the remote system and displays the output locally.ssh-copy-id user@hostname: Installs your public key on the remote system for passwordless login.
Command-line tool for controlling NetworkManager. Essential on systems using NetworkManager (common on modern desktops and servers like Ubuntu, Fedora, CentOS/RHEL).
nmcli general status: Show overall NetworkManager status and connectivity state.nmcli device statusornmcli dev: List network devices and their current state (connected, disconnected, etc.).nmcli connection showornmcli con show: List all configured network connections.nmcli connection show --active: List only active connections.nmcli device wifi list: Scan for and list available Wi-Fi networks.nmcli device wifi connect <SSID> password <password>: Connect to a specific Wi-Fi network.nmcli connection up <connection_name_or_uuid>: Activate a specific configured connection.nmcli connection down <connection_name_or_uuid>: Deactivate a specific connection.nmcli radio wifi off: Turn off Wi-Fi.nmcli radio wifi on: Turn on Wi-Fi.