Table of Contents[Hide][Show]
Unix is a powerful, multi-user, multi-tasking operating system that has been the foundation for many modern operating systems, including Linux and macOS. One of the key features of Unix is its command-line interface (CLI), which allows users to interact with the system using text-based commands. In this article, we will explore various commands in Unix with examples to help you understand how to navigate, manage files, and perform system operations efficiently.
50 Commands in Unix with Examples
Unix commands are instructions given to the operating system to perform specific tasks. These commands are executed in a shell, which is a command-line interpreter that processes user input and executes commands. The most common Unix shells are the Bourne Shell (sh), the Bourne Again Shell (bash), and the C Shell (csh).
Basic Unix Commands
Let’s start with some basic commands in Unix with examples that every user should know.
1. pwd
– Print Working Directory
The pwd
command displays the current working directory.
Example:
$ pwd /home/user
2. ls
– List Directory Contents
The ls
command lists the files and directories in the current directory.
Example:
$ ls Documents Downloads Music Pictures Videos
You can also use options with ls
to display more information:
ls -l
: Long listing format.ls -a
: Include hidden files.
Example:
$ ls -l total 4 drwxr-xr-x 2 user user 4096 Jan 1 12:34 Documents drwxr-xr-x 2 user user 4096 Jan 1 12:34 Downloads
3. cd
– Change Directory
The cd
command is used to change the current working directory.
Example:
$ cd Documents $ pwd /home/user/Documents
To go back to the previous directory, use cd ..
.
Example:
$ cd .. $ pwd /home/user
4. mkdir
– Make Directory
The mkdir
command creates a new directory.
Example:
$ mkdir new_folder $ ls Documents Downloads Music new_folder Pictures Videos
5. rmdir
– Remove Directory
The rmdir
command removes an empty directory.
Example:
$ rmdir new_folder $ ls Documents Downloads Music Pictures Videos
6. touch
– Create an Empty File
The touch
command creates an empty file or updates the timestamp of an existing file.
Example:
$ touch new_file.txt $ ls Documents Downloads Music new_file.txt Pictures Videos
7. rm
– Remove Files or Directories
The rm
command removes files or directories. Use the -r
option to remove directories recursively.
Example:
$ rm new_file.txt $ ls Documents Downloads Music Pictures Videos
Example with directory:
$ rm -r new_folder
8. cp
– Copy Files or Directories
The cp
command copies files or directories from one location to another.
Example:
$ cp file1.txt file2.txt $ ls file1.txt file2.txt
To copy a directory, use the -r
option:
Example:
$ cp -r dir1 dir2
9. mv
– Move or Rename Files or Directories
The mv
command moves or renames files or directories.
Example (rename):
$ mv file1.txt file3.txt $ ls file3.txt file2.txt
Example (move):
$ mv file3.txt Documents/ $ ls Documents/ file3.txt
10. cat
– Concatenate and Display Files
The cat
command displays the contents of a file.
Example:
$ cat file2.txt This is the content of file2.txt.
You can also concatenate multiple files:
Example:
$ cat file1.txt file2.txt > combined.txt $ cat combined.txt This is the content of file1.txt. This is the content of file2.txt.
11. more
and less
– View File Content Page by Page
The more
and less
commands allow you to view the contents of a file one page at a time.
Example with more
:
$ more large_file.txt
Example with less
:
$ less large_file.txt
The less
command is more advanced and allows you to scroll both forward and backward.
12. head
and tail
– Display the Beginning or End of a File
The head
command displays the first few lines of a file, while the tail
command displays the last few lines.
Example with head
:
$ head -n 5 file.txt
Example with tail
:
$ tail -n 5 file.txt
13. grep
– Search Text Using Patterns
The grep
command searches for patterns within files.
Example:
$ grep "search_term" file.txt
You can also use grep
with regular expressions:
Example:
$ grep "^start" file.txt
14. find
– Search for Files and Directories
The find
command searches for files and directories based on various criteria.
Example:
$ find /home/user -name "*.txt"
This command searches for all .txt
files in the /home/user
directory.
15. chmod
– Change File Permissions
The chmod
command changes the permissions of a file or directory.
Example:
$ chmod 755 script.sh
This command gives the owner read, write, and execute permissions, and others read and execute permissions.
16. chown
– Change File Owner
The chown
command changes the owner of a file or directory.
Example:
$ chown user:group file.txt
This command changes the owner of file.txt
to user
and the group to group
.
17. ps
– Display Active Processes
The ps
command displays information about active processes.
Example:
$ ps aux
This command lists all running processes.
18. kill
– Terminate Processes
The kill
command terminates a process by its process ID (PID).
Example:
$ kill 1234
This command terminates the process with PID 1234
.
19. top
– Display System Activity
The top
command provides a real-time view of system activity, including CPU and memory usage.
Example:
$ top
20. df
– Display Disk Space Usage
The df
command displays the amount of disk space used and available on file systems.
Example:
$ df -h
The -h
option makes the output human-readable.
21. du
– Display Directory Space Usage
The du
command displays the disk usage of files and directories.
Example:
$ du -sh /home/user
The -s
option summarizes the total, and the -h
option makes the output human-readable.
22. tar
– Archive Files
The tar
command is used to create and extract archive files.
Example (create archive):
$ tar -cvf archive.tar /home/user
Example (extract archive):
$ tar -xvf archive.tar
23. gzip
and gunzip
– Compress and Decompress Files
The gzip
command compresses files, and gunzip
decompresses them.
Example (compress):
$ gzip file.txt
Example (decompress):
$ gunzip file.txt.gz
24. ssh
– Secure Shell
The ssh
command is used to securely connect to a remote server.
Example:
$ ssh user@remote_host
25. scp
– Secure Copy
The scp
command is used to securely copy files between local and remote systems.
Example:
$ scp file.txt user@remote_host:/remote/directory
26. wget
– Download Files from the Web
The wget
command downloads files from the web.
Example:
$ wget https://example.com/file.txt
27. curl
– Transfer Data from or to a Server
The curl
command is used to transfer data from or to a server.
Example:
$ curl -O https://example.com/file.txt
28. man
– Display Manual Pages
The man
command displays the manual pages for other commands.
Example:
$ man ls
This command displays the manual page for the ls
command.
29. alias
– Create Command Aliases
The alias
command creates shortcuts for longer commands.
Example:
$ alias ll='ls -la' $ ll
This command creates an alias ll
for ls -la
.
30. history
– Display Command History
The history
command displays the command history.
Example:
$ history
You can also rerun a command from history using !n
, where n
is the command number.
Example:
$ !123
This command reruns command number 123
.
Advanced Unix Commands
Now that we’ve covered the basics, let’s dive into some more advanced commands in Unix with examples.
31. awk
– Pattern Scanning and Processing
The awk
command is a powerful text-processing tool.
Example:
$ awk '{print $1}' file.txt
This command prints the first column of file.txt
.
32. sed
– Stream Editor
The sed
command is used for text substitution and manipulation.
Example:
$ sed 's/old/new/' file.txt
This command replaces the first occurrence of old
with new
in file.txt
.
33. cut
– Remove Sections from Lines of Files
The cut
command is used to extract specific sections from each line of a file.
Example:
$ cut -d':' -f1 /etc/passwd
This command extracts the first field (username) from the /etc/passwd
file.
34. tr
– Translate or Delete Characters
The tr
command translates or deletes characters.
Example:
$ tr 'a-z' 'A-Z' < file.txt
This command converts all lowercase letters in file.txt
to uppercase.
35. sort
– Sort Lines of Text Files
The sort
command sorts lines of text files.
Example:
$ sort file.txt
This command sorts the lines in file.txt
alphabetically.
36. uniq
– Report or Omit Repeated Lines
The uniq
command filters out repeated lines in a file.
Example:
$ uniq file.txt
This command removes duplicate lines from file.txt
.
37. diff
– Compare Files Line by Line
The diff
command compares two files line by line.
Example:
$ diff file1.txt file2.txt
This command shows the differences between file1.txt
and file2.txt
.
38. ln
– Create Links Between Files
The ln
command creates hard or symbolic links between files.
Example (hard link):
$ ln file1.txt file2.txt
Example (symbolic link):
$ ln -s file1.txt file2.txt
39. wc
– Word Count
The wc
command counts lines, words, and characters in a file.
Example:
$ wc file.txt
This command displays the number of lines, words, and characters in file.txt
.
40. tee
– Redirect Output to Multiple Files
The tee
command reads from standard input and writes to standard output and files.
Example:
$ echo "Hello, World!" | tee file1.txt file2.txt
This command writes “Hello, World!” to both file1.txt
and file2.txt
.
41. xargs
– Build and Execute Command Lines
The xargs
command builds and executes command lines from standard input.
Example:
$ find . -name "*.txt" | xargs rm
This command finds all .txt
files and deletes them.
42. nohup
– Run Commands Immune to Hangups
The nohup
command runs another command immune to hangups.
Example:
$ nohup long_running_script.sh &
This command runs long_running_script.sh
in the background, even if the terminal is closed.
43. cron
– Schedule Commands
The cron
command schedules commands to run at specific times.
Example:
$ crontab -e
This command opens the cron table for editing. You can add a line like this to schedule a script:
0 2 * * * /path/to/script.sh
This command runs script.sh
at 2:00 AM every day.
44. at
– Schedule a Command to Run Once
The at
command schedules a command to run once at a specific time.
Example:
$ at 2:00 PM at> /path/to/script.sh at>
This command schedules script.sh
to run at 2:00 PM.
45. watch
– Execute a Command Periodically
The watch
command executes a command repeatedly and displays the output.
Example:
$ watch -n 1 'date'
This command displays the current date and time every second.
46. netstat
– Network Statistics
The netstat
command displays network connections, routing tables, and interface statistics.
Example:
$ netstat -tuln
This command lists all listening ports.
47. ifconfig
– Configure Network Interfaces
The ifconfig
command configures network interfaces.
Example:
$ ifconfig eth0
This command displays the configuration of the eth0
interface.
48. ping
– Test Network Connectivity
The ping
command tests network connectivity to another host.
Example:
$ ping google.com
This command sends ICMP echo requests to google.com
.
49. traceroute
– Trace the Route to a Host
The traceroute
command traces the route packets take to a network host.
Example:
$ traceroute google.com
This command shows the route to google.com
.
50. dig
– DNS Lookup
The dig
command performs DNS lookups.
Example:
$ dig google.com
This command performs a DNS lookup for google.com
.
51. echo
– Display a Line of Text
The echo
command prints text or variables to the terminal.
Example:
$ echo "Hello, World!" Hello, World!
Example with variable:
$ name="Unix" $ echo "Welcome to $name" Welcome to Unix
52. export
– Set Environment Variables
The export
command sets environment variables for the current session.
Example:
$ export PATH=$PATH:/new/directory $ echo $PATH /usr/local/bin:/usr/bin:/new/directory
53. unset
– Remove Environment Variables
The unset
command removes environment variables.
Example:
$ unset PATH $ echo $PATH (No output, as PATH is unset)
54. which
– Locate a Command
The which
command shows the full path of a command.
Example:
$ which ls /bin/ls
55. whereis
– Locate Binary, Source, and Manual Pages
The whereis
command locates the binary, source, and manual pages for a command.
Example:
$ whereis ls ls: /bin/ls /usr/share/man/man1/ls.1.gz
56. file
– Determine File Type
The file
command determines the type of a file.
Example:
$ file script.sh script.sh: Bourne-Again shell script, ASCII text executable
57. stat
– Display File Status
The stat
command displays detailed information about a file or directory.
Example:
$ stat file.txt File: file.txt Size: 1234 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 123456 Links: 1 Access: 2023-10-01 12:34:56.000000000 +0000 Modify: 2023-10-01 12:34:56.000000000 +0000 Change: 2023-10-01 12:34:56.000000000 +0000
58. env
– Display Environment Variables
The env
command displays all environment variables.
Example:
$ env HOME=/home/user USER=user PATH=/usr/local/bin:/usr/bin:/bin
59. passwd
– Change User Password
The passwd
command changes the password of the current user.
Example:
$ passwd Changing password for user. (current) UNIX password: Enter new UNIX password: Retype new UNIX password:
60. su
– Switch User
The su
command switches to another user.
Example:
$ su root Password: # (Now you are the root user)
61. sudo
– Execute Commands as Superuser
The sudo
command allows a permitted user to execute a command as the superuser.
Example:
$ sudo apt-get update [sudo] password for user:
62. shutdown
– Shutdown or Restart the System
The shutdown
command shuts down or restarts the system.
Example (shutdown):
$ sudo shutdown -h now
Example (restart):
$ sudo shutdown -r now
63. reboot
– Restart the System
The reboot
command restarts the system.
Example:
$ sudo reboot
64. who
– Display Logged-in Users
The who
command displays information about currently logged-in users.
Example:
$ who user tty1 2023-10-01 12:34 user pts/0 2023-10-01 12:35 (192.168.1.1)
65. whoami
– Display Current User
The whoami
command displays the username of the current user.
Example:
$ whoami user
66. groups
– Display User Groups
The groups
command displays the groups a user belongs to.
Example:
$ groups user adm cdrom sudo dip plugdev lpadmin sambashare
67. id
– Display User and Group Information
The id
command displays user and group information.
Example:
$ id uid=1000(user) gid=1000(user) groups=1000(user),4(adm),24(cdrom),27(sudo)
68. uname
– Display System Information
The uname
command displays system information.
Example:
$ uname -a Linux hostname 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 GNU/Linux
69. hostname
– Display or Set System Hostname
The hostname
command displays or sets the system’s hostname.
Example:
$ hostname hostname
70. date
– Display or Set the System Date and Time
The date
command displays or sets the system date and time.
Example:
$ date Mon Oct 1 12:34:56 UTC 2023
71. cal
– Display a Calendar
The cal
command displays a calendar.
Example:
$ cal October 2023 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
72. sleep
– Delay for a Specified Time
The sleep
command pauses the execution for a specified time.
Example:
$ sleep 5 (Command pauses for 5 seconds)
73. time
– Measure Command Execution Time
The time
command measures the execution time of a command.
Example:
$ time ls real 0m0.002s user 0m0.001s sys 0m0.001s
74. bc
– Arbitrary Precision Calculator
The bc
command is a calculator for arbitrary precision arithmetic.
Example:
$ echo "5 + 7" | bc 12
75. factor
– Display Prime Factors
The factor
command displays the prime factors of a number.
Example:
$ factor 42 42: 2 3 7
76. zip
and unzip
– Compress and Decompress Files
The zip
command compresses files into a .zip
archive, and unzip
extracts them.
Example (compress):
$ zip archive.zip file1.txt file2.txt
Example (decompress):
$ unzip archive.zip
77. rsync – Synchronize Files and Directories
The rsync
command synchronizes files and directories between two locations.
Example:
$ rsync -av /source/directory /destination/directory
78. mount
– Mount File Systems
The mount
command attaches a file system to a directory.
Example:
$ sudo mount /dev/sdb1 /mnt
79. umount
– Unmount File Systems
The umount
command detaches a file system from a directory.
Example:
$ sudo umount /mnt
80. dd
– Convert and Copy Files
The dd
command is used for low-level copying and conversion of files.
Example (create a bootable USB):
$ sudo dd if=image.iso of=/dev/sdb bs=4M status=progress
81. fdisk
– Partition Table Manipulator
The fdisk
command manipulates disk partition tables.
Example:
$ sudo fdisk -l
82. mkfs
– Create a File System
The mkfs
command creates a file system on a partition.
Example:
$ sudo mkfs.ext4 /dev/sdb1
83. fsck
– File System Consistency Check
The fsck
command checks and repairs file systems.
Example:
$ sudo fsck /dev/sdb1
84. lsof
– List Open Files
The lsof
command lists files opened by processes.
Example:
$ lsof | grep /home/user
85. strace
– Trace System Calls
The strace
command traces system calls and signals.
Example:
$ strace ls
86. nc
(netcat) – Network Utility
The nc
command is a versatile networking tool for reading and writing data across networks.
Example (listen on a port):
$ nc -l 1234
Example (connect to a port):
$ nc localhost 1234
87. nmap
– Network Mapper
The nmap
command is used for network discovery and security auditing.
Example:
$ nmap 192.168.1.1
88. iptables
– Configure Firewall Rules
The iptables
command configures firewall rules.
Example (allow SSH):
$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
89. crontab
– Schedule Tasks
The crontab
command schedules tasks to run at specific times.
Example (edit crontab):
$ crontab -e
Example (list crontab):
$ crontab -l
90. atq
– List Scheduled Jobs
The atq
command lists pending scheduled jobs.
Example:
$ atq
91. atrm
– Remove Scheduled Jobs
The atrm
command removes scheduled jobs.
Example:
$ atrm 1
92. screen
– Terminal Multiplexer
The screen
command allows you to manage multiple terminal sessions.
Example (start a new session):
$ screen
Example (detach from session):
Ctrl+A, D
93. tmux
– Terminal Multiplexer
The tmux
command is another terminal multiplexer.
Example (start a new session):
$ tmux new -s mysession
94. w
– Display Logged-in Users and Their Activities
The w
command shows who is logged in and what they are doing.
Example:
$ w
95. last
– Display Login History
The last
command displays the login history of users.
Example:
$ last
96. finger
– Display User Information
The finger
command displays information about users.
Example:
$ finger user
97. chroot
– Change Root Directory
The chroot
command changes the root directory for a command.
Example:
$ sudo chroot /new/root /bin/bash
98. ldd
– List Shared Library Dependencies
The ldd
command lists shared libraries required by a program.
Example:
$ ldd /bin/ls
99. nm
– List Symbols in Object Files
The nm
command lists symbols in object files.
Example:
$ nm /bin/ls
100. objdump
– Display Object File Information
The objdump
command displays information about object files.
Example:
$ objdump -d /bin/ls
101. strings
– Extract Strings from Files
The strings
command extracts printable strings from files.
Example:
$ strings /bin/ls
102. hexdump
– Display File Contents in Hexadecimal
The hexdump
command displays file contents in hexadecimal format.
Example:
$ hexdump -C file.txt
103. xxd
– Create a Hex Dump
The xxd
command creates a hex dump of a file.
Example:
$ xxd file.txt
104. base64
– Encode/Decode Data
The base64
command encodes or decodes data in Base64 format.
Example (encode):
$ echo "Hello" | base64
Example (decode):
$ echo "SGVsbG8=" | base64 --decode
105. shuf
– Generate Random Permutations
The shuf
command generates random permutations of input lines.
Example:
$ shuf -i 1-10
These commands cover a wide range of functionalities, from system administration to network management and file manipulation. Let me know if you’d like further details or examples!
Conclusion
Unix commands are powerful tools that allow users to interact with the operating system efficiently. By mastering these commands in Unix with examples, you can perform a wide range of tasks, from basic file management to advanced system administration. Whether you’re a beginner or an experienced user, understanding these commands will help you get the most out of your Unix-based system.
Remember, practice is key to becoming proficient with Unix commands. Try out these examples on your own system, and don’t hesitate to explore the manual pages (man
) for more information on each command. With time and experience, you’ll find that the command line is an incredibly powerful and flexible tool for managing your system.