Welcome to our comprehensive guide on various Linux commands.
Whether you’re a beginner dipping your toes into the world of Linux or an experienced user aiming to broaden your knowledge, this guide aims to be an invaluable resource.
Linux, renowned for its robustness and flexibility, powers millions of servers worldwide, and having a solid understanding of its command-line interface is crucial for effective system administration.
This guide is designed to walk you through a wide array of Linux commands, from basic file navigation commands to more advanced commands for system management. We’ll provide explanations and practical examples to help you understand the purpose and functionality of each command.
By the end of this guide, you should be more comfortable with the Linux command-line environment and have a stronger foundation to perform tasks efficiently and effectively.
Basic Linux Commands
Basic Linux commands form the foundation for interacting with a Linux system. They enable users to navigate through the file system, manage files and directories, control processes, and perform various system-related tasks. Commands like ls
, cd
, mkdir
, ps
, and chmod
are fundamental for day-to-day operations on a Linux environment.
Command | Description |
---|---|
ls | Lists all files and directories in the current directory |
cd | Changes the current working directory |
pwd | Prints the path of the current working directory |
cat | Concatenates and displays the content of files |
touch | Creates a new empty file |
rm | Removes files or directories |
cp | Copies files or directories |
mv | Moves or renames files or directories |
mkdir | Creates a new directory |
rmdir | Removes an empty directory |
man | Displays the manual page of a command |
sudo | Executes a command with superuser (root) privileges |
exit | Closes the terminal or ends a terminal session |
hostnamectl | Shows or sets system hostname settings |
date | Displays or sets the system date and time |
hostname | Shows or sets the system’s host name |
ifconfig | Displays or configures network interface parameters |
w | Displays who is logged on and what they are doing |
free -m | Shows the amount of free and used memory in the system |
top | Displays dynamic real-time view of the running tasks in the system |
ls -al | Lists all files (including hidden) in a directory in long format |
cd .. | Changes the working directory to the parent directory |
cat filename | Displays the content of ‘filename’ |
cat file1 file2 > file3 | Concatenates ‘file1’ and ‘file2’ and redirects the output to ‘file3’ |
tail filename | Displays the last part of a file ‘filename’ |
head filename | Displays the beginning of a file ‘filename’ |
mv oldfile newfile | Renames ‘oldfile’ to ‘newfile’ |
rm filename | Deletes ‘filename’ |
mkdir dirname | Creates a directory named ‘dirname’ |
rm -rf dirname | Deletes a directory named ‘dirname’ and its contents |
history | Displays the command history |
shutdown -h now | Shuts down the system immediately |
reboot | Reboots the system |
Networking Commands
Networking commands in Linux are used for diagnosing, monitoring, and configuring network interfaces and connections. They help users to check the network status, troubleshoot connectivity issues, monitor network traffic, configure network interfaces, and interact with remote servers. Commands like ping
, netstat
, ifconfig
, and ssh
are common tools for managing network interactions in a Linux environment.
Command | Description |
---|---|
ifconfig | Displays or configures network interface parameters |
ip | A versatile tool that can display or manipulate all IP or network devices |
netstat | Displays various network-related information such as network connections, routing tables, and interface statistics |
ss | Another utility to investigate sockets, similar to netstat |
ping | Sends an ICMP echo request to establish a connection to server addresses |
traceroute | Prints the route that a packet takes to reach the host |
route | Displays or alters the IP routing table |
nslookup | Queries internet domain name servers |
dig | A flexible tool for interrogating DNS name servers |
host | Performs DNS lookups |
arp | Displays and modifies the IP-to-Physical address translation tables used by Address Resolution Protocol (ARP) |
iwconfig | Configures wireless network interfaces |
curl | A tool to transfer data from or to a server |
wget | Downloads files from the web |
telnet | A user interface to the TELNET protocol |
ssh | Secure Shell is a protocol to securely get access to a remote computer |
ip addr show | Displays IP addresses and all the network interfaces |
ip addr add dev eth1 | Adds an IP address to network interface ‘eth1’ |
netstat -pnltu | Displays listening server sockets along with their PID |
whois domainname | Looks up WHOIS information for a domain name |
dig domainname | Queries DNS servers for information about ‘domainname’ |
host domainname | Performs a DNS lookup for ‘domainname’ |
dig -x IP-Address | Performs a reverse DNS lookup on ‘IP-Address’ |
dig -x domainame | Performs a reverse DNS lookup on ‘domainame’ |
ping host-ip | Sends ICMP echo request to ‘host-ip’ to check network connectivity |
File Permission Commands
File Permission commands in Linux are used to control who can read, write, and execute files and directories. They are essential for maintaining system security, ensuring only authorized users have access to certain files. Commands like chmod
and chown
allow users to modify the permissions and ownership of files and directories.
Command | Description |
---|---|
ls -l filename | Displays detailed information about ‘filename’, including file permissions |
chmod 777 filename | Changes the permissions of ‘filename’ to be fully open (read, write, and execute permissions for everyone) |
chmod -R 777 dirname | Changes the permissions of ‘dirname’ and all its contents to be fully open (read, write, and execute permissions for everyone) |
chmod 766 filename | Changes the permissions of ‘filename’ to allow the owner to read, write, and execute, while everyone else can only read and write |
chmod -x filename | Removes the execute permission from ‘filename’ for all users |
chown username filename | Changes the ownership of ‘filename’ to ‘username’ |
chown user:group filename | Changes the ownership of ‘filename’ to ‘user’ and the group ownership to ‘group’ |
chown -R user:group dirname | Recursively changes the ownership of ‘dirname’ and all its contents to ‘user’ and the group ownership to ‘group’ |
User and Group Management Commands
User and Group Management commands in Linux are utilized for the creation, deletion, and modification of user accounts and groups. They enable system administrators to manage access rights, assign users to specific groups, change user passwords, and adjust other user and group settings. This set of commands is crucial for maintaining system security and managing access to resources.
Command | Description |
---|---|
useradd username | Adds a new user ‘username’ to the system |
userdel username | Deletes the user ‘username’ from the system |
usermod options username | Modifies the settings of user ‘username’ |
passwd username | Changes the password of user ‘username’ |
groupadd groupname | Adds a new group ‘groupname’ to the system |
groupdel groupname | Deletes the group ‘groupname’ from the system |
groupmod options groupname | Modifies the settings of group ‘groupname’ |
usermod -aG groupname username | Adds user ‘username’ to group ‘groupname’ |
id username | Prints real and effective user and group IDs for ‘username’ |
su - username | Switches the current user to ‘username’ |
whoami | Prints the current user name |
chown username filename | Changes the owner of the file to ‘username’ |
chgrp groupname filename | Changes the group of the file to ‘groupname’ |
Process Management Commands
Process management commands in Linux are used to control and interact with processes (running instances of programs). They allow users to view, start, stop, and manage processes, as well as adjust their priority and monitor their status. These commands provide essential control over system resources and running applications.
Command | Description |
---|---|
ps | Reports a snapshot of the current processes |
top | Displays dynamic real-time view of running tasks |
htop | Interactive process viewer (more user-friendly than top) |
pgrep pattern | Looks through the currently running processes and lists the process IDs which match the selection criteria |
pkill pattern | Sends a signal to the processes identified by the ‘pattern’ |
kill PID | Sends a signal to the process with the specified Process ID (PID) |
killall process-name | Kills all processes with the name ‘process-name’ |
bg | Places a job in the background |
fg | Brings a job to the foreground |
jobs | Lists the jobs currently running in the background |
nice | Modifies the priority of a process |
renice | Changes the priority of a running process |
uptime | Shows how long the system has been running |
Disk Management Commands
Disk management commands in Linux are utilities used to manage storage devices, file systems, and data on a Linux system. They help users to view disk usage, manage files and directories, format and partition disks, and mount or unmount disk volumes. These commands are critical for managing storage effectively and efficiently on a Linux system.
Command | Description |
---|---|
df | Reports the amount of disk space used and available on file systems |
du | Estimates file and directory space usage |
fdisk | Manipulates disk partition table |
parted | A partition manipulation program |
mkfs | Builds a Linux file system, usually on a unmounted partition |
mount | Mounts a file system |
umount | Unmounts a file system |
fsck | Checks the integrity of a file system and fixes errors if necessary |
lsblk | Lists block devices |
blkid | Locates/print block device attributes |
dd | Converts and copies a file, can also be used to create disk images |
file | Determines the file type |
findmnt | Finds a file system |
lsof | Lists open files, and can also list files opened by a specific process |
fdisk /dev/sda | Starts fdisk for device /dev/sda |
mkfs.ext4 /dev/sda1 | Makes an ext4 file system on /dev/sda1 |
fsck.ext4 /dev/sda1 | Checks an ext4 file system on /dev/sda1 |
mount /dev/sda1 /mnt | Mounts /dev/sda1 to the /mnt directory |
df -h | Displays disk usage in human-readable format |
df -i | Displays the inode usage of the file system |
du -hs | Displays a summary of the total disk usage in human-readable format |
lsusb -tv | Displays USB devices as a tree |
hdparm -tT /dev/sda | Performs a read speed test on /dev/sda |
badblocks -s /dev/sda | Checks for bad blocks on /dev/sda |
Package Management Command
Package management commands in Linux are used to automate the process of installing, upgrading, configuring, and removing software packages in a consistent manner. They handle software dependencies and ensure that all packages are kept up-to-date and function correctly. The specific package management system and the corresponding commands vary between different Linux distributions.
Below are all of the commands to install, remove and manage packages in Linux.
Command | Description |
---|---|
apt-get update | Updates the list of available packages and their versions, but it does not install or upgrade any packages |
apt-get upgrade | Installs the newest versions of all packages currently installed on the system |
apt-get install packageName | Installs a package |
apt-get remove packageName | Removes a package |
apt-get autoremove | Removes packages that were automatically installed to satisfy dependencies for some package and that are no more needed |
apt-get -y upgrade | Upgrades the system by removing/installing/upgrading packages |
dpkg -i packageName.deb | Installs a DEB package |
dpkg -r packageName | Removes a DEB package |
dpkg -l | grep -i installed | Lists all installed DEB packages |
yum update | Updates all system packages |
yum install packageName | Installs a package |
yum remove packageName | Removes a package |
yum list | Lists all packages in all repositories |
rpm -i packageName.rpm | Installs an RPM package |
rpm -e packageName | Removes an RPM package |
Compress and Uncompress Commands
In Linux, compress and uncompress commands are used to reduce the size of files or directories (compression) and restore them to their original size (uncompression or extraction). They are essential for data storage, file transfer, and for saving bandwidth on the internet. Tools like ‘tar’, ‘gzip’, ‘zip’, and ‘unzip’ are widely used for these tasks.
Command | Description |
---|---|
tar cf file.tar files | Creates a tarball |
tar xf file.tar | Extracts from a tarball |
tar czf file.tar.gz files | Creates a tarball with gzip compression |
tar xzf file.tar.gz | Extracts from a gzip compressed tarball |
tar cjf file.tar.bz2 files | Creates a tarball with bzip2 compression |
tar xjf file.tar.bz2 | Extracts from a bzip2 compressed tarball |
tar -cvf filename.tar filename | Creates a tarball with verbose output |
tar -xvf filename.tar | Extracts from a tarball with verbose output |
tar -tvf filename.tar | Lists the contents of a tarball |
tar -xvf filename.tar file1.txt | Extracts a specific file from a tarball |
tar -rvf filename.tar file2.txt | Appends a file to a tarball |
gzip file | Compresses a file with gzip, resulting in file.gz |
gunzip file.gz | Decompresses a gzip file |
zip file.zip files | Creates a zip archive |
unzip file.zip | Extracts files from a zip archive |
zip filename.zip file1.txt file2.txt file3.txt | Adds multiple files to a zip archive |
zip -u filename.zip file4.txt | Adds or updates a file in a zip archive |
zip -d filename.zip file4.txt | Deletes a file from a zip archive |
unzip -l filename.zip | Lists the contents of a zip archive |
unzip filename.zip -d /dirname | Extracts files from a zip archive to a specific directory |