ls [options] [directory]
-l
: Long format-a
: Show hidden files-h
: Human-readable file sizes$ ls
Documents Downloads Pictures
$ ls -l
total 12
drwxr-xr-x 2 user user 4096 May 15 10:00 Documents
drwxr-xr-x 2 user user 4096 May 15 10:00 Downloads
drwxr-xr-x 2 user user 4096 May 15 10:00 Pictures
pwd
$ pwd
/home/user
cd [directory]
..
: Parent directory.
: Current directory~
: Home directory$ pwd
/home/user
$ cd Documents
$ pwd
/home/user/Documents
$ cd ..
$ pwd
/home/user
mkdir [options] directory_name
-p
: Create parent directories as needed$ mkdir new_folder
$ ls
Documents Downloads new_folder Pictures
$ mkdir -p parent/child/grandchild
$ ls -R parent
parent:
child
parent/child:
grandchild
parent/child/grandchild:
mv [options] source destination
$ touch file.txt
$ ls
file.txt
$ mv file.txt newfile.txt
$ ls
newfile.txt
$ mv newfile.txt Documents/
$ ls Documents
newfile.txt
cp [options] source destination
-r
: Copy directories recursively$ cp newfile.txt backup.txt
$ ls
backup.txt newfile.txt
$ cp -r Documents Docs_backup
$ ls
backup.txt Docs_backup Documents newfile.txt
rm [options] file/directory
-r
: Remove directories and their contents recursively-f
: Force removal without prompting$ rm backup.txt
$ ls
Docs_backup Documents newfile.txt
$ rm -r Docs_backup
$ ls
Documents newfile.txt
touch file_name
$ touch new_file.txt
$ ls -l
total 0
-rw-r--r-- 1 user user 0 May 15 11:00 new_file.txt
ln [options] target link_name
-s
: Create a symbolic link$ ln -s /path/to/file.txt link_to_file
$ ls -l
total 0
lrwxrwxrwx 1 user user 17 May 15 11:05 link_to_file -> /path/to/file.txt
clear
$ clear
# Terminal screen is now cleared
Remember to practice these commands regularly to become proficient in using the Linux command line interface!
Let’s expand our Linux command-line knowledge with these crucial tools:
cat [options] file1 file2 ...
-n
: Number lines-b
: Number non-empty lines$ cat file.txt
This is the content of file.txt
$ cat -n file.txt
1 This is the content of file.txt
echo [options] string
-e
: Enable interpretation of backslash escapes-n
: Do not print a trailing newline$ echo "Hello, world!"
Hello, world!
$ echo -e "Hello, \nworld!"
Hello,
world!
less [options] file
Space
: Scroll down one pageb
: Scroll up one pageEnter
: Scroll down one lineq
: Quit less$ less large_file.txt
# You can now scroll through the file using the navigation keys.
man [command]
$ man ls
# Displays the manual page for the 'ls' command.
uname [options]
-a
: Display all information-s
: Display the kernel name$ uname -a
Linux hostname 5.10.0-10-amd64 #21~20.04.1-Ubuntu SMP Fri Jul 1 17:06:08 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ uname -s
Linux
whoami
$ whoami
user
tar [options] [archive-file] [files]
-c
: Create archive-x
: Extract archive-t
: List archive contents-z
: Use gzip compression-f
: Specify archive file$ tar -czvf my_archive.tar.gz file1.txt file2.txt
# Creates a compressed archive named 'my_archive.tar.gz' containing 'file1.txt' and 'file2.txt'
$ tar -xzvf my_archive.tar.gz
# Extracts the contents of the archive.
grep [options] pattern [file]
-i
: Ignore case-v
: Invert the match (show lines that don’t match)$ grep "hello" file.txt
# Displays lines in 'file.txt' that contain "hello".
$ grep -i "world" file.txt
# Displays lines containing "world" regardless of case.
head [options] [number] file
-n
: Specify the number of lines to display$ head file.txt
# Displays the first 10 lines of 'file.txt'.
$ head -n 5 file.txt
# Displays the first 5 lines of 'file.txt'.
tail [options] [number] file
-n
: Specify the number of lines to display$ tail file.txt
# Displays the last 10 lines of 'file.txt'.
$ tail -n 5 file.txt
# Displays the last 5 lines of 'file.txt'.
Let’s dive deeper into the world of Linux command-line tools with these advanced utilities:
diff [options] file1 file2
-u
: Unified output format-c
: Context output format-q
: Quiet output, only reports if files differ$ diff file1.txt file2.txt
# Displays the differences between 'file1.txt' and 'file2.txt'.
$ diff -u file1.txt file2.txt
# Displays the differences in unified format.
cmp [options] file1 file2
-s
: Silent output, only reports if files differ$ cmp file1.txt file2.txt
# Reports if 'file1.txt' and 'file2.txt' are identical.
$ cmp -s file1.txt file2.txt
# Silently reports if 'file1.txt' and 'file2.txt' differ.
diff
and cmp
to compare two filescomm [options] file1 file2
-1
: Suppress lines unique to file1-2
: Suppress lines unique to file2-3
: Suppress lines common to both files$ comm file1.txt file2.txt
# Displays lines unique to 'file1.txt', 'file2.txt', and common to both.
$ comm -1 file1.txt file2.txt
# Suppresses lines unique to 'file1.txt'.
sort [options] file
-n
: Sort numerically-r
: Reverse sort-u
: Unique sort$ sort file.txt
# Sorts the content of 'file.txt' alphabetically.
$ sort -n file.txt
# Sorts the content of 'file.txt' numerically.
export [variable_name]=[value]
export PATH=$PATH:/new/path
$ export MY_VAR="Hello, world!"
# Exports the environment variable 'MY_VAR' with the value "Hello, world!".
$ echo $MY_VAR
Hello, world!
zip [options] archive_name file1 file2 ...
-r
: Recursively zip directories-q
: Quiet output$ zip my_archive.zip file1.txt file2.txt
# Zips 'file1.txt' and 'file2.txt' into 'my_archive.zip'.
$ zip -r my_archive.zip directory/
# Recursively zips the contents of 'directory/' into 'my_archive.zip'.
unzip [options] archive_name
-q
: Quiet output-d
: Extract to a specific directory$ unzip my_archive.zip
# Unzips the contents of 'my_archive.zip' to the current directory.
$ unzip -d /path/to/directory my_archive.zip
# Unzips the contents of 'my_archive.zip' to '/path/to/directory'.
ssh [options] user@host
-p
: Specify the port number-i
: Specify the identity file$ ssh user@remote_host
# Establishes a secure connection to 'remote_host' as 'user'.
$ ssh -p 2222 user@remote_host
# Establishes a secure connection to 'remote_host' on port 2222.
service [service_name] [action]
start
: Starts the servicestop
: Stops the servicerestart
: Restarts the service$ service httpd start
# Starts the 'httpd' service.
$ service httpd stop
# Stops the 'httpd' service.
ps [options]
-a
: Displays all processes-u
: Displays processes owned by the specified user-x
: Displays processes without a controlling terminal$ ps -a
# Displays all active processes.
$ ps -u user
# Displays processes owned by 'user'.
kill [signal] [PID]
SIGTERM
(15): Gracefully stop a process.SIGKILL
(9): Immediately terminate a process without cleanup.SIGHUP
(1): Reload a process.$ kill -9 1234
# Immediately terminates the process with PID 1234.
$ kill -TERM 1234
# Gracefully stops the process with PID 1234.
$ kill -l
# Lists all available signals.
To find the PID, you can use commands like ps
, pgrep
, or pidof
.
killall [signal] process_name
killall -9 firefox
$ killall firefox
# Terminates all Firefox processes with the default SIGTERM signal.
$ killall -9 firefox
# Immediately terminates all Firefox processes.
This command is useful for killing multiple processes with the same name without needing to specify each PID.
df [options]
-h
: Human-readable format.-T
: Display file system type.$ df -h
# Displays disk space usage in a human-readable format.
$ df -T
# Displays the type of each file system along with usage information.
This command helps you understand how much disk space is used and available on each mounted file system.
mount [options] device_name mount_point
-t
: Specify the file system type.-o
: Specify mount options.$ mount /dev/sdb1 /mnt
# Mounts the device /dev/sdb1 to the /mnt directory.
$ mount -t ext4 /dev/sdb1 /mnt
# Mounts an ext4 file system from /dev/sdb1 to /mnt.
This command is essential for making file systems available for use by the operating system.
chmod [permissions] file/directory
u
: Userg
: Groupo
: Otherr
: Readw
: Writex
: Execute$ chmod 755 file.txt
# Sets the permissions to rwxr-x (owner has full permissions, group and others have read and execute).
$ chmod u+x file.txt
# Adds execute permission for the owner.
This command is used to manage access control for files and directories.
chown [user]:[group] file/directory
chown user file.txt
chown user:group file.txt
$ chown user file.txt
# Changes the owner of 'file.txt' to 'user'.
$ chown user:group file.txt
# Changes the owner and group of 'file.txt' to 'user' and 'group', respectively.
This command is used to manage file and directory ownership.
ifconfig
is deprecated in favor of ip
command in modern Linux systems.ifconfig [interface]
ifconfig eth0
$ ifconfig eth0
# Displays information about the eth0 network interface.
For modern systems, use the ip
command instead:
$ ip addr show
# Displays detailed information about all network interfaces.
traceroute [options] host
-i
: Specify the network interface.-n
: Suppress DNS lookups.$ traceroute example.com
# Traces the network path to example.com.
$ traceroute -n example.com
# Traces the network path to example.com without performing DNS lookups.
This command helps in diagnosing network issues by showing the path packets take to reach a destination.
wget [options] URL
-b
: Run in the background.-o
: Specify the output file name.$ wget https://example.com/file.txt
# Downloads the file from the specified URL.
$ wget -b https://example.com/file.txt
# Downloads the file in the background.
This command is useful for downloading files from the web directly from the command line.
ufw [options]
enable
: Enables the firewall.disable
: Disables the firewall.allow
: Allows traffic on a specific port or service.deny
: Denies traffic on a specific port or service.$ ufw enable
# Enables the firewall.
$ ufw allow 80
# Allows HTTP traffic on port 80.
$ ufw deny 22
# Denies SSH traffic on port 22.
This command is used to manage firewall rules in Ubuntu and other compatible systems.
iptables [options]
-A
: Append a rule to a chain.-D
: Delete a rule from a chain.-L
: List the rules in a chain.$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# Allows HTTP traffic on port 80.
$ iptables -D INPUT -p tcp --dport 80 -j ACCEPT
# Deletes the rule allowing HTTP traffic on port 80.
$ iptables -L
# Lists all the current firewall rules.
This command is used to manage the underlying firewall rules and is often used by other firewall utilities like ufw
.
Understanding and using these commands will help you manage various aspects of your Linux system, from process management and file system operations to network diagnostics and firewall configuration.
apt
(Advanced Package Tool)apt [options] command
install
: Install packages.remove
: Remove packages.update
: Update the package list.upgrade
: Upgrade installed packages.$ sudo apt update
# Updates the package list.
$ sudo apt install package_name
# Installs the specified package.
$ sudo apt remove package_name
# Removes the specified package.
pacman
(Package Manager)pacman [options] command
-S
: Synchronize packages.-R
: Remove packages.-Syu
: Sync and update the system.$ sudo pacman -Syu
# Synchronizes the package database and updates the system.
$ sudo pacman -S package_name
# Installs the specified package.
$ sudo pacman -R package_name
# Removes the specified package.
yum
(Yum)yum [options] command
install
: Install packages.remove
: Remove packages.update
: Update installed packages.$ sudo yum update
# Updates all installed packages.
$ sudo yum install package_name
# Installs the specified package.
$ sudo yum remove package_name
# Removes the specified package.
rpm
(RPM Package Manager)rpm [options]
-i
: Install a package.-e
: Remove a package.-q
: Query the package database.$ sudo rpm -i package_name.rpm
# Installs the specified RPM package.
$ sudo rpm -e package_name
# Removes the specified package.
$ rpm -q package_name
# Queries the package database for information about the specified package.
sudo
(Superuser Do)sudo [command]
$ sudo apt update
# Executes the 'apt update' command with superuser privileges.
$ sudo useradd new_user
# Adds a new user with superuser privileges.
cal
(Calendar)cal [options]
$ cal
# Displays the current month's calendar.
$ cal 2023
# Displays the calendar for the entire year 2023.
alias
(Alias)alias name='command'
$ alias ll='ls -l'
# Creates an alias 'll' for the 'ls -l' command.
$ alias gs='git status'
# Creates an alias 'gs' for the 'git status' command.
dd
(Disk Dump)dd [options] if=input_file of=output_file
if
: Input file.of
: Output file.bs
: Block size.$ sudo dd if=/path/to/image.iso of=/dev/sdX bs=4M
# Creates a bootable USB stick from an ISO image.
whereis
(Where Is)whereis [command]
$ whereis ls
# Displays the path to the 'ls' binary and its manual page.
whatis
(What Is)whatis [command]
$ whatis ls
# Displays a brief description of the 'ls' command.
top
(Top)top
$ top
# Opens the top utility, displaying system processes and their resource usage.
useradd
(User Add)useradd [options] username
-m
: Create the user’s home directory.-s
: Specify the user’s shell.$ sudo useradd -m new_user
# Adds a new user with a home directory.
usermod
(User Modify)usermod [options] username
-aG
: Add the user to supplementary groups.-s
: Change the user’s shell.$ sudo usermod -aG sudo new_user
# Adds the user to the 'sudo' group.
$ sudo usermod -s /bin/bash new_user
# Changes the user's shell to bash.
passwd
(Password)passwd [username]
$ sudo passwd new_user
# Sets a new password for the specified user.
$ passwd
# Changes the password for the current user.
systemctl
(System Control)$ sudo systemctl start httpd
# Starts the 'httpd' service.
$ sudo systemctl stop httpd
# Stops the 'httpd' service.
$ sudo systemctl enable httpd
# Enables the 'httpd' service to start automatically at boot.
journalctl
(System Journal)journalctl [options]
-f
: Follow the log output.-u
: Filter by unit name.-p
: Filter by priority level.$ journalctl -f
# Displays the system journal and follows the log output.
$ journalctl -u httpd
# Displays the journal entries related to the 'httpd' service.
free
(Free Memory)free [options]
-h
: Human-readable format.$ free -h
# Displays memory usage in a human-readable format.
du
(Disk Usage)du [options] file/directory
-h
: Human-readable format.-a
: Display disk usage for all files.$ du -h /home
# Displays disk usage for the '/home' directory in a human-readable format.
$ du -a /home
# Displays disk usage for all files and directories within '/home'.
These commands are essential for managing packages, system resources, and user accounts on a Linux system. Practice using them to become proficient in administering your environment.