Shell Scripting 101: Essential Commands for Every Developer

Shell Scripting 101: Essential Commands for Every Developer

Diving into the universe of shell scripting? Welcome aboard! Shell scripting is a potent means to automate mundane tasks, string several commands together, and interact dynamically with your system. Here's your beginner-friendly guide to 101 essential shell commands.

Let's dive into 50 shell commands:

The Basics

  1. echo - Display a line of text

    It's one of the simplest commands. It's frequently used in shell scripts to display status or to produce formatted outputs.

    echo [option] [string]

     echo "Hello, World!"
    
    1. man - Manual pages

      If you are a bash scripter, this is the MOST IMPORTANT command you need throughout your journey. Even this blog isn't compared to what help the man command provides us. It is used to display manual pages for commands, giving detailed information on usage.

      man [option] command

       man ls
      
  2. ls - List contents of the directory

    Lists files and directories in the current directory, with options to format or filter the results.

    ls [option] [directory]

     ls -l /home/user
    
  3. cd - Change Directory

    Navigate to a different part of the filesystem.

    cd [directory]

     cd /home/user/documents
    

Working With Files and Directories

  1. touch - Create an empty file

    Generates new files quickly or updates timestamps on existing ones.

    touch [option] filename

     touch sample.txt
    
  2. cp - Copy files or directories

    Duplicate files or directories from one location to another.

    cp [option] source destination

     cp file1.txt file2.txt
    
  3. mv - Move or rename files/directories

    Transfer or rename files and directories.

    mv [option] source destination

     mv oldname.txt newname.txt
    
  4. rm - Remove files or directories

    Delete files or directories. Use with caution; it's irreversible.

    rm [option] file

     rm unwantedfile.txt
    
  5. mkdir - Make directories

    Create new directories.

    mkdir [option] directory

     mkdir new_directory
    
  6. rmdir - Remove empty directories

Delete empty directories.

rmdir [option] directory

rmdir empty_directory

Manipulating Text and Files

  1. cat - Concatenate and display file contents

Read and display text files.

cat [option] file

cat file.txt
  1. grep - Search text using patterns

Hunt for specific patterns in text.

grep [option] pattern [file...]

grep 'hello' file.txt
  1. sed - Stream editor

Powerful tool to parse and modify text in a data stream or file.

sed [option] 'command' file

sed 's/apple/orange/' file.txt

Permissions, Ownerships and Monitoring

  1. chmod - Change file permissions

Adjust permissions on files or directories.

chmod [option] mode file

chmod 755 script.sh
  1. chown - Change file owner and group

Alter the ownership of files or directories.

chown [option] owner[:group] file

chown user:group file.txt
  1. ps - Report process status

Snapshot of current processes.

ps [option]

ps aux
  1. top - Display dynamic real-time processes

Monitor system tasks in real-time.

top [option]

top
  1. kill - Terminate or signal a process

Send signals to specific processes, usually to terminate.

kill [signal or option] pid

kill -9 12345
  1. history - Command history

Display commands recently used.

history [option]

history
  1. find - Search for files in directories

Locate files in the system based on various criteria.

find [path...] [expression]

find /home/user -name "*.txt"

21.pwd - Print Working Directory

Displays the full pathname of the current directory, helping to understand where you are in the filesystem.

pwd [option]

pwd

Compressing and Decompressing files

  1. tar - Archive utility

Combine multiple files into one or extract files from such a combined archive.

tar [option] [file...]

tar -xvf archive.tar
  1. gzip - Compress files

Reduce file sizes.

gzip [option] file

gzip file.txt
  1. gunzip - Decompress files

Decompress .gz files.

gunzip [option] file.gz

gunzip file.txt.gz

Networking

  1. ping - Network diagnostic tool

Check the network connection to a specific IP or domain.

ping [option] destination

ping google.com
  1. netstat - Network statistics

Display network connections, routing tables, and interface statistics.

netstat [option]

netstat -tuln
  1. ifconfig - Display or configure network interfaces

Show or set network interfaces.

ifconfig [interface] [options]

ifconfig eth0
  1. ssh - Secure shell remote login

Connect to remote servers securely.

ssh [option] user@host

ssh user@domain.com
  1. scp - Securely copy files between hosts

Transfer files between local and remote hosts securely.

scp [option] source destination

scp file.txt user@remote.com:/path/
  1. wget - Non-interactive network downloader

Download files from the internet.

wget [option] [URL]

wget http://example.com/file.zip
  1. curl - Command line tool for transferring data

Fetch data from a URL.

curl [option] [URL]

curl -O http://example.com/file.zip
  1. cut - Remove sections from lines of files

Extract and display specific columns from a file's content.

cut OPTION... [FILE]...

cut -f1,3 -d',' data.csv

Displaying Files and contents

  1. head - Output the first part of files

Display the beginning of a file.

head [option] [file...]

head -n 10 file.txt
  1. tail - Output the last part of files

Show the end of a file, often used to display logs.

tail [option] [file...]

tail -f /var/log/syslog
  1. sort - Sort lines of text files

Organize the lines in text files.

sort [option] [file...]

sort file.txt
  1. date - Display or set the system date and time

Show the current date and time or set a new one.

date [option]

date
  1. cal - Display a calendar

Showcase a simple calendar.

cal [option]

cal 12 2023

System Checkup and Reports

  1. df - Report file system disk space usage

Check available disk space.

df [option]

df -h
  1. du - Estimate file and directory space usage

Gauge how much space a directory or file uses.

du [option] [file...]

du -sh /home/user/
  1. alias - Create an alias for a command

Shorten or customize command names.

alias name='command'

alias ll='ls -la'
  1. unalias - Remove an alias

Remove a previously defined alias.

unalias alias_name

unalias ll
  1. which - Shows the full path of commands

Display where a particular program is located.

which [command]

which ls
  1. passwd - Change user password

Modify the password for a user.

passwd [username]

passwd john
  1. wc - Print newline, word, and byte counts for a file

Count the number of lines, words, and bytes.

wc [option] [file...]

wc file.txt
  1. diff - Compare files line by line

Contrast the contents of two files.

diff [option] file1 file2

diff file1.txt file2.txt
  1. tee - Read from standard input and write to standard output and files

Useful to split the output of a command to both display and save in a file simultaneously.

command | tee [option] file

ls | tee output.txt

Running System Jobs

  1. bg - Put jobs in background

Send a process to run in the background.

bg [job_id]

bg %1
  1. fg - Bring jobs to foreground

Retrieve a process to run in the foreground.

fg [job_id]

fg %1
  1. jobs - List active jobs

Display the jobs currently running in the background.

jobs [option]

jobs
  1. clear - Clear the terminal screen

Clean the console display.

clear

clear

Arming yourself with the knowledge of these 50 shell commands will significantly enhance your command line prowess. Remember, the key to mastering them is regular practice. Happy coding!

And that's our detailed guide to 50 foundational shell commands. While it's not all 101 commands as the title says, mastering these will provide a strong foundation for any developer or system administrator. Remember, practice makes perfect. Explore, experiment, and most importantly, enjoy the journey into the world of shell scripting!

Happy scripting!