DEV Community

Cover image for Shell Scripting 101: Essential Commands for Every Developer
Abhinav Pandey
Abhinav Pandey

Posted on

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!"


Enter fullscreen mode Exit fullscreen mode
  1. man - Manual pages

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

man [option] command



   man ls


Enter fullscreen mode Exit fullscreen mode
  1. ls - List contents of directory

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

ls [option] [directory]



   ls -l /home/user


Enter fullscreen mode Exit fullscreen mode
  1. cd - Change Directory

Navigate to a different part of the filesystem.

cd [directory]



   cd /home/user/documents


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode
  1. cp - Copy files or directories

Duplicate files or directories from one location to another.

cp [option] source destination



   cp file1.txt file2.txt


Enter fullscreen mode Exit fullscreen mode
  1. mv - Move or rename files/directories

Transfer or rename files and directories.

mv [option] source destination



   mv oldname.txt newname.txt


Enter fullscreen mode Exit fullscreen mode
  1. rm - Remove files or directories

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

rm [option] file



   rm unwantedfile.txt


Enter fullscreen mode Exit fullscreen mode
  1. mkdir - Make directories

Create new directories.

mkdir [option] directory



   mkdir new_directory


Enter fullscreen mode Exit fullscreen mode
  1. rmdir - Remove empty directories

Delete empty directories.

rmdir [option] directory



   rmdir empty_directory


Enter fullscreen mode Exit fullscreen mode

Manipulating Text and Files

  1. cat - Concatenate and display file contents

Read and display text files.

cat [option] file



   cat file.txt


Enter fullscreen mode Exit fullscreen mode
  1. grep - Search text using patterns

Hunt for specific patterns in text.

grep [option] pattern [file...]



   grep 'hello' file.txt


Enter fullscreen mode Exit fullscreen mode
  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


Enter fullscreen mode Exit fullscreen mode

Permissions, Ownerships and Monitoring

  1. chmod - Change file permissions

Adjust permissions on files or directories.

chmod [option] mode file



   chmod 755 script.sh


Enter fullscreen mode Exit fullscreen mode
  1. chown - Change file owner and group

Alter the ownership of files or directories.

chown [option] owner[:group] file



   chown user:group file.txt


Enter fullscreen mode Exit fullscreen mode
  1. ps - Report process status

Snapshot of current processes.

ps [option]



   ps aux


Enter fullscreen mode Exit fullscreen mode
  1. top - Display dynamic real-time processes

Monitor system tasks in real-time.

top [option]



   top


Enter fullscreen mode Exit fullscreen mode
  1. kill - Terminate or signal a process

Send signals to specific processes, usually to terminate.

kill [signal or option] pid



   kill -9 12345


Enter fullscreen mode Exit fullscreen mode
  1. history - Command history

Display commands recently used.

history [option]



   history


Enter fullscreen mode Exit fullscreen mode
  1. find - Search for files in directories

Locate files in the system based on various criteria.

find [path...] [expression]



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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode
  1. gzip - Compress files

Reduce file sizes.

gzip [option] file



   gzip file.txt


Enter fullscreen mode Exit fullscreen mode
  1. gunzip - Decompress files

Decompress .gz files.

gunzip [option] file.gz



   gunzip file.txt.gz


Enter fullscreen mode Exit fullscreen mode

Networking

  1. ping - Network diagnostic tool

Check the network connection to a specific IP or domain.

ping [option] destination



   ping google.com


Enter fullscreen mode Exit fullscreen mode
  1. netstat - Network statistics

Display network connections, routing tables, and interface statistics.

netstat [option]



   netstat -tuln


Enter fullscreen mode Exit fullscreen mode
  1. ifconfig - Display or configure network interfaces

Show or set network interfaces.

ifconfig [interface] [options]



   ifconfig eth0


Enter fullscreen mode Exit fullscreen mode
  1. ssh - Secure shell remote login

Connect to remote servers securely.

ssh [option] user@host



   ssh user@domain.com


Enter fullscreen mode Exit fullscreen mode
  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/


