Shell Script Commands

By: zigmoid
Posted on: 05/13/2014

Essential Shell Script Commands πŸš€

Here’s a list of commonly used Shell (Bash) commands that you’ll need for scripting and automation:


1. File & Directory Operations πŸ“

shCopyEditls                    # List files in a directory
ls -l                 # List with details (permissions, size, etc.)
ls -a                 # List all files, including hidden ones
pwd                   # Print current directory
cd /path/to/dir       # Change directory
mkdir new_folder      # Create a new directory
rmdir empty_folder    # Remove an empty directory
rm file.txt           # Remove a file
rm -r my_folder       # Remove a directory and its contents
touch file.txt        # Create a new empty file
cp file1.txt file2.txt  # Copy a file
mv file1.txt file2.txt  # Move/Rename a file

2. File Content Operations πŸ“

shCopyEditcat file.txt          # Display file content
tac file.txt          # Display file content in reverse
less file.txt         # View file content page by page
head -n 10 file.txt   # Show first 10 lines
tail -n 10 file.txt   # Show last 10 lines
tail -f logfile.log   # Continuously display new lines in a log file
grep "word" file.txt  # Search for a word in a file
sed 's/old/new/g' file.txt  # Replace text in a file
awk '{print $1}' file.txt   # Print first column from a file

3. File Permissions & Ownership πŸ”

shCopyEditchmod 755 script.sh   # Change file permissions (owner rwx, others r-x)
chmod +x script.sh    # Make a script executable
chown user:group file.txt  # Change file owner and group
chown -R user:group folder  # Change ownership of a directory recursively

4. Process Management βš™οΈ

shCopyEditps aux               # List all running processes
top                  # Show active processes (press 'q' to exit)
kill -9 <PID>        # Kill a process by ID
pkill process_name   # Kill a process by name
nohup command &      # Run a process in the background
jobs                 # List background jobs
fg %1                # Bring job 1 to the foreground
bg %1                # Resume job 1 in the background

5. Networking Commands 🌐

shCopyEditping google.com        # Check network connection
curl -I google.com     # Get HTTP headers of a website
wget url.com/file.zip  # Download a file
netstat -tulnp         # Show active network connections

6. System Information πŸ“Š

shCopyEdituname -a            # Show system information
df -h               # Show disk usage
du -sh folder       # Show size of a folder
free -m             # Show RAM usage
uptime              # Show system uptime
whoami              # Show current user
who                 # Show logged-in users
history             # Show command history

7. Variable Handling in Shell Scripts πŸ”§

shCopyEditname="John"
echo "Hello, $name"     # Print variable value

8. Loops in Shell Scripts πŸ”„

For Loop

shCopyEditfor i in 1 2 3 4 5; do
  echo "Number $i"
done

While Loop

shCopyEditcounter=1
while [ $counter -le 5 ]; do
  echo "Count: $counter"
  ((counter++))
done

9. Conditional Statements βœ…

shCopyEditif [ -f "file.txt" ]; then
  echo "File exists"
else
  echo "File does not exist"
fi

10. Reading User Input ⌨️

shCopyEditecho "Enter your name:"
read user_name
echo "Hello, $user_name!"

11. Background Jobs & Scheduling Tasks ⏰

shCopyEditcommand &           # Run command in background
crontab -e          # Edit cron jobs
crontab -l          # List cron jobs

Example cron job (Run script every day at 5 AM):

shCopyEdit0 5 * * * /path/to/script.sh

12. Archiving & Compression πŸ“¦

shCopyEdittar -cvf archive.tar folder  # Create tar archive
tar -xvf archive.tar         # Extract tar archive
tar -czvf archive.tar.gz folder  # Create compressed tar archive
tar -xzvf archive.tar.gz     # Extract compressed tar archive
zip -r archive.zip folder    # Create zip archive
unzip archive.zip            # Extract zip archive

13. Disk Management πŸ’Ύ

shCopyEditdf -h               # Show disk usage
du -sh *            # Show size of each file/folder in current directory
fdisk -l            # Show disk partitions

14. String Operations in Shell βœ‚οΈ

shCopyEditstr="Hello World"
echo ${str:0:5}    # Output: Hello
echo ${str:6}      # Output: World
echo ${str^^}      # Convert to uppercase
echo ${str,,}      # Convert to lowercase

15. Exit Codes & Error Handling ⚠️

shCopyEditcommand || echo "Command failed!"
command && echo "Command succeeded!"

This covers most shell scripting essentials! πŸš€
Let me know if you need more details or specific scripts! πŸ˜ƒ