DEV Community

Cover image for 🐧 Linux Command Cheat Sheet: Essential Commands with Examples
Eshan Roy (eshanized) for Snigdha OS

Posted on

🐧 Linux Command Cheat Sheet: Essential Commands with Examples

Linux is a powerful operating system, and its command line is the key to unlocking its full potential. Here's your Linux command cheat sheet with examples and a touch of fun πŸŽ‰ using emojis for easier navigation!


1. πŸ“‚ File and Directory Management

πŸ“œ List Files

ls
Enter fullscreen mode Exit fullscreen mode
  • List all files in a directory.

Example:

ls -la
Enter fullscreen mode Exit fullscreen mode
  • -l: Detailed listing
  • -a: Include hidden files

πŸšͺ Change Directory

cd <directory>
Enter fullscreen mode Exit fullscreen mode

Example:

cd /home/user/Documents
Enter fullscreen mode Exit fullscreen mode
  • Move to the "Documents" directory.

πŸ“ Create a Directory

mkdir <directory_name>
Enter fullscreen mode Exit fullscreen mode

Example:

mkdir my_project
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Remove a File or Directory

To remove a file:

rm <file>
Enter fullscreen mode Exit fullscreen mode

Example:

rm example.txt
Enter fullscreen mode Exit fullscreen mode

To remove a directory:

rm -r <directory>
Enter fullscreen mode Exit fullscreen mode

Example:

rm -r old_directory
Enter fullscreen mode Exit fullscreen mode

πŸ“‹ Copy Files

cp <source> <destination>
Enter fullscreen mode Exit fullscreen mode

Example:

cp file.txt /home/user/backup/
Enter fullscreen mode Exit fullscreen mode

βœ‚οΈ Move or Rename Files

mv <source> <destination>
Enter fullscreen mode Exit fullscreen mode

Example:

mv old_name.txt new_name.txt
Enter fullscreen mode Exit fullscreen mode

2. πŸ“ File Viewing and Editing

πŸ‘€ View File Contents

cat <file>
Enter fullscreen mode Exit fullscreen mode

Example:

cat /etc/passwd
Enter fullscreen mode Exit fullscreen mode

πŸ“– Page Through File

less <file>
Enter fullscreen mode Exit fullscreen mode

Example:

less largefile.log
Enter fullscreen mode Exit fullscreen mode

✏️ Edit Files

nano <file>
Enter fullscreen mode Exit fullscreen mode

Example:

nano notes.txt
Enter fullscreen mode Exit fullscreen mode
  • Open the "notes.txt" file for editing in Nano.

3. πŸ‘₯ User Management

πŸ”„ Switch Users

su <username>
Enter fullscreen mode Exit fullscreen mode

Example:

su root
Enter fullscreen mode Exit fullscreen mode

πŸ‘€ Add a New User

sudo useradd -m <username>
Enter fullscreen mode Exit fullscreen mode

Example:

sudo useradd -m alice
Enter fullscreen mode Exit fullscreen mode

Set a password for the user:

sudo passwd alice
Enter fullscreen mode Exit fullscreen mode

πŸ—‘οΈ Delete a User

sudo userdel <username>
Enter fullscreen mode Exit fullscreen mode

Example:

sudo userdel alice
Enter fullscreen mode Exit fullscreen mode

4. πŸ” Permissions

πŸ› οΈ Change File Permissions

chmod <mode> <file>
Enter fullscreen mode Exit fullscreen mode

Example:

chmod 755 script.sh
Enter fullscreen mode Exit fullscreen mode
  • 755: Owner can read/write/execute; others can read/execute.

πŸ™‹ Change File Ownership

sudo chown <user>:<group> <file>
Enter fullscreen mode Exit fullscreen mode

Example:

sudo chown alice:users file.txt
Enter fullscreen mode Exit fullscreen mode

5. 🌐 Networking

🌍 Check IP Address

ip addr
Enter fullscreen mode Exit fullscreen mode

πŸ“‘ Ping a Host

ping <hostname_or_ip>
Enter fullscreen mode Exit fullscreen mode

Example:

ping google.com
Enter fullscreen mode Exit fullscreen mode

⬇️ Download a File

wget <url>
Enter fullscreen mode Exit fullscreen mode

Example:

wget https://example.com/file.tar.gz
Enter fullscreen mode Exit fullscreen mode

6. πŸ’Ύ Disk and System

πŸ’Ώ Check Disk Space

df -h
Enter fullscreen mode Exit fullscreen mode

πŸ“Š View Disk Usage

du -sh <directory>
Enter fullscreen mode Exit fullscreen mode

Example:

du -sh /var/log
Enter fullscreen mode Exit fullscreen mode

πŸš€ Monitor System Processes

top
Enter fullscreen mode Exit fullscreen mode
  • Real-time view of system processes.

🧠 Check Memory Usage

free -h
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Reboot or Shutdown

Reboot:

sudo reboot
Enter fullscreen mode Exit fullscreen mode

Shutdown:

sudo shutdown now
Enter fullscreen mode Exit fullscreen mode

7. πŸ“¦ Package Management

πŸ“₯ Install a Package (Debian/Ubuntu)

sudo apt install <package>
Enter fullscreen mode Exit fullscreen mode

Example:

sudo apt install curl
Enter fullscreen mode Exit fullscreen mode

🧹 Remove a Package

sudo apt remove <package>
Enter fullscreen mode Exit fullscreen mode

