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) Snigdha OS

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) Snigdha OS

Sure bro! I will modify thisโค๏ธ

Collapse
 
lotfijb profile image
Lotfi Jebali

Thanks for sharing

Collapse
 
eshanized profile image
Eshan Roy (eshanized) Snigdha OS

Thanks for appreciating ! Do follow my project on GitHub: github.com/Snigdha-OS โค๏ธ

Collapse
 
kiran_baliga profile image
Kiran Baliga

Nice post!

Collapse
 
eshanized profile image
Eshan Roy (eshanized) Snigdha OS

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

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) Snigdha OS

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
 
abhijeet_waghmare_59c8649 profile image
Abhijeet Waghmare

Nice

Collapse
 
eshanized profile image
Eshan Roy (eshanized) Snigdha OS

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

Collapse
 
eshanized profile image
Eshan Roy (eshanized) Snigdha OS

@schelp I have added your writing also. Checkout! Thanks for contributing! 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.