Here’s a comprehensive note on essential Unix commands with examples
Mostafa Shariare
Posted on September 13, 2024
1. pwd
– Print Working Directory
Displays the current directory you're working in.
$ pwd
/home/user
2. ls
– List Directory Contents
Lists files and directories in the current directory.
$ ls
Documents Downloads Music Pictures
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
3. cd
– Change Directory
Navigates to a different directory.
$ cd /home/user/Documents
To go back to the previous directory:
$ cd -
4. mkdir
– Make Directory
Creates a new directory.
$ mkdir new_folder
5. rmdir
– Remove Directory
Deletes an empty directory.
$ rmdir old_folder
6. rm
– Remove Files or Directories
Deletes files or directories.
$ rm filename.txt
To remove a directory and its contents:
$ rm -r directory_name
Force deletion without confirmation:
$ rm -rf directory_name
7. touch
– Create a New File
Creates an empty file.
$ touch file.txt
8. cp
– Copy Files or Directories
Copies files or directories.
$ cp source.txt destination.txt
Copy directories recursively:
$ cp -r source_directory/ destination_directory/
9. mv
– Move (or Rename) Files or Directories
Moves or renames files or directories.
$ mv oldname.txt newname.txt
To move a file to another directory:
$ mv file.txt /path/to/directory/
10. cat
– Concatenate and Display Files
Displays the content of a file.
$ cat file.txt
Hello, World!
Combine multiple files into one:
$ cat file1.txt file2.txt > merged.txt
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
13. find
– Search for Files and Directories
Searches files based on name, size, or other properties.
$ find /path -name "filename.txt"
Find files based on size:
$ find /path -size +100M
14. grep
– Search Inside Files
Searches for a specific pattern within a file.
$ grep "hello" file.txt
Search recursively in a directory:
$ grep -r "pattern" /path/to/directory
15. chmod
– Change File Permissions
Modify file permissions using symbolic (rwx) or numeric (777) notation.
$ chmod 755 script.sh
Symbolic example:
$ chmod u+x script.sh
16. chown
– Change File Owner
Changes the ownership of files or directories.
$ sudo chown user:group file.txt
17. ps
– Process Status
Lists running processes.
$ ps
PID TTY TIME CMD
1234 pts/0 00:00:01 bash
Show all processes:
$ ps aux
18. kill
– Terminate Processes
Terminates a process using its PID (Process ID).
$ kill 1234
Forcefully terminate:
$ kill -9 1234
19. top
– Real-Time Process Monitoring
Displays real-time information about system processes and resource usage.
$ top
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% /
21. du
– Disk Usage
Shows the disk usage of files and directories.
$ du -sh *
4.0K file1.txt
200M folder
22. tar
– Archive Files
Create or extract .tar
archives.
$ tar -cvf archive.tar file1 file2
Extract an archive:
$ tar -xvf archive.tar
Compress with gzip:
$ tar -czvf archive.tar.gz file1 file2
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
25. scp
– Secure Copy
Copies files between local and remote machines.
$ scp file.txt user@remote:/path/to/destination/
26. wget
– Download Files
Downloads files from the web.
$ wget https://example.com/file.zip
27. curl
– Transfer Data from a URL
Fetches content from a URL.
$ curl https://example.com
28. history
– View Command History
Shows previously executed commands.
$ history
29. alias
– Create Command Aliases
Creates a shortcut for a command.
$ alias ll='ls -la'
30. echo
– Display Text
Displays text or outputs data to a file.
$ echo "Hello, World!"
Redirect output to a file:
$ echo "Hello, World!" > file.txt
31. man
– Manual Pages
Shows the manual or help pages for a command.
$ man ls
32. exit
– Exit the Terminal
Closes the terminal or ends the current session.
$ exit
33. sudo
– Execute Commands as Superuser
Runs commands with root privileges.
$ sudo apt update
34. apt
– Package Manager (For Debian-based systems)
Installs, updates, or removes software packages.
$ sudo apt install package_name
35. whoami
– Display Current User
Shows the username of the current user.
$ whoami
Posted on September 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.