Example:

sudo apt remove apache2
Enter fullscreen mode Exit fullscreen mode

πŸ“ˆ Update System

On Debian/Ubuntu:

sudo apt update && sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

8. πŸ” Searching and Grep

πŸ”Ž Find Files

find <directory> -name "<file_name>"
Enter fullscreen mode Exit fullscreen mode

Example:

find / -name "*.log"
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Search Text in Files

grep "<text>" <file>
Enter fullscreen mode Exit fullscreen mode

Example:

grep "error" /var/log/syslog
Enter fullscreen mode Exit fullscreen mode

9. πŸ“¦ Archiving and Compression

πŸ“¦ Create a Tar Archive

tar -cvf archive.tar <directory>
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Extract a Tar Archive

tar -xvf archive.tar
Enter fullscreen mode Exit fullscreen mode

πŸ’¨ Compress Files

gzip <file>
Enter fullscreen mode Exit fullscreen mode

Example:

gzip logs.txt
Enter fullscreen mode Exit fullscreen mode

10. ⚑ Miscellaneous

πŸ“ Print Working Directory

pwd
Enter fullscreen mode Exit fullscreen mode

⏱️ Check System Uptime

uptime
Enter fullscreen mode Exit fullscreen mode

🧹 Clear Terminal Screen

clear
Enter fullscreen mode Exit fullscreen mode

Suggestions From comments by @schelp :

  • ls
ls -ltrha (to bring the lastest files/dirs that had modifications.
Enter fullscreen mode Exit fullscreen mode
  • cd "", to enter in directories with that had an space into their names, like:
cd "/home/schelp/my files"
ls -ltrha "/home/schelp/my files"
Enter fullscreen mode Exit fullscreen mode
  • df -h (known a little bit more of the filesystem used, maybe its a good idea)
df -Th
Enter fullscreen mode Exit fullscreen mode

One of the shortcuts that I like is: @schelp

  • to clear the terminal screen
crtl + l
Enter fullscreen mode Exit fullscreen mode

Suggestion from comment @xinitd :

  • Change directory ownership recursively:
chown -R user:froup /path/to/directory
Enter fullscreen mode Exit fullscreen mode
  • Just add executable flag to file
chmod +x /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

Conclusion

πŸŽ‰ Mastering these commands will make your Linux journey smooth and efficient. From file management to networking, this cheat sheet covers essential tasks.

Do you have a favorite Linux command that isn’t here? Share it in the comments below! πŸ‘‡

Written by: @eshanized
Contributor 1: @schelp
Contributor 2: @xinitd

Top comments (13)

Collapse
 
xinitd profile image
Shakhzhakhan Maxudbek • Edited

Good job! Here my suggestions:

Changing directory ownership recursively:

chown -R user:group /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Adding executable flag to file:

chmod +x /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode
Collapse
 
eshanized profile image
Eshan Roy (eshanized)

I have added your writing also. Checkout! Thanks for contributing! Do follow my project on GitHub: github.com/Snigdha-OS

Collapse
 
schelp profile image
Schelp

Nice work, dude!
Maybe, you can add some like:

  • ls
ls -ltrha (to bring the lastest files/dirs that had modifications.
Enter fullscreen mode Exit fullscreen mode
  • cd "", to enter in directories with that had an space into their names, like:
cd "/home/schelp/my files"
ls -ltrha "/home/schelp/my files"
Enter fullscreen mode Exit fullscreen mode
  • df -h (known a little bit more of the filesystem used, maybe its a good idea)
df -Th
Enter fullscreen mode Exit fullscreen mode

One of the shortcuts that I like is:

# to clear the terminal screen
crtl + l
Enter fullscreen mode Exit fullscreen mode
Collapse
 
eshanized profile image
Eshan Roy (eshanized)

Sure bro! I will modify this❀️

Collapse
 
fazly_fathhy profile image
Fazly Fathhy

what is the difference between chmod +x /path/to/script.sh and chmod u+x /path/to/script.sh ?

Collapse
 
eshanized profile image
Eshan Roy (eshanized)

chmod +x /path/to/script.sh is used when you want to make the script executable for all users e.g groups and it is equivalent to a+x -a stands for all. on the other side u+x is used when you want to give permission for owner only. in details you can say like chmod +x = rwxr-xr-x and chmod u+x = rwxr--r--. Thank You for the question.

Collapse
 
kiran_baliga profile image
Kiran Baliga

Nice post!

Collapse
 
eshanized profile image
Eshan Roy (eshanized)

Thanks for appreciating ! Do follow my project on GitHub: github.com/Snigdha-OS

Collapse
 
lotfijb profile image
Lotfi Jebali

Thanks for sharing

Collapse
 
eshanized profile image
Eshan Roy (eshanized)

Thanks for appreciating ! Do follow my project on GitHub: github.com/Snigdha-OS ❀️

Collapse
 
eshanized profile image
Eshan Roy (eshanized)

@schelp I have added your writing also. Checkout! Thanks for contributing! Do follow my project on GitHub: github.com/Snigdha-OS

Collapse
 
abhijeet_waghmare_59c8649 profile image
Abhijeet Waghmare

Nice

Collapse
 
eshanized profile image
Eshan Roy (eshanized)

Thanks for appreciating ! Do follow my project on GitHub: github.com/Snigdha-OS

Some comments may only be visible to logged-in visitors. Sign in to view all comments.