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
- 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!"
- 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
- 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
- cd - Change Directory
Navigate to a different part of the filesystem.
cd [directory]
cd /home/user/documents
Working With Files and Directories
- touch - Create an empty file
Generates new files quickly or updates timestamps on existing ones.
touch [option] filename
touch sample.txt
- cp - Copy files or directories
Duplicate files or directories from one location to another.
cp [option] source destination
cp file1.txt file2.txt
- mv - Move or rename files/directories
Transfer or rename files and directories.
mv [option] source destination
mv oldname.txt newname.txt
- rm - Remove files or directories
Delete files or directories. Use with caution; it's irreversible.
rm [option] file
rm unwantedfile.txt
- mkdir - Make directories
Create new directories.
mkdir [option] directory
mkdir new_directory
- rmdir - Remove empty directories
Delete empty directories.
rmdir [option] directory
rmdir empty_directory
Manipulating Text and Files
- cat - Concatenate and display file contents
Read and display text files.
cat [option] file
cat file.txt
- grep - Search text using patterns
Hunt for specific patterns in text.
grep [option] pattern [file...]
grep 'hello' file.txt
- 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
- chmod - Change file permissions
Adjust permissions on files or directories.
chmod [option] mode file
chmod 755 script.sh
- chown - Change file owner and group
Alter the ownership of files or directories.
chown [option] owner[:group] file
chown user:group file.txt
- ps - Report process status
Snapshot of current processes.
ps [option]
ps aux
- top - Display dynamic real-time processes
Monitor system tasks in real-time.
top [option]
top
- kill - Terminate or signal a process
Send signals to specific processes, usually to terminate.
kill [signal or option] pid
kill -9 12345
- history - Command history
Display commands recently used.
history [option]
history
- 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
- tar - Archive utility
Combine multiple files into one or extract files from such a combined archive.
tar [option] [file...]
tar -xvf archive.tar
- gzip - Compress files
Reduce file sizes.
gzip [option] file
gzip file.txt
- gunzip - Decompress files
Decompress .gz
files.
gunzip [option] file.gz
gunzip file.txt.gz
Networking
- ping - Network diagnostic tool
Check the network connection to a specific IP or domain.
ping [option] destination
ping google.com
- netstat - Network statistics
Display network connections, routing tables, and interface statistics.
netstat [option]
netstat -tuln
- ifconfig - Display or configure network interfaces
Show or set network interfaces.
ifconfig [interface] [options]
ifconfig eth0
- ssh - Secure shell remote login
Connect to remote servers securely.
ssh [option] user@host
ssh user@domain.com
- 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/
- wget - Non-interactive network downloader
Download files from the internet.
wget [option] [URL]
wget http://example.com/file.zip
- curl - Command line tool for transferring data
Fetch data from a URL.
curl [option] [URL]
curl -O http://example.com/file.zip
- 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
- head - Output the first part of files
Display the beginning of a file.
head [option] [file...]
head -n 10 file.txt
- 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
- sort - Sort lines of text files
Organize the lines in text files.
sort [option] [file...]
sort file.txt
- date - Display or set the system date and time
Show the current date and time or set a new one.
date [option]
date
- cal - Display a calendar
Showcase a simple calendar.
cal [option]
cal 12 2023
System Checkup and Reports
- df - Report file system disk space usage
Check available disk space.
df [option]
df -h
- du - Estimate file and directory space usage
Gauge how much space a directory or file uses.
du [option] [file...]
du -sh /home/user/
- alias - Create an alias for a command
Shorten or customize command names.
alias name='command'
alias ll='ls -la'
- unalias - Remove an alias
Remove a previously defined alias.
unalias alias_name
unalias ll
- which - Shows the full path of commands
Display where a particular program is located.
which [command]
which ls
- passwd - Change user password
Modify the password for a user.
passwd [username]
passwd john
- wc - Print newline, word, and byte counts for a file
Count the number of lines, words, and bytes.
wc [option] [file...]
wc file.txt
- diff - Compare files line by line
Contrast the contents of two files.
diff [option] file1 file2
diff file1.txt file2.txt
- 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
- bg - Put jobs in background
Send a process to run in the background.
bg [job_id]
bg %1
- fg - Bring jobs to foreground
Retrieve a process to run in the foreground.
fg [job_id]
fg %1
- jobs - List active jobs
Display the jobs currently running in the background.
jobs [option]
jobs
- 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!
Top comments (6)
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. :)
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.
Nice post.
However IIRC ifconfig (at lease on ubuntu systems) was being phased out in favor of the ip command.
Happy Coding
It's canon😂
Realy huge post. It's a nice 'glossary commands' to save on our 'utils notes'. Thank you.
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. 😊