Here’s a comprehensive note on essential Unix commands with examples

mostafa_

Mostafa Shariare

Posted on September 13, 2024

Here’s a comprehensive note on essential Unix commands with examples

1. pwd – Print Working Directory

Displays the current directory you're working in.

   $ pwd
   /home/user
Enter fullscreen mode Exit fullscreen mode

2. ls – List Directory Contents

Lists files and directories in the current directory.

   $ ls
   Documents Downloads Music Pictures
Enter fullscreen mode Exit fullscreen mode

Options:

  • -l: Long listing format.
  • -a: Show hidden files.
   $ ls -la
   drwxr-xr-x  2 user user 4096 Sep 12 12:34 .
   drwxr-xr-x 25 user user 4096 Sep 11 13:14 ..
   -rw-r--r--  1 user user  220 Sep 12 11:32 .bashrc
Enter fullscreen mode Exit fullscreen mode

3. cd – Change Directory

Navigates to a different directory.

   $ cd /home/user/Documents
Enter fullscreen mode Exit fullscreen mode

To go back to the previous directory:

   $ cd -
Enter fullscreen mode Exit fullscreen mode

4. mkdir – Make Directory

Creates a new directory.

   $ mkdir new_folder
Enter fullscreen mode Exit fullscreen mode

5. rmdir – Remove Directory

Deletes an empty directory.

   $ rmdir old_folder
Enter fullscreen mode Exit fullscreen mode

6. rm – Remove Files or Directories

Deletes files or directories.

   $ rm filename.txt
Enter fullscreen mode Exit fullscreen mode

To remove a directory and its contents:

   $ rm -r directory_name
Enter fullscreen mode Exit fullscreen mode

Force deletion without confirmation:

   $ rm -rf directory_name
Enter fullscreen mode Exit fullscreen mode

7. touch – Create a New File

Creates an empty file.

   $ touch file.txt
Enter fullscreen mode Exit fullscreen mode

8. cp – Copy Files or Directories

Copies files or directories.

   $ cp source.txt destination.txt
Enter fullscreen mode Exit fullscreen mode

Copy directories recursively:

   $ cp -r source_directory/ destination_directory/
Enter fullscreen mode Exit fullscreen mode

9. mv – Move (or Rename) Files or Directories

Moves or renames files or directories.

   $ mv oldname.txt newname.txt
Enter fullscreen mode Exit fullscreen mode

To move a file to another directory:

   $ mv file.txt /path/to/directory/
Enter fullscreen mode Exit fullscreen mode

10. cat – Concatenate and Display Files

Displays the content of a file.

   $ cat file.txt
   Hello, World!
Enter fullscreen mode Exit fullscreen mode

Combine multiple files into one:

   $ cat file1.txt file2.txt > merged.txt
Enter fullscreen mode Exit fullscreen mode

11. more and less – View File Content

  • more displays file content page by page.

     $ more file.txt
    
  • less allows scrolling through the file content with less memory usage.

     $ less file.txt
    

12. head and tail – Display File Content

  • head shows the first 10 lines by default.

     $ head file.txt
    
  • tail shows the last 10 lines.

     $ tail file.txt
    

Show specific number of lines:

   $ head -n 5 file.txt
   $ tail -n 5 file.txt
Enter fullscreen mode Exit fullscreen mode

13. find – Search for Files and Directories

Searches files based on name, size, or other properties.

   $ find /path -name "filename.txt"
Enter fullscreen mode Exit fullscreen mode

Find files based on size:

   $ find /path -size +100M
Enter fullscreen mode Exit fullscreen mode

14. grep – Search Inside Files

Searches for a specific pattern within a file.

   $ grep "hello" file.txt
Enter fullscreen mode Exit fullscreen mode

Search recursively in a directory:

   $ grep -r "pattern" /path/to/directory
Enter fullscreen mode Exit fullscreen mode

15. chmod – Change File Permissions

