Linux Command Line 101 : Essential Commands for Software Engineers
Summarizing the Most Used Commands and Tools for Daily Tasks in Backend Development, DevOps, and Beyond 👨💻
We all know that Linux and Unix based operating systems have a wide range of commands that can be difficult to remember and fully understand. Even I often forget some commands (especially when it comes to searching for files😅). I find myself repeatedly searching the web to remember the right command. So, I decided to write this article to help anyone who, like me, struggles with remembering these commands. This article aims to provide a simple guide that you can refer to whenever you need help with these commands.
I have structured this article in a way that begins by categorizing commands based on their domains. Following this categorization, we will explore a selection of highly practical use cases for each command .At the end, I have provided some cool tricks and shortcuts that can help you work more efficiently in the CLI environment. Lets Dive In 😉!
Table Of Content:
- File and Directory Operations
- Text Processing
- System Information
- Monitoring
- Process Management
- Network Operations
- User and Group Management
- Package Management
- File Compression and Archiving
- System Administration
- File Permissions and Ownership
- Input and Output Redirection
- System Startup and Shutdown
- File and Data Transfer
- Disk and Filesystem Operations
- Shell Scripting
- System Configuration
- Text Editing and Manipulation
- File Searching and Locating
- Working with Environment Variables
- Tips & Tricks
File and Directory Operations:
Manipulate files and directories, such as listing, creating, removing, copying, and moving them.
- ls : List files and directories
Ls -lash
- cd : Change directory.
cd [directory_name]
- mkdir : Create a new directory.
mkdir [directory_name]
//Make directory wit
mkdir -m 755 new_directory
- touch : Create an empty file.
touch [file_name]
- rm : Remove files and directories.
#Simple remove
rm [directory/file_names]
#Recursive and Forced remove
rm -rf [directory_name / path to directory]
- cp : Copy files and directories.
#Simple copy
cp source-file.txt destination-file.txt
#Recursive copy
cp -R source-directory destination-directory
- mv : Move or rename files and directories.
mv [directory/file_name] [target_path]
- stat: Display file or file system status.
stat [directory/file_name]
- pwd : Print working directory.
Text Processing:
Search, manipulate, and analyze text files using tools like concatenation, searching for patterns, and text transformations.
- cat : Display the contents of a file . suitable for short files
cat [textfile]
- Less :same as cat but suitable for large files and offers more features
less [textfile]
- grep : Search for a pattern in files.
#Searches for occurrences of the pattern in file
grep pattern [file_name]
#Recursively searche over directory
grep -r pattern [directory_name]
System Information:
Retrieve information about the system’s hardware, software.
- uname : Print system information . displays all system information, including the kernel name, network node hostname, kernel release, kernel version, machine hardware name, and processor architecture.
uname -a
- Lsblk : list information about block devices, such as hard drives and storage devices
#List only disk devices and their rotational status (SSD or not).
lsblk -d -o name,rota
- Lspci : displays information about all PCI (Peripheral Component Interconnect) devices connected to your system, including graphics cards, network adapters, sound cards, and other devices.
- Lshw : List hardware information including , memory, storage devices, network interfaces, and more
- Lscpu : Display CPU information including the architecture, vendor, model name, CPU cores, cache sizes, and other CPU-related details
Monitoring:
monitor resource utilization
- top : Monitor and display real-time system resource usage.
- free : Display available and used memory.
#Available and used memory in human readable format
free -h
- df : Show disk space usage of file systems.
#Shows disk usage in human readable format
df -h
- du : Estimate file and directory space usage. This command is at its best when you need to see the size of a given directory or subdirectory
#Shows directory size in a summarized and human readble format
du -sh directory_name
Process Management:
Control and manage running processes, including listing, terminating, and monitoring them.
- ps : Display running processes.
#Detailed and comprehensive view of the processes running
ps aux
- kill : Terminate a process by its process ID (PID).
#Sending SIGKILL to process
kill -9 [PID]
- killall : Terminate processes by their name. command is used to terminate processes by their names rather than their PIDs
#Sending SIGKILL to process
killall -KILL [process_name]
- pkill : It’s like killall but It allows you to send signals to processes based on various criteria such as process names, user ownership, process attributes, and more.
#Kill pricess bt name
pkill [process_name]
#terminate processes matching a particular pattern using regular expressions
pkill -f "pattern"
#terminate processes owned by a specific user
pkill -u [user_name]
#terminate processes in a specific process group
pkill -g [group_ID]
- pgrep : List processes based on their name or other attributes.
#Get process IDs by name
pgrep -a [process_name]
- nice : Set the priority of a process.
#Set value from -20(highest priority) to 19 (lowest priority)
nice -n [value] [command]
- renice : Change the priority of a running process.
#Changing process niceness
renice [value] [PID]
- jobs : lists the currently running jobs or processes in the background.
- bg : move a job or process into the background.
bg [job_id]
- fg : bring a job or process back to the foreground.
fg [job_id]
Network Operations:
Configure network interfaces, troubleshoot connectivity, and communicate with other network hosts.
- ifconfig : Configure and display network interfaces.
- ip : Configure and display network interfaces (more powerful than ifconfig).
ip addr show
- ping : Send ICMP echo requests to a network host.
ping [host_name/ip]
- ufw: Firewall Manager, a user-friendly interface to manage netfilter firewall rules in Linux.
#Activate the firewall
ufw enable
#Deactivate the firewall
ufw disable
#Allow incoming traffic on a specific port or protocol.
ufw allow [port/protocol]
#Deny incoming traffic on a specific port or protocol.
ufw deny [port/protocol]
#Show the status of the firewall, including which rules are active.
ufw status
#Reset all firewall rules to default settings.
ufw reset
- traceroute : Trace the route that packets take to reach a network host.
traceroute [host_name]
- netstat : Display network connections, routing tables, and network interface statistics.
#This command provides information about active network connections,
#listening ports, routing tables, and network interface statistics.
#It also show PID.
netstat -tunap
- nslookup : Query DNS servers to retrieve domain name or IP address information.
#Retrieve information about a domain name
nslookup[domain_name]
- SSH : Secure command execution on remote server
#Create SSH key pair ( creates 2 files :1- id_rsa (your private key) and
# 2- id_rsa.pub (your public key))
ssh-keygen -t rsa
#See SSH-related files
ls ~/.ssh
#SSH into a remote server
ssh [user_name]@[host_name / IP]
User and Group Management:
Create, modify, and manage user accounts and groups on the system.
- id : Display user and group identity information.
#Username is optional
id [user_name]
- useradd : Creates a new user account.
useradd [user_name]
- userdel : Deletes a user account.
userdel [user_name]
- groupadd : Creates a new group.
groupadd [group_name]
- groupdel : Deletes a group.
groupdel [group_name]
- passwd : Sets or changes a user's password.
passwd [user_name]
- usermod : Modifies user account properties
#To add a user to additional groups without removing from existing groups
sudo usermod -aG [group_name] [user_name]
- groupmod : Modifies group properties.
#Add a user to a group,
sudo groupmod -A [user_name] [group_name]
#Remove user from group
sudo groupmod -R [user_name] [group_name]
Package Management:
Install, update, and remove software packages on the system.
- apt : Package handling utility for Debian-based systems.
#Update pkg list
apt update
#Upgrade installed pkgs
apt upgrade
#Install pkg
apt install [pkg_name]
#Uninstall pkg
apt remove [pkg_name]
#List installed pkgs
apt list
#Search pkg
apt search [pkg_name]
#Show pkg detail
apt show [pkg_name]
#Remove unused dependencies
apt autoremove
#Autoremove and upgrade together
apt full-upgrade
- yum : Package manager for RPM-based systems.
#Update pkg list
yum update
#Upgrade installed pkgs
yum upgrade
#List installed pkgs
yum list
#Search pkg
yum search [pkg_name]
#show pkg detail
yum info [pkg_name]
- snaps : Package manager with easy distribution and automatic updates.
#Install a snap
snap install [snap_name]
#Remove snap
snap remove [snap_name]
#List installed snaps
snap list
#View snap info
snap info [snap_name]
#Search for snaps
snap find [search_term]
File Compression and Archiving :
Compress and archive files and directories into a single file for storage or transmission.
- tar : Archive files into a tarball.
#Create an archive (tarball) of files or directories(not compressed)
tar -cf archive.tar [file_name1] [file_name2] directory
#Compressed archive
tar -zcf archive.tar [file_name1] [file_name2] directory
#Extract the contents of an archive
tar -xvf archive.tar
- gzip : Compress files using gzip compression.
#Maximum compression level
gzip -v9 [file_name1] [file_name2]
#Decompress
gzip -d mydoc.txt.gz
- zip : Compress files into a zip archive
#Create zip archive
zip [archive_name.zip] [file_name1] [file_name2]
#list the contents of a zip archive
zip -sf <archive_name.zip>
#Extract the content of zip archive
unzip [archive_name.zip]
System Administration:
Perform administrative tasks such as managing users, configuring system settings, and maintaining system security.
- sudo : Execute commands with superuser privileges.
#Change to root user
sudo su
- su : Switch to a different user account.
su [user_name]
- systemctl : Control system services.
#Start service
systemctl start {service_name}
#Stop service
systemctl stop {service_name}
#Restart service
systemctl restart {service_name}
#Service status
systemctl status {service_name}
- service : Control system services (legacy). Exactly like systemctl but for older versions.
- chroot : Used to change the root directory for a specific command or process, creating a restricted environment.
sudo chroot [new_root] [command]
- visudo : Used to edit the sudoers file . The sudoers file determines which users or groups are allowed to run commands with administrative privileges using sudo.
sudo visudo
File Permissions and Ownership:
Set permissions and ownership for files and directories to control access rights.
- chmod: Change file permissions.
#Add permissions
chmod +rwx [directory/file_name]
#Remove permissions
chmod -rwx [directory/file_name]
#Read, write, and execute permissions for everyone
chmod 777 [directory_name]
- chown : Change file ownership.
chown [user_name] [file_name]
- chgrp : Change group ownership.
chgrp groupname filename
- umask : Set default file permissions. used to set the default file permissions for newly created files and directories.
#Current umask value
umask
#Set umask value
umask 022
- chown : Change file ownership with superuser privileges.
sudo chown [user_name] [file_name]
Input and Output Redirection:
Redirect input and output streams between commands and files.
- | (pipe) : Redirect output of one command as input to another command.
command1 | command2
- | | : Append output to a file.
command1 || command2
- < : Redirect input from a file.
- 2>: Redirect error output to a file.
- &: Redirect both standard output and standard error.
- tee : Read from standard input and write to both standard output and files.
System Startup and Shutdown:
Manage system startup and shutdown processes, including starting and stopping services.
- shutdown : Used to schedule system shutdowns or reboots
#Shutdown the system immediately
shutdown -h now
#Reboot the system immediately
shutdown -r now
- init : Used to change the system’s runlevel or to control the init process, which is the first process started by the Linux kernel during the boot process.
init [runlevel]
#Shutdown
init 0
#Reboot
init 6
File and Data Transfer:
Transfer files between local and remote systems securely and efficiently.
- sftp : Securely transfer files over SSH.(Most used and easy to use File transfer tool)
#Connect to remote server
sftp user@host
#Commands starting with l start on local server
#Local system pwd
lpwd
#Local ls
lls
#Commands without l at the begining run on remote server
#Remote pwd
pwd
#Remote ls
ls
#Upload file to remote server
put localFile
#Download file from remote server
get remoteFile
- wget : Retrieve files from the web using HTTP, HTTPS, or FTP.
#download url
wget [url]
- curl : Transfer data from or to a server using various protocols.
curl [url]
-X POST|GET|DELETE or ... -> HTTP Verb
-H -> Set Header
-b "cookie1=test" -> Set a Cookie
-c {"cookie jar file"} -> Set Cookie file
-d '{"productId": 123456, "quantity": 100}' -> Posting body with request
- ftp : Transfer files to or from a remote server using FTP.
#Connet to FTP server
ftp [host_name/IP]
#Download file
get [file_name]
#Upload file
put [localfile] [remotefile]
#Delete file on ftp server
delete [file_name]
#Quit and disconnect from the FTP server:
quit
Disk and Filesystem Operations:
Manage disks, partitions, and filesystems, including checking disk space usage and manipulating partitions.
- df : Show disk space usage of file systems.
#Shows disk usage in human readable format
df -h
- du : Estimate file and directory space usage. This command is at its best when you need to see the size of a given directory or subdirectory
#Shows directory size in a summarized and human readble format
du -sh directory_name
- fsck : Check and repair a filesystem.
#Automatically repair filesystem errors (if possible)
fsck -a <device>
- mkfs : Create a new filesystem.
- mount : Mount a filesystem, making it accessible and usable within the directory tree of the Linux filesystem.
mount [device] [mount_point]
- umount : Unmount a filesystem.
umount [mount_point]
- fdisk : Manipulate disk partition table.
blkid [device]
- blkid : Display block device attributes.
blkid [device]
Shell Scripting:
Write and execute scripts to automate tasks and execute sequences of commands.
- bash : Execute a Bash script.
bash [script_name.sh]
- sh : Execute a shell script . It runs the script using the default shell interpreter defined in the system.
sh [script_name.sh]
- /etc/shells : List of available shells
cat /etc/shells
System Configuration:
Configure system settings and parameters, often related to the kernel, network, or system services.
- sysctl : Configure kernel parameters at runtime.
#Display all kernel variables
sysctl -a
#View the current value of a kernel parameter
sysctl [parameter_name]
#Modify a kernel parameter value
sysctl -w [parameter_name]=[new_value]
- hostnamectl : Control the system hostname.
#View the current hostname
hostnamectl
#Set a new hostname
hostnamectl set-hostname [new_hostname]
- timedatectl : Control the system time and date settings.
#Current system time
timedatectl
#Set the system time zone
sudo timedatectl set-timezone [timezone]
#Manually set the system time
sudo timedatectl set-time "<date> <time>"
Text Editing and Manipulation:
Edit and modify text files using command-line text editors.
- nano : Beginner-friendly terminal-based text editor.
nano [file_name]
- vim : Improved version of vi with additional features.
vim [file_name]
File Searching and Locating:
Search for files and directories on the system based on various criteria.
- find : Search for files and directories based on various criteria.
find [path to search] -name "file_name"
- locate : Quickly find files based on a pre-built database.
locate "filename"
- updatedb : Update the file database used by locate.
- whereis : Locate the binary, source, and manual page files for a command.
whereis [command]
- grep : Searching and filtering text patterns within files or command output.
grep [options] pattern [file...]
#Recursive search in directory
grep -r "pattern" directory/
#Search in STDOUT
cat [file_name] | grep "pattern"
Working with Environment Variables:
- env: Display or set environment variables.
#List of current env vars
env
#Set env var
env VAR1=HTF
- printenv: Display the values of environment variables.
- $VARIABLE_NAME: Access the value of an environment variable.
echo $VARIABLE_NAME
- export: Set environment variables.
export [VARIABLE_NAME]=[value]
- echo: Display the value of an environment variable.
echo $VARIABLE_NAME
- unset: Remove environment variables.
unset [VARIABLE_NAME]
- envsubst: Substitute environment variables in a file.
envsubst [inputfile-name] [outputfile_name]
Tips & Tricks:
- Ctrl+a : Cursur to the line begining
- Ctrl+e : Cursor to the line end
- Ctrl+u :Delete everything before cursor
- Ctrl+r : Reverse search command history
- Ctrl+c : Sends the SIGINT signal to the current process.
- Ctrl+z : Sends the SIGTSTP signal to the current process
- Ctrl+d : Sends the SIGOUT signal to the current process
- !!(bang bang) : Repeat last cli command
!! | grep "search term"
- #! (shebang) : Changing shell
- ~ : point to home dir
~/.bash-history
- /dev/null : special file in Linux that discards any data written to it
#Discarding output
command > /dev/null
- || : Executing the next command if the previous command fails
command1 || command2
- && : Executing the next command if the previous command succeeds
command1 && command2
- & : Running a command in the background
command &
- ; : Sequentially executing multiple commands
command1 ; command2 ; command3
Thank you 🙏 for taking the time to read blog post! Your comments 💬and claps👏would be greatly appreciated as they would motivate me to continue writing and improve.
Moein Moeinnia , My linkedin