LINUX COMMANDS CHEAT
SHEET
www.codelivly.com
LINUX COMMANDS CHEAT SHEET
Basic File Operations
ls -lhfile Display file permissions, size, owner, etc.
touch file Creates an empty file
Copy file1 to file2. File2 can be a directory.
cp file1 file2
mv file dir Move a file to a directory
mv file1 file2
Rename file1 to file2
rm file
Delete a file.
ls -lah
List all the contents in a directory.
mkdir data Creates a directory.
Copy dir1 and its contents to dir2.
cp -r dir1 dir2
rm -rf dir
Delete a directory and its contents.
pwd
Print current working directory.
stat file
Display attributes of files and directories.
wc file
Count bytes, words, lines in a file or STDIN.
type cd
Find out whether cd binary is built-in, alias, or external binary file
LINUX COMMANDS CHEAT SHEET
File Viewing
cat file.txt Displays the entire content of the file.
cat -n file.txt Displays the contents of a file with line numbers.
Displays the content of the file in reverse order (from the last line
tac file.txt
to the first).
Allows you to view large text files one screen at a time.
more file.txt
head -n 5 file.txt Displays the first 5 lines of the file.
tail -n 5 file.txt Displays the last 5 lines of the file.
Similar to more, but with additional features like scrolling and
less file.txt
searching.
Displays the file's contents with line numbers.
nl file.txt
Compares two files line by line and shows the differences between
diff file1.txt file2.txt them.
vi filename.txt Opens the file filename.txt in vi editor for editing.
nano filename.txt Opens the file filename.txt for editing.
LINUX COMMANDS CHEAT SHEET
File Search
locate file Finds the locations of files and directories by name.
which cd Shows the path of the command if it exists.
Displays the location of the binary, source, and man pages of
whereis ls
the command ls.
Searches for the file hello.txt inside the /data directory.
find /data -name hello.txt
Directory Traversal
cd or cd ~ Navigates to the home directory.
cd .. Moves up one directory (to the parent directory).
cd - Switches back to the previous working directory.
cd / Moves to the root directory.
cd /tmp Navigates to the /tmpdirectory.
cd ../.. used to change directories and move up two levels in the file
LINUX COMMANDS CHEAT SHEET
Disk Management
Displays disk usage information in a human-readable format (like
df -h
GB, MB).
du -h /home Shows the size of files and directories within the /home directory.
fdisk -l Lists all disks and their partitions.
lsblk Displays the block devices and their hierarchy.
mount /dev/sda1 /mnt Mounts the partition /dev/sda1 to the /mntdirectory.
umount /mnt Unmounts the partition from /mnt.
mount -a used to mount all filesystems mentioned in the /etc/fstab file.
free -m show the swap file system
mkswap /dev/sdb1 Create Swap Space
swapon /dev/sdb1 Activate Swap Space
swapon -a activate all swap spaces listed in the /etc/fstabfile
LINUX COMMANDS CHEAT SHEET
Process Management
ps Displays a snapshot of currently running processes.
ps aux Shows all running processes for all users.
top Displays real-time system processes and their resource usage.
Finds and displays the process ID (PID) for processes named
pgrep firefox
firefox.
Displays the PID of the firefoxprocess.
pidof firefox
kill 6732 Terminates the process with PID 6732.
pkill firefox Kills all processes named firefox.
bg Resumes a stopped process in the background.
fg Brings a background process to the foreground.
used to display the status of jobs that are running or have been
jobs
started in the background within the current shell session
The nice command is used to start a process with a user defined
nice vim text &
priority
LINUX COMMANDS CHEAT SHEET
Networking
Sends ICMP echo requests to google.com to test network
ping google.com
connectivity.
ifconfig
Displays network interface configurations, including IP addresses.
ip addr Shows IP address information for network interfaces.
netstat Displays network statistics and active connections.
hostname displays the current hostname of the system.
hostnamectl Display information about the system
nmcli nmcliis a command-line interface for NetworkManager
nmcliconnection show List Available Connections
route -n Display routing table
ip route Display routing table
netstat -tulpen Listening ports
LINUX COMMANDS CHEAT SHEET
File Permissions
chmod g+w file1 adds write permission for the group on the file file1.
chmod o+w file1 Adds write permission for others on file1
chmodu-w file1 Removes write permission for the user (owner) on file1.
chmod u+w,g+wx,o+r Adds write permission for the user, write and execute permissions
file1 for the group, and read permission for others on file1.
Sets read and write permissions for all users (owner, group, and
chmod a=rw file1
others) on file1.
Adds read and write permissions for all users (owner, group, and
chmod +rw file1
others) on file1.
Changes the owner of file1 to Ali.
chownAli file1
chown-R Ali dir1 Recursively changes the owner of dir1 and its contents to Ali
chown user:group
Changes the ownership of file.txt to user and group group.
file.txt
chown:sales file1 Changes the group ownership of file1 to sales without altering the
file’s owner.
Sets the defaults perms for newly created files to 644 and for
umask 022
directories to 755
LINUX COMMANDS CHEAT SHEET
Process Management
Creates a backup.tar archive from the /home
tar -cf backup.tar /home
directory.
Extracts files from the backup.tar archive.
tar -xfbackup.tar
Compresses the /home directory into a data.tar.gz
tar -zcvfdata.tar.gz /home archive.
Extracts the data.tar.gz archive.
tar -zxvfdata.tar.gz
gzip data Compresses the file data using Gzip, creating data.gz.
gunzip data.gz Decompresses the data.gz file back to data.
Recursively compresses the /home directory into a
bzip2 file.text
data.zip archive.
Decompresses the file.text.bz2 file, returning it to
bunzip2 file.text.bz2
file.text.
Compresses the file.textfile using the XZ compression
xz file.text
format, creating file.text.xz.
Decompresses the file.text.xzfile, returning it to
unxz file.text.xz
file.text.
Compresses file.textusing Gzipand writes the
gzip-c file.text> file.gz
compressed output to file.gz.
LINUX COMMANDS CHEAT SHEET
Installing and updating software packages with RPM
rpm -qa List all RPM packages currently installed.
Display the version of the package named "NAME"
rpm -q NAME installed on the system.
Display detailed information about a package.
rpm -qi NAME
rpm -ql NAME List all files included in a package.
rpm -qc NAME List configuration files included in a package.
rpm -qd NAME List documentation files included in a package.
Show a short summary of the reason for a new
rpm -q --changelog NAME
package release.
Display the shell scripts run during package
rpm -q --scripts NAME installation, upgrade, or removal.
LINUX COMMANDS CHEAT SHEET
Installing and updating software packages with YUM
yum list [NAME-PATTERN] List installed and available packages by name
yum group list List installed and available groups
yum search KEYWORD Search for a package by keyword
yum info PACKAGENAME Show details of a package
yum install PACKAGENAME Install a package
yum group install GROUPNAME Install a package group
yum remove PACKAGENAME Remove a package
yum history Display transaction history
LINUX COMMANDS CHEAT SHEET
Access Control Lists (ACL)
Displays the ACL entries for the file, showing which
getfacl file
users and groups have specific permissions.
getfacl. Displays the ACL entries for the current directory.
Grants read and write (rw) permissions to the user
setfacl-m u:abeer:rw file.txt moahmedfor file.txt.
Grants read and write (rw) permissions to all other
setfacl-m o::rw file.txt users (others) for file.txt.
Grants read and write (rw) permissions to the sales
setfacl-m g:sales:rw dir1
group for dir1.
Recursively grants read and write (rw) permissions to the sales
group for dir1 and all its subdirectories and files.
setfacl-R -m g:sales:rw dir1 Removes the ACL entry for the user moahmedfrom
setfacl-x u:abeer file.txt file.txt.
Removes all ACL entries and restores default
setfacl-b file.txt permissions for file.txt.
LINUX COMMANDS CHEAT SHEET
User Management
Displays the user ID (UID), group ID (GID), and group
id
memberships of the current user.
Displays the username of the currently logged-in user.
whoami
useradd mohamed Creates a new user named mohamed.
passwd mohamed Sets or changes the password for the user mohamed.
usermod-L mohamed Locks the user mohamedaccount, disabling login.
usermod-U mohamed Unlocks the user mohamedaccount, enabling login.
Assigns the user mohamedto the sales group
usermod-G sales mohamed
(replaces any existing groups).
Adds the user mohamedto the admin group without
usermod-aGadmin mohamed
affecting other group memberships.
Deletes the user mohamed(home directory and files
userdel mohamed
are retained).
Deletes the user test and removes their home
userdel -r test
directory and files.
Switches the current session to the user mohamed
su - mohamed