Overview of Common Linux Commands
Introduction
Shell commands are the foundational elements of the Unix-like operating system, enabling users to interact with the file system, execute programs, and manage processes. The shell acts as a bridge between human commands and the underlying system, offering a powerful and flexible interface for system administration and development. In this article, we explore the core commands that form the backbone of shell interaction, focusing on their syntax, usage, and practical applications.
Core Commands
1. pwd
Purpose: Print the current working directory.
Syntax: pwd
Example:
pwd
This command outputs the full path of the directory where the current shell session is running.
2. cd
Purpose: Change the current working directory.
Syntax: cd [directory]
Examples:
cd /home/userto navigate to the user's home directory.cd ..to move to the parent directory.cd -to revert to the previous directory.
3. ls
Purpose: List directory contents.
Syntax: ls [options]
Options:
-l: Show detailed information (e.g., file size, permissions).-a: Include hidden files.-h: Human-readable file sizes (e.g.,1Kinstead of1024).
Example:
ls -la
This command displays a list of files and directories in the current directory, including hidden files and detailed metadata.
4. mkdir
Purpose: Create a new directory.
Syntax: mkdir [directory]
Example:
mkdir project
This command creates a new directory named project in the current working directory.
5. rmdir
Purpose: Remove an empty directory.
Syntax: rmdir [directory]
Example:
rmdir project
This command deletes the project directory only if it is empty.
6. cp
Purpose: Copy files or directories.
Syntax: cp [options] source destination
Options:
-i: Confirm before overwriting existing files.-a: Copy entire directories (including metadata).
Example:
cp /etc/config.txt /tmp/
This command copies the config.txt file from /etc to /tmp.
7. mv
Purpose: Move or rename files/directories.
Syntax: mv [options] source destination
Options:
-i: Confirm before overwriting.-n: Overwrite without confirmation.
Example:
mv /home/user/docs reports/
This command moves all files in docs to reports in the user's home directory.
8. rm
Purpose: Remove files or directories.
Syntax: rm [options] file
Options:
-i: Confirm before deletion.-f: Force deletion without prompting.
Example:
rm -f /tmp/data.txt
This command deletes the data.txt file from /tmp without confirmation.
9. echo
Purpose: Print text to the terminal.
Syntax: echo [text]
Example:
echo "Hello, world!"
This command outputs the specified text to the console.
10. exit
Purpose: Terminate the current shell session.
Syntax: exit
Example:
exit
This command exits the shell, closing the session and freeing resources.
Advanced Commands
11. clear
Purpose: Clear the terminal screen.
Syntax: clear
Example:
clear
This command resets the terminal display, removing all previous output.
12. less
Purpose: View the contents of a file line by line.
Syntax: less [file]
Example:
less config.txt
This command allows users to navigate through the contents of config.txt using keyboard arrows.
13. cat
Purpose: Concatenate files and display their contents.
Syntax: cat [options] file
Options:
-n: Number the lines.-s: Suppress empty lines.
Example:
cat -n /etc/passwd
This command displays the contents of /etc/passwd with line numbers.
14. grep
Purpose: Search for patterns in files.
Syntax: grep [options] pattern file
Options:
-i: Ignore case sensitivity.-r: Search recursively in directories.
Example:
grep "error" /var/log/syslog
This command searches for the word error in the system log file.
Conclusion
Shell commands are the building blocks of command-line interaction, enabling users to perform tasks efficiently and customize their workflows. The core commands—such as pwd, cd, ls, and cp—provide essential functionality for navigating and manipulating the file system. While advanced commands like grep and less offer powerful search and display capabilities, the foundational commands remain indispensable for daily use. Mastering these commands allows users to harness the full power of the shell environment.