thelinuxvault blog

Essential Unix Commands

Unix is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in the 1970s at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and others. Unix commands are the foundation of interacting with a Unix - like operating system (including Linux and macOS). Mastering these commands can significantly boost your productivity, whether you're a system administrator, a developer, or just someone who wants to have more control over their computer. In this blog, we'll explore some of the most essential Unix commands.

2026-06

Table of Contents#

  1. File and Directory Management Commands
    • ls
    • cd
    • mkdir
    • rmdir
    • rm
    • cp
    • mv
  2. Text Processing Commands
    • cat
    • grep
    • sed
    • awk
  3. System Information Commands
    • uname
    • df
    • du
    • top
  4. Process Management Commands
    • ps
    • kill
    • pkill
  5. Networking Commands
    • ping
    • ifconfig
    • netstat

1. File and Directory Management Commands#

ls#

  • Function: Lists the contents of a directory.
  • Common Practices: By default, it shows the files and directories in the current working directory. You can add options to modify its behavior.
  • Best Practices: Use -l to get a long - format listing, which includes details like permissions, owner, group, size, and modification time. Use -a to show hidden files.
  • Example Usage:
    • ls: Lists the non - hidden files and directories in the current directory.
    • ls -l: Lists files and directories in long - format.
    • ls -la: Lists all files (including hidden ones) in long - format.

cd#

  • Function: Changes the current working directory.
  • Common Practices: You can use relative or absolute paths.
  • Best Practices: Use cd .. to move up one directory level and cd - to go back to the previous working directory.
  • Example Usage:
    • cd /home/user/documents: Changes the working directory to /home/user/documents.
    • cd ..: Moves up one directory level.
    • cd -: Switches back to the previous working directory.

mkdir#

  • Function: Creates a new directory.
  • Common Practices: You can create a single directory or multiple directories at once.
  • Best Practices: Use the -p option to create parent directories if they don't exist.
  • Example Usage:
    • mkdir new_directory: Creates a directory named new_directory in the current working directory.
    • mkdir -p project/subproject: Creates the project directory and its sub - directory subproject.

rmdir#

  • Function: Removes an empty directory.
  • Common Practices: It only works on empty directories.
  • Best Practices: Make sure the directory is empty before using this command.
  • Example Usage:
    • rmdir empty_directory: Removes the empty_directory if it is empty.

rm#

  • Function: Removes files and directories.
  • Common Practices: Use with caution as it permanently deletes data.
  • Best Practices: Use -r to remove directories recursively and -f to force deletion without prompting.
  • Example Usage:
    • rm file.txt: Removes the file file.txt.
    • rm -r directory: Removes the directory and all its contents.
    • rm -rf directory: Forces the removal of the directory and all its contents without prompting.

cp#

  • Function: Copies files and directories.
  • Common Practices: You can copy a single file, multiple files, or directories.
  • Best Practices: Use -r to copy directories recursively.
  • Example Usage:
    • cp file.txt destination/: Copies file.txt to the destination directory.
    • cp -r source_directory destination/: Copies the source_directory and all its contents to the destination directory.

mv#

  • Function: Moves or renames files and directories.
  • Common Practices: If the destination is a different directory, it moves the file; if it's just a new name in the same directory, it renames the file.
  • Best Practices: It's a quick way to organize your files.
  • Example Usage:
    • mv file.txt new_directory/: Moves file.txt to the new_directory.
    • mv old_name.txt new_name.txt: Renames old_name.txt to new_name.txt.

2. Text Processing Commands#

cat#

  • Function: Concatenates and displays the contents of files.
  • Common Practices: It can be used to view the content of a single file or multiple files at once.
  • Best Practices: Use -n to number the output lines.
  • Example Usage:
    • cat file.txt: Displays the contents of file.txt.
    • cat file1.txt file2.txt: Displays the contents of file1.txt followed by file2.txt.
    • cat -n file.txt: Displays the contents of file.txt with line numbers.

grep#

  • Function: Searches for a pattern in a file or input stream.
  • Common Practices: It's very useful for finding specific text in large files.
  • Best Practices: Use -i for case - insensitive search and -r to search recursively in directories.
  • Example Usage:
    • grep "pattern" file.txt: Searches for the word "pattern" in file.txt.
    • grep -i "pattern" file.txt: Searches for the word "pattern" in file.txt case - insensitively.
    • grep -r "pattern" directory/: Searches for the word "pattern" recursively in all files in the directory.

sed#

  • Function: A stream editor used for text transformation.
  • Common Practices: It can perform tasks like replacing text, deleting lines, etc.
  • Best Practices: Use the -i option to edit the file in - place.
  • Example Usage:
    • sed 's/old_text/new_text/' file.txt: Replaces the first occurrence of old_text with new_text in each line of file.txt.
    • sed -i 's/old_text/new_text/g' file.txt: Replaces all occurrences of old_text with new_text in file.txt and saves the changes.