Enter fullscreen mode Exit fullscreen mode
  1. wget - Non-interactive network downloader

Download files from the internet.

wget [option] [URL]



   wget http://example.com/file.zip


Enter fullscreen mode Exit fullscreen mode
  1. curl - Command line tool for transferring data

Fetch data from a URL.

curl [option] [URL]



   curl -O http://example.com/file.zip


Enter fullscreen mode Exit fullscreen mode
  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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode
  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


Enter fullscreen mode Exit fullscreen mode
  1. sort - Sort lines of text files

Organize the lines in text files.

sort [option] [file...]



   sort file.txt


Enter fullscreen mode Exit fullscreen mode
  1. date - Display or set the system date and time

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

date [option]



   date


Enter fullscreen mode Exit fullscreen mode
  1. cal - Display a calendar

Showcase a simple calendar.

cal [option]



   cal 12 2023


Enter fullscreen mode Exit fullscreen mode

System Checkup and Reports

  1. df - Report file system disk space usage

Check available disk space.

df [option]



   df -h


Enter fullscreen mode Exit fullscreen mode
  1. du - Estimate file and directory space usage

Gauge how much space a directory or file uses.

du [option] [file...]



   du -sh /home/user/


Enter fullscreen mode Exit fullscreen mode
  1. alias - Create an alias for a command

Shorten or customize command names.

alias name='command'



   alias ll='ls -la'


Enter fullscreen mode Exit fullscreen mode
  1. unalias - Remove an alias

Remove a previously defined alias.

unalias alias_name



   unalias ll


Enter fullscreen mode Exit fullscreen mode
  1. which - Shows the full path of commands

Display where a particular program is located.

which [command]



   which ls


Enter fullscreen mode Exit fullscreen mode
  1. passwd - Change user password

Modify the password for a user.

passwd [username]



   passwd john


Enter fullscreen mode Exit fullscreen mode
  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


Enter fullscreen mode Exit fullscreen mode
  1. diff - Compare files line by line

Contrast the contents of two files.

diff [option] file1 file2



   diff file1.txt file2.txt


Enter fullscreen mode Exit fullscreen mode
  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


Enter fullscreen mode Exit fullscreen mode

Running System Jobs

  1. bg - Put jobs in background

Send a process to run in the background.

bg [job_id]



   bg %1


Enter fullscreen mode Exit fullscreen mode
  1. fg - Bring jobs to foreground

Retrieve a process to run in the foreground.

fg [job_id]



   fg %1


Enter fullscreen mode Exit fullscreen mode
  1. jobs - List active jobs

Display the jobs currently running in the background.

jobs [option]



   jobs


Enter fullscreen mode Exit fullscreen mode
  1. clear - Clear the terminal screen

Clean the console display.

clear



   clear


Enter fullscreen mode Exit fullscreen mode

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!

Top comments (6)

Collapse
 
apetryla profile image
Aidas Petryla

Great post, thanks!

From my experience, it's crucial to find ways/strategy to practice them to learn these in depth.

To give some examples, it could be some CTF, personal projects, taking related initiatives at work etc.

Simple reading through or doing an exercise or two in my experience doesn't work that well. :)

Collapse
 
devrx profile image
Abhinav Pandey

Exactly. I've been solving CTFs on picoCTF for a long time. Hence, I can confirm this as well. Brainstorming is the only way to learn things the right way.

Collapse
 
cjsmocjsmo profile image
Charlie J Smotherman

Nice post.

However IIRC ifconfig (at lease on ubuntu systems) was being phased out in favor of the ip command.

Happy Coding

Collapse
 
devrx profile image
Abhinav Pandey

It's canon๐Ÿ˜‚

Collapse
 
robsongrangeiro profile image
Robson Grangeiro

Realy huge post. It's a nice 'glossary commands' to save on our 'utils notes'. Thank you.

Collapse
 
devrx profile image
Abhinav Pandey

Haha thanks a lot. Being a bash scripter as a beginner, I had faced many difficulties browsing through various websites for other commands. That's the reason I though to store them all at once for our future bash scripters. Anyways, thanks for your support. ๐Ÿ˜Š