Table of Contents#
- File and Directory Management Commands
lscdmkdirrmdirrmcpmv
- Text Processing Commands
catgrepsedawk
- System Information Commands
unamedfdutop
- Process Management Commands
pskillpkill
- Networking Commands
pingifconfignetstat
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
-lto get a long - format listing, which includes details like permissions, owner, group, size, and modification time. Use-ato 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 andcd -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
-poption to create parent directories if they don't exist. - Example Usage:
mkdir new_directory: Creates a directory namednew_directoryin the current working directory.mkdir -p project/subproject: Creates theprojectdirectory and its sub - directorysubproject.
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 theempty_directoryif it is empty.
rm#
- Function: Removes files and directories.
- Common Practices: Use with caution as it permanently deletes data.
- Best Practices: Use
-rto remove directories recursively and-fto force deletion without prompting. - Example Usage:
rm file.txt: Removes the filefile.txt.rm -r directory: Removes thedirectoryand all its contents.rm -rf directory: Forces the removal of thedirectoryand 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
-rto copy directories recursively. - Example Usage:
cp file.txt destination/: Copiesfile.txtto thedestinationdirectory.cp -r source_directory destination/: Copies thesource_directoryand all its contents to thedestinationdirectory.
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/: Movesfile.txtto thenew_directory.mv old_name.txt new_name.txt: Renamesold_name.txttonew_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
-nto number the output lines. - Example Usage:
cat file.txt: Displays the contents offile.txt.cat file1.txt file2.txt: Displays the contents offile1.txtfollowed byfile2.txt.cat -n file.txt: Displays the contents offile.txtwith 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
-ifor case - insensitive search and-rto search recursively in directories. - Example Usage:
grep "pattern" file.txt: Searches for the word "pattern" infile.txt.grep -i "pattern" file.txt: Searches for the word "pattern" infile.txtcase - insensitively.grep -r "pattern" directory/: Searches for the word "pattern" recursively in all files in thedirectory.
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
-ioption to edit the file in - place. - Example Usage:
sed 's/old_text/new_text/' file.txt: Replaces the first occurrence ofold_textwithnew_textin each line offile.txt.sed -i 's/old_text/new_text/g' file.txt: Replaces all occurrences ofold_textwithnew_textinfile.txtand 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 infile.txt.awk '$3 > 10 {print $0}' file.txt: Prints all lines infile.txtwhere 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
-ato 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
-hto 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
-hfor human - readable format and-sto show only the total summary. - Example Usage:
du directory/: Displays the disk usage of each sub - directory indirectory.du -sh directory/: Displays the total disk usage ofdirectoryin 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
qto quit thetoputility. - Example Usage:
top: Launches thetoputility, 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
-efto 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 -9as 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
-ioption 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 togoogle.comand 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
ipcommand can be used as an alternative. - Example Usage:
ifconfig eth0: Displays the configuration of theeth0network interface.
netstat#
- Function: Displays network connections, routing tables, interface statistics, etc.
- Common Practices: It helps in diagnosing network - related issues.
- Best Practices: Use
-tulnto 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