0% found this document useful (0 votes)
13 views

Linux commands cheat sheet

Uploaded by

arabe.codz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Linux commands cheat sheet

Uploaded by

arabe.codz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Stardz.dz@gmail.

com 20 Dec 2024

Mohamed Atef
Elbitawy

LINUX COMMANDS CHEAT SHEET


BY: Mohamed Atef Elbitawy
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

LINUX COMMANDS CHEAT SHEET


Basic File Operations
ls -lh file Display file permissions, size, owner, etc.

touch file Creates an empty file

cp file1 file2 Copy file1 to file2. File2 can be a directory.

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.

cp -r dir1 dir2 Copy dir1 and its contents to 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
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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).

more file.txt Allows you to view large text files one screen at a time.

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.

nl file.txt Displays the file's contents with line numbers.

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.


Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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.

find /data -name hello.txt Searches for the file hello.txt inside the /data directory.

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 /tmp directory.

cd ../.. used to change directories and move up two levels in the file
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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 /mnt directory.

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/fstab file


Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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.

pidof firefox Displays the PID of the firefox process.

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
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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 nmcli is a command-line interface for NetworkManager

nmcli connection show List Available Connections

route -n Display routing table

ip route Display routing table

netstat -tulpen Listening ports


Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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

chmod u-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.

chown Ali file1 Changes the owner of file1 to Ali.

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
Changes the group ownership of file1 to sales without altering the
chown :sales file1 file’s owner.
Sets the defaults perms for newly created files to 644 and for
umask 022 directories to 755
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

LINUX COMMANDS CHEAT SHEET


Process Management
Creates a backup.tar archive from the /home
tar -cf backup.tar /home directory.

tar -xf backup.tar Extracts files from the backup.tar archive.

Compresses the /home directory into a data.tar.gz


tar -zcvf data.tar.gz /home archive.

tar -zxvf data.tar.gz Extracts the data.tar.gz archive.

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.text file using the XZ compression
xz file.text format, creating file.text.xz.
Decompresses the file.text.xz file, returning it to
unxz file.text.xz file.text.
Compresses file.text using Gzip and writes the
gzip -c file.text > file.gz compressed output to file.gz.
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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.

rpm -qi NAME Display detailed information about a package.

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.
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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


Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

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 moahmed for 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
setfacl -R -m g:sales:rw dir1 group for dir1 and all its subdirectories and files.

Removes the ACL entry for the user moahmed from


setfacl -x u:abeer file.txt file.txt.
Removes all ACL entries and restores default
setfacl -b file.txt permissions for file.txt.
Stardz.dz@gmail.com 20 Dec 2024

Mohamed Atef
Elbitawy

LINUX COMMANDS CHEAT SHEET


User Management
Displays the user ID (UID), group ID (GID), and group
id memberships of the current user.

whoami Displays the username of the currently logged-in user.

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 mohamed account, disabling login.

usermod -U mohamed Unlocks the user mohamed account, enabling login.

Assigns the user mohamed to the sales group


usermod -G sales mohamed (replaces any existing groups).
Adds the user mohamed to the admin group without
usermod -aG admin 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.

su - mohamed Switches the current session to the user mohamed

You might also like