awk#

  • Function: A powerful text - processing tool for data extraction and reporting.
  • Common Practices: It can be used to perform calculations, filter data, etc. based on patterns.
  • Best Practices: Use it when you need to perform complex text processing on tabular data.
  • Example Usage:
    • awk '{print $1}' file.txt: Prints the first field of each line in file.txt.
    • awk '$3 > 10 {print $0}' file.txt: Prints all lines in file.txt where the third field is greater than 10.

3. System Information Commands#

uname#

  • Function: Prints system information.
  • Common Practices: Use different options to get specific information.
  • Best Practices: Use -a to get all available information.
  • Example Usage:
    • uname: Prints the kernel name.
    • uname -a: Prints all system information including kernel name, network node hostname, kernel release, kernel version, machine hardware name, etc.

df#

  • Function: Displays the amount of disk space available on the file system.
  • Common Practices: It helps in monitoring disk usage.
  • Best Practices: Use -h to display the sizes in human - readable format.
  • Example Usage:
    • df: Displays disk usage information.
    • df -h: Displays disk usage information in human - readable format (e.g., KB, MB, GB).

du#

  • Function: Estimates file space usage.
  • Common Practices: Useful for finding out how much space a directory or a set of files is taking.
  • Best Practices: Use -h for human - readable format and -s to show only the total summary.
  • Example Usage:
    • du directory/: Displays the disk usage of each sub - directory in directory.
    • du -sh directory/: Displays the total disk usage of directory in human - readable format.

top#

  • Function: Displays real - time information about system processes and resource usage.
  • Common Practices: It helps in monitoring system performance.
  • Best Practices: Press q to quit the top utility.
  • Example Usage:
    • top: Launches the top utility, showing a dynamic view of the system's processes.

4. Process Management Commands#

ps#

  • Function: Displays information about currently running processes.
  • Common Practices: Use different options to get detailed information.
  • Best Practices: Use -ef to get a full list of processes with detailed information.
  • Example Usage:
    • ps: Displays a simple list of processes associated with the current terminal.
    • ps -ef: Displays a full list of all running processes with information like user, PID, CPU time, etc.

kill#

  • Function: Sends a signal to a process to terminate it.
  • Common Practices: You need to know the process ID (PID) of the process you want to kill.
  • Best Practices: Use kill -9 as a last resort as it force - terminates the process and may cause data loss.
  • Example Usage:
    • kill 1234: Sends the default termination signal to the process with PID 1234.
    • kill -9 1234: Force - terminates the process with PID 1234.

pkill#

  • Function: Kills processes by name instead of PID.
  • Common Practices: It's more convenient when you don't know the PID of the process.
  • Best Practices: Use the -i option for case - insensitive matching.
  • Example Usage:
    • pkill firefox: Kills all Firefox processes.
    • pkill -i chrome: Kills all Chrome processes (case - insensitive).

5. Networking Commands#

ping#

  • Function: Tests the reachability of a host on an Internet Protocol (IP) network.
  • Common Practices: It sends ICMP echo requests to the target host and waits for responses.
  • Best Practices: Use it to check if a network device is online.
  • Example Usage:
    • ping google.com: Sends ICMP echo requests to google.com and displays the response statistics.

ifconfig#

  • Function: Configures network interfaces. It is also used to view network interface settings.
  • Common Practices: You can use it to check the IP address, MAC address, etc. of a network interface.
  • Best Practices: It may be deprecated in some systems, and ip command can be used as an alternative.
  • Example Usage:
    • ifconfig eth0: Displays the configuration of the eth0 network interface.

netstat#

  • Function: Displays network connections, routing tables, interface statistics, etc.
  • Common Practices: It helps in diagnosing network - related issues.
  • Best Practices: Use -tuln to display all TCP and UDP listening ports.
  • Example Usage:
    • netstat -tuln: Displays all TCP and UDP listening ports on the system.

Conclusion#

Unix commands are powerful tools that offer a high level of control and automation on Unix - like operating systems. By mastering these essential commands, you can become more efficient in file management, text processing, system monitoring, process management, and networking tasks. Keep practicing these commands in a safe environment to gain more confidence and proficiency.

References#

  • "The Linux Documentation Project" - https://tldp.org/
  • "Unix and Linux System Administration Handbook" by Evi Nemeth, Garth Snyder, Trent R. Hein, Ben Whaley
  • "Advanced Programming in the Unix Environment" by W. Richard Stevens and Stephen A. Rago