The Linux command line and Bash scripting are incredibly versatile tools that can help you automate tasks, manage systems, and become more efficient in your daily workflows. Hereโs a comprehensive guide packed with commands and examples to supercharge your Linux and Bash skills! ๐
-
Change Directory:
cd /path/to/directory
Move to a specific directory.
-
List Files and Directories:
ls -la
List all files and directories, including hidden ones, with detailed information.
-
Show Current Directory:
pwd
Prints the current working directory.
-
Find Files:
find /path -name "filename"
Search for files or directories by name.
-
Create a File:
touch newfile.txt
Creates an empty file.
-
Create a Directory:
mkdir new_directory
Creates a new directory.
-
Copy Files and Directories:
cp file.txt /destination/ cp -r folder /destination/
Copies files or directories.
-
Move or Rename Files:
mv oldname.txt newname.txt mv file.txt /destination/
Moves or renames files.
-
Delete Files and Directories:
rm file.txt rm -r directory/
Deletes files or directories.
-
Display File Content:
cat file.txt
Prints the contents of a file.
-
Paginate Through Content:
less file.txt
View content one page at a time.
-
Show the First/Last Lines:
head file.txt # First 10 lines tail file.txt # Last 10 lines
Displays the first or last lines of a file.
-
View Line Numbers in a File:
nl file.txt
Shows line numbers with the content.
-
Create a new script file:
nano script.sh
-
Add the script content:
#!/bin/bash echo "Hello, World!"
-
Save and make it executable:
chmod +x script.sh
-
Run the script:
./script.sh
-
Using Variables:
name="Linux" echo "Welcome to $name!"
-
Conditional Statements:
if [ -f file.txt ]; then echo "File exists." else echo "File does not exist." fi
-
For Loop:
for i in {1..5}; do echo "Iteration $i" done
-
While Loop:
count=1 while [ $count -le 5 ]; do echo "Count: $count" ((count++)) done
-
Substring Extraction:
text="Hello, Linux!" echo ${text:7:5} # Output: Linux
-
Replace Text:
text="I love cats" echo ${text/cats/dogs} # Output: I love dogs
-
Backup a Directory:
#!/bin/bash tar -czf backup-$(date +%Y-%m-%d).tar.gz /path/to/directory echo "Backup created!"
-
Find and Delete Old Files:
find /path/to/files -type f -mtime +30 -exec rm {} \;
Deletes files older than 30 days.
- Run with Debugging:
Shows each command before it executes.
bash -x script.sh
-
View Running Processes:
ps aux
-
Kill a Process:
kill -9 <PID>
-
Monitor System Usage:
top htop # Enhanced interactive view
-
Check Disk Space:
df -h
-
Check Free Memory:
free -h
-
Test Connectivity:
ping google.com
-
Download a File:
wget https://example.com/file.zip
-
Check Open Ports:
netstat -tuln
-
Find a Keyword:
grep "error" logfile.txt
-
Search Recursively:
grep -r "TODO" .
-
Replace Text Inline:
sed 's/old/new/' file.txt
-
Delete Lines Containing a Pattern:
sed '/pattern/d' file.txt
-
Print Specific Columns:
awk '{print $1, $3}' file.txt
-
Filter Rows:
awk '$3 > 50' data.txt
-
Edit Cron Jobs:
crontab -e
-
Run a Script Daily at Midnight:
0 0 * * * /path/to/script.sh
- Schedule a Task:
at 4:00 PM
-
Encrypt Files:
gpg -c file.txt
-
Check File Integrity:
sha256sum file.txt
-
Set File Permissions:
chmod 600 file.txt
- Dive deeper into tools like
awk
,sed
, andgrep
. - Automate server setups with Bash and tools like Ansible.
- Explore system tuning using
sysctl
. - Learn how to troubleshoot and debug complex Bash scripts.
๐ง With these skills, youโre ready to conquer Linux environments and automate like a pro! ๐