Essential Linux Commands Cheat Sheet for Beginners
Posted on Aug 30, 2024 in Computers
Linux Commands Cheat Sheet
Directory Navigation
ls = List files and directories in the current directory.
ls -a = List all files and directories, including hidden files.
ls -l = List files and directories in long format.
pwd = Show the present working directory.
cd [dir_path] = Change location to the specified directory.
cd ~ = Change directory to $HOME.
cd .. = Move up one directory level.
Files
mkdir [dir_name] = Create a new directory.
rm [file_name] = Remove a file.
rm -r [directory_name] = Remove a directory recursively.
cp [source_file] [destination_file] = Copy the contents of one file to another file.
mv [source_file] [destination_file] = Move or rename files or directories.
touch [file_name] = Create a new file.
cat [file_name] = Show the contents of a file.
cat [source_file] >> [destination_file] = Append file contents to another file.
nano [file_name] = Open or create a file using the Nano text editor.
vi [file_name], vim [file_name] = Open or create a file using the Vi/Vim text editor.
head [file_name] = Show the first ten lines of a file.
tail [file_name] = Show the last ten lines of a file.
Users and Groups
sudo useradd [user_name] = Create a new user account.
sudo userdel [user_name] = Delete a user account.
sudo usermod -aG [group_name] [user_name] = Modify user information (add a user to a group).
sudo passwd [user_name] = Change the current user’s or another user’s password.
sudo groupadd [group_name] = Add a new group.
sudo groupdel [group_name] = Delete a group.
sudo [command] = Temporarily elevate user privileges to superuser or root.
su - [user_name] = Switch the user account or become a superuser.
File Permissions
chmod 777 [file_name] = Assign read, write, and execute file permission to everyone (rwxrwxrwx).
chown [user_name] [file_name] = Change the ownership of a file.
chown [user_name]:[group_name] [file_name] = Change the owner and group ownership of a file.
chgrp [group_name] [file/directory] = Change file or directory group.
Processes
ps = List active processes.
top = See all running processes.
htop = Interactive and colorful process viewer.
kill [process_id] = Terminate a Linux process under a given ID.
killall [label] = Terminate all processes with a given label.
nohup [command] & = Run a Linux process in the background.
bg = List and resume stopped jobs in the background.
fg = Bring the most recently suspended job to the foreground.
fg [job] = Bring a particular job to the foreground.
System Management
uptime = Display how long the system has been running, including the load average.
hostname = View system hostname.
hostname -i = Show the IP address of the system.
date = See current date and time.
cal = Show current calendar (month and day).
whoami = See which user you are using.
Network
ifconfig = Display IP addresses of all network interfaces.
ping [remote_host] = Ping remote host.
netstat = Show network statistics.
nslookup [domain_name] = Receive information about an internet domain.
SSH Login
ssh [user_name]@[host] = Connect to a remote host as a user via SSH.
ssh [host] = Securely connect to a host via SSH default port 22.
ssh-keygen = Generate SSH key pairs.
scp [file_name] [user_name]@[host]:[remote_path] = Securely copy files between local and remote systems via SSH.
Disk Usage
df -h = Check free and used space on mounted systems.
mount = Show currently mounted file systems.
mount [device_path] [mount_point] = Mount a device.
File Transfer
scp [source_file] [user]@[remote_host]:[destination_path] = Copy a file to a server directory securely.
wget [link] = Download files from FTP or web servers.
curl [link] = Transfer data to or from a server.
File Compression
tar czf [archive.tar.gz] = Create a .gz compressed tar archive.
tar cf [archive.tar] [file/directory] = Archive an existing file or directory.
gzip [file_name], gunzip [file_name.gz] = Compress or decompress .gz files.
unzip [archive.zip] = Extract a zip archive.
Packages
Debian/Ubuntu
sudo apt update = Update package list.
sudo apt upgrade = Upgrade installed packages.
sudo apt install [package_name] = Install an APT package.
sudo apt remove [package_name] = Remove an APT package.
RedHat/CentOS/Fedora
sudo yum update = Update package list and upgrade them.
sudo yum install [package_name] = Install a package.
sudo yum remove [package_name] = Remove a package.
Searching
find [path] -name [search_pattern] = Find files and directories.
grep [search_pattern] [file_name] = Search for a specific pattern in a file.
grep -r [search_pattern] [directory_name] = Recursively search for a pattern in a directory.
grep -i [search_pattern] [file_name] = Case insensitive search.
locate [name] = Locate all files and directories related to a particular name.
awk '[search_pattern] {print $0}' [file_name] = Print all lines matching a pattern in a file.
sed 's/[old_text]/[new_text]/' [file_name] = Find and replace text in a specified file.
find [dir_name] -name [search_pattern] = Find files by name.
Service Management
systemctl start [service_name] = Start a service.
systemctl stop [service_name] = Stop a service.
systemctl restart [service_name] = Restart a service.
systemctl status [service_name] = Check service status.
systemctl enable [service_name] = Enable a service.
systemctl disable [service_name] = Disable a service.
Useful Shell Commands
man [command] = Display a built-in manual for a command.
history = Print the command history used in the terminal.
echo [text] = Display the text.
who = Show who is logged on.