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 theetc
folder.cd ~
: Go to your home directory.
Creates a folder inside the working directory.
mkdir dir1
: Create a folder nameddir1
in the current directory.mkdir dir1 dir2 dir3
: Create multiple folders in the current directory.mkdir ~/Desktop/dir1
: Create a folder nameddir1
on 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 thedir1
directory.
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.txt
to thedir1
directory.mv dir1/* .
: Moves everything fromdir1
to the current directory.mv oldname.txt newname.txt
: Renames a file.
Copies files and directories:
cp output.txt output2.txt
: Copiesoutput.txt
tooutput2.txt
.cp -r dir1 dir2
: Recursively copies thedir1
directory todir2
.
markdown Copy code
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 theetc
folder.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 nameddir1
in the current directory.mkdir dir1 dir2 dir3
: Create multiple folders in the current directory.mkdir ~/Desktop/dir1
: Create a folder nameddir1
on 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 thedir1
directory.
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.txt
to thedir1
directory.mv dir1/* .
: Moves everything fromdir1
to the current directory.mv oldname.txt newname.txt
: Renames a file.
Copies files and directories:
cp output.txt output2.txt
: Copiesoutput.txt
tooutput2.txt
.cp -r dir1 dir2
: Recursively copies thedir1
directory todir2
.
Removes files and directories:
rm output.txt
: Removesoutput.txt
.rm -rf dir1
: Recursively removesdir1
and 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.txt
pointing tosource.txt
.ln source.txt link.txt
: Creates a hard link namedlink.txt
tosource.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.txt
for 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.txt
for editing.
Read more about nano
here.
Creates, extracts, or manipulates archive files:
tar -cvf archive.tar file1 file2
: Creates an archive containingfile1
andfile2
.tar -xvf archive.tar
: Extracts the contents ofarchive.tar
.tar -czvf archive.tar.gz dir/
: Creates a compressed archive of thedir
directory.
Compresses and extracts files:
zip myzip.zip file1 file2
: Compressesfile1
andfile2
intomyzip.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.py
files in the current directory and subdirectories.find . -iname "*.py"
: Performs a case-insensitive search for.py
files.find /home/user/projects -type d -name build
: Finds all directories namedbuild
in/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.jpg
files in/home/user/images
and moves them to/home/user/backup
.find /path/to/dir -name "*.tmp" -exec rm {} \;
: Finds all.tmp
files 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 themkdir
command.
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.txt
touser
.
Changes the group ownership of a file:
chgrp group file.txt
: Changes the group offile.txt
togroup
.
Sets default file permissions:
umask 022
: Ensures new files are created with644
permissions.
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 theNetworkManager
service.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 thePATH
variable.
Note
ifconfig
is considered deprecated and replaced by theip
toolset.netstat
is considered deprecated and replaced by thess
toolset.
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.txt
to a remote system.scp user@remote:/path/to/file.txt ./
: Copiesfile.txt
from the remote system to the current directory.scp -r localdir user@remote:/path/to/destination
: Recursively copieslocaldir
to the remote system.
Connects to a remote system securely:
ssh user@hostname
: Logs intohostname
asuser
.ssh -A user@hostname
: Logs intohostname
asuser
with 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.