Shell scripting is a powerful scripting language that operates within a shell environment, allowing users to automate repetitive tasks, manage files and directories, configure systems, and execute a series of commands in a single script.
Essential Shell Commands: A Practical Guide
Mastering shell commands provides the foundation to harness the power of shell scripting. Here’s a comprehensive guide to the key commands you’ll rely on for efficient file management, system monitoring, text processing, and more.
File and Directory Management
Create a Directory
mkdir new_directory
: Creates a new directory in the current location.
Navigate Directories
cd new_directory
: Changes to the specified directory.
cd ..
: Returns to the previous directory.
List Directory Contents
ls
: Lists files and folders.
ls -l
: Lists detailed information about files (permissions, size, date).
ls -a
: Lists all files, including hidden ones.
Copy Files and Directories
cp source_file.txt destination.txt
: Copies a file to a new location.
cp -r source_directory destination_directory
: Recursively copies an entire directory.
Move or Rename Files
mv oldname.txt newname.txt
: Renames a file.
mv file.txt /path/to/destination/
: Moves a file to a specified directory.
Delete Files and Directories
rm file.txt
: Deletes a file.
rm -r directory/
: Deletes a directory and its contents (use with caution).
File Content Operations
Display File Contents
cat file.txt
: Displays file contents.
head file.txt
: Shows the first 10 lines of a file.
tail file.txt
: Shows the last 10 lines of a file.
Search within Files
grep "search_term" file.txt
: Searches for specific text within a file.
grep -r "search_term" directory/
: Searches within a directory recursively.
Edit a File
vim file.txt
: Opens a file in Vim editor.
nano file.txt
: Opens a file in Nano editor.
File Permissions and Ownership
Set File Permissions
chmod 755 file.txt
: Sets read, write, execute permissions for the owner and read/execute for others.
Change File Ownership
chown user
:group file.txt: Changes file ownership.
System Information and Monitoring
Print Working Directory
pwd
: Shows the current directory path.
Check Disk Usage
df -h
: Displays disk space usage in a human-readable format.
du -h directory/
: Shows the size of a specific directory.
System Information
uname -a
: Provides system information.
top
: Real-time process monitor.
ps -ef
: Lists running processes.
Network and Connectivity Commands
Network Connectivity Check
ping example.com
: Checks if a host is reachable.
View Network Configuration
ifconfig
: Displays network interfaces (use ip a on newer systems).
Compression and Archiving
Create Archives
tar -cvf archive.tar directory/
: Creates a tar archive.
tar -czvf archive.tar.gz directory/
: Creates a compressed (gzip) tar archive.
Extract Archives
tar -xvf archive.tar
: Extracts a tar archive.
tar -xzvf archive.tar.gz
: Extracts a gzip-compressed tar archive.
Advanced Utilities
Output Redirection
command > output.txt
: Redirects output to a file (overwrites).
command >> output.txt
: Appends output to a file.
Command Chaining and Piping
ls -l | grep "search_term"
: Pipes output of one command into another.
Set Environment Variables
export VAR_NAME="value"
: Sets an environment variable.
Command History
history
: Displays command history.
With these essential commands, you’re well-equipped to start scripting effectively in the shell environment. Mastering these commands opens up the potential for automation, efficient file handling, and streamlined system administration through shell scripting.
Top comments (1)
very insightful.