Modify file permissions using symbolic (rwx) or numeric (777) notation.

   $ chmod 755 script.sh
Enter fullscreen mode Exit fullscreen mode

Symbolic example:

   $ chmod u+x script.sh
Enter fullscreen mode Exit fullscreen mode

16. chown – Change File Owner

Changes the ownership of files or directories.

   $ sudo chown user:group file.txt
Enter fullscreen mode Exit fullscreen mode

17. ps – Process Status

Lists running processes.

   $ ps
   PID TTY          TIME CMD
   1234 pts/0    00:00:01 bash
Enter fullscreen mode Exit fullscreen mode

Show all processes:

   $ ps aux
Enter fullscreen mode Exit fullscreen mode

18. kill – Terminate Processes

Terminates a process using its PID (Process ID).

   $ kill 1234
Enter fullscreen mode Exit fullscreen mode

Forcefully terminate:

   $ kill -9 1234
Enter fullscreen mode Exit fullscreen mode

19. top – Real-Time Process Monitoring

Displays real-time information about system processes and resource usage.

   $ top
Enter fullscreen mode Exit fullscreen mode

20. df – Disk Space Usage

Displays disk space usage of file systems.

   $ df -h
   Filesystem      Size  Used Avail Use% Mounted on
   /dev/sda1        50G   15G   32G  32% /
Enter fullscreen mode Exit fullscreen mode

21. du – Disk Usage

Shows the disk usage of files and directories.

   $ du -sh *
   4.0K    file1.txt
   200M    folder
Enter fullscreen mode Exit fullscreen mode

22. tar – Archive Files

Create or extract .tar archives.

   $ tar -cvf archive.tar file1 file2
Enter fullscreen mode Exit fullscreen mode

Extract an archive:

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

Compress with gzip:

   $ tar -czvf archive.tar.gz file1 file2
Enter fullscreen mode Exit fullscreen mode

23. zip and unzip – Compress and Decompress Files

  • zip compresses files into a .zip archive.

     $ zip archive.zip file1 file2
    
  • unzip extracts files from a .zip archive.

     $ unzip archive.zip
    

24. ssh – Secure Shell

Connects to a remote server.

   $ ssh user@remote_server
Enter fullscreen mode Exit fullscreen mode

25. scp – Secure Copy

Copies files between local and remote machines.

   $ scp file.txt user@remote:/path/to/destination/
Enter fullscreen mode Exit fullscreen mode

26. wget – Download Files

Downloads files from the web.

   $ wget https://example.com/file.zip
Enter fullscreen mode Exit fullscreen mode

27. curl – Transfer Data from a URL

Fetches content from a URL.

   $ curl https://example.com
Enter fullscreen mode Exit fullscreen mode

28. history – View Command History

Shows previously executed commands.

   $ history
Enter fullscreen mode Exit fullscreen mode

29. alias – Create Command Aliases

Creates a shortcut for a command.

   $ alias ll='ls -la'
Enter fullscreen mode Exit fullscreen mode

30. echo – Display Text

Displays text or outputs data to a file.

   $ echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode

Redirect output to a file:

   $ echo "Hello, World!" > file.txt
Enter fullscreen mode Exit fullscreen mode

31. man – Manual Pages

Shows the manual or help pages for a command.

   $ man ls
Enter fullscreen mode Exit fullscreen mode

32. exit – Exit the Terminal

Closes the terminal or ends the current session.

   $ exit
Enter fullscreen mode Exit fullscreen mode

33. sudo – Execute Commands as Superuser

Runs commands with root privileges.

   $ sudo apt update
Enter fullscreen mode Exit fullscreen mode

34. apt – Package Manager (For Debian-based systems)

Installs, updates, or removes software packages.

   $ sudo apt install package_name
Enter fullscreen mode Exit fullscreen mode

35. whoami – Display Current User

Shows the username of the current user.

   $ whoami
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mostafa_
Mostafa Shariare

Posted on September 13, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related