Unix Commands Cheat Sheet with Examples

Unix Commands Cheat Sheet with Examples: A Comprehensive Guide

Unix is a powerful, multi-user, multitasking operating system that serves as the backbone for many modern operating systems like Linux and macOS. Mastering Unix commands is essential for anyone working in a Unix-based environment, whether you are a system administrator, developer, or hobbyist. This cheat sheet provides a comprehensive guide to the most commonly used Unix commands cheat sheet with examples to help you get started.

Getting Started Unix Commands Cheat Sheet with Examples

Before diving into the commands, it’s important to open the terminal, the command-line interface used to interact with the Unix operating system. On most Unix systems, you can open the terminal by searching for “Terminal” in the applications menu.

Basic Unix Commands

1. pwd (Print Working Directory)

Displays the current directory you are in.

sh  Copy code
pwd

Example:

sh  Copy code
/home/user

2. ls (List)

Lists files and directories in the current directory.

sh  Copy code
ls

Options:

  • ls -l: Lists files in long format.
  • ls -a: Lists all files, including hidden ones.

Example:

sh  Copy code
ls -l /home/user

3. cd (Change Directory)

Changes the current directory.

sh  Copy code
cd /path/to/directory

Examples:

  • cd ~: Changes to the home directory.
  • cd ..: Moves up one directory level.
sh Copy code
cd /home/user/Documents

4. mkdir (Make Directory)

Creates a new directory.

sh  Copy code
mkdir new_directory

Example:

sh  Copy code
mkdir projects

5. rmdir (Remove Directory)

Deletes an empty directory.

sh  Copy code
rmdir directory_name

Example:

sh  Copy code
rmdir old_directory

6. rm (Remove)

Deletes files or directories.

sh  Copy code
rm file_name

Options:

  • rm -r directory_name: Deletes a directory and its contents recursively.

Example:

sh  Copy code
rm -r unwanted_folder

7. touch

Creates an empty file or updates the timestamp of an existing file.

sh  Copy code
touch new_file

Example:

sh  Copy code
touch example.txt

8. cp (Copy)

Copies files or directories.

sh  Copy code
cp source_file destination_file

Options:

  • cp -r source_directory destination_directory: Copies directories recursively.

Example:

sh  Copy code
cp document.txt /backup/document.txt

9. mv (Move)

Moves or renames files or directories.

sh  Copy code
mv old_name new_name

Example:

sh  Copy code
mv old_name.txt new_name.txt

10. cat (Concatenate)

Displays the contents of a file.

sh  Copy code
cat file_name

Example:

sh  Copy code
cat README.md

11. nano

Opens the Nano text editor to edit files.

sh  Copy code
nano file_name

Example:

sh  Copy code
nano notes.txt

System Information Commands

12. uname

Displays system information.

sh  Copy code
uname -a

Example:

sh  Copy code
uname -a

13. top

Shows real-time system processes and resource usage.

sh  Copy code
top

Example:

sh  Copy code
top

14. df (Disk Free)

Displays disk space usage.

sh  Copy code
df -h

Example:

sh  Copy code
df -h

15. du (Disk Usage)

Shows the size of a directory and its contents.

sh  Copy code
du -sh directory_name

Example:

sh  Copy code
du -sh /home/user

16. free

Displays memory usage.

sh  Copy code
free -h

Example:

sh  Copy code
free -h

File Permissions Commands

17. chmod (Change Mode)

Changes file permissions.

sh  Copy code
chmod 755 file_name

Example:

sh  Copy code
chmod 755 script.sh

18. chown (Change Owner)

Changes file owner and group.

sh  Copy code
chown user:group file_name

Example:

sh  Copy code
chown alice:developers project.zip

Searching and Finding Files

19. find

Searches for files in a directory hierarchy.

sh  Copy code
find /path/to/search -name file_name

Example:

sh  Copy code
find /home/user -name "*.txt"

20. grep (Global Regular Expression Print)

Searches for a pattern within files.

sh  Copy code
grep "pattern" file_name

Options:

  • grep -r “pattern” /path/to/search: Recursively searches within a directory.

Example:

sh  Copy code
grep -r "hello" /home/user

Network Commands

21. ping

Checks connectivity to a host.

sh  Copy code
ping host_name_or_ip

Example:

sh  Copy code
ping google.com

22. ifconfig

Displays network configuration.

sh  Copy code
ifconfig

Example:

sh  Copy code
ifconfig

23. wget

Downloads files from the internet.

sh  Copy code
wget http://example.com/file

Example:

sh  Copy code
wget http://example.com/sample.zip

24. ssh (Secure Shell)

Connects to a remote server securely.

sh  Copy code
ssh user@host

Example:

sh  Copy code
ssh [email protected]

Process Management Commands

25. ps (Process Status)

Displays currently running processes.

sh  Copy code
ps aux

Example:

sh  Copy code
ps aux | grep firefox

26. kill

Terminates a process by PID (Process ID).

sh  Copy code
kill PID

Options:

  • kill -9 PID: Forcefully terminates a process.

Example:

sh  Copy code
kill 1234

Package Management Commands

27. apt-get

Manages packages on Debian-based systems (like Ubuntu).

sh  Copy code
sudo apt-get update
sudo apt-get install package_name

Example:

sh  Copy code
sudo apt-get update
sudo apt-get install git

28. yum

Manages packages on Red Hat-based systems (like CentOS).

sh  Copy code
sudo yum update
sudo yum install package_name

Example:

sh  Copy code
sudo yum update
sudo yum install vim

29. dpkg

Manages Debian packages.

sh  Copy code
sudo dpkg -i package_name.deb

Example:

sh  Copy code
sudo dpkg -i package.deb

30. rpm

Manages Red Hat packages.

sh  Copy code
sudo rpm -i package_name.rpm

Example:

sh  Copy code
sudo rpm -i package.rpm

Text Processing Commands

31. head

Displays the first few lines of a file.

sh  Copy code
head file_name

Example:

sh  Copy code
head -n 10 log.txt

32. tail

Displays the last few lines of a file.

sh  Copy code
tail file_name

Options:

  • tail -f file_name: Follows the file, displaying new lines as they are added.

Example:

sh  Copy code
tail -f log.txt

33. wc (Word Count)

Counts lines, words, and characters in a file.

sh  Copy code
wc file_name

Example:

sh  Copy code
wc -l document.txt

34. sort

Sorts lines of text files.

sh  Copy code
sort file_name

Example:

sh  Copy code
sort names.txt

35. uniq

Removes duplicate lines from a sorted file.

sh  Copy code
uniq file_name

Example:

sh  Copy code
sort names.txt | uniq

Archiving and Compression Commands

36. tar

Archives files into a tarball.

sh  Copy code
tar -cvf archive_name.tar file_or_directory

Options:

  • tar -xvf archive_name.tar: Extracts a tarball.

Example:

sh  Copy code
tar -cvf backup.tar /home/user

37. gzip

Compresses files.

sh  Copy code
gzip file_name

Example:

sh  Copy code
gzip largefile.txt

38. gunzip

Decompresses files.

sh  Copy code
gunzip file_name.gz

Example:

sh  Copy code
gunzip largefile.txt.gz

User Management Commands

39. adduser

Adds a new user.

sh  Copy code
sudo adduser user_name

Example:

sh  Copy code
sudo adduser bob

40. passwd

Changes a user’s password.

sh  Copy code
passwd user_name

Example:

sh  Copy code
passwd bob

41. usermod

Modifies a user account.

sh  Copy code
sudo usermod -aG group_name user_name

Example:

sh  Copy code
sudo usermod -aG sudo bob

42. deluser

Deletes a user.

sh  Copy code
sudo deluser user_name

Example:

sh  Copy code
sudo deluser bob

43. whoami

Displays the current logged-in user.

sh  Copy code
whoami
Scroll to Top