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:
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 throughout 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
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
cd - Change Directory
Navigate to a different part of the filesystem.
Working With Files and Directories
touch - Create an empty file
Generates new files quickly or updates timestamps on existing ones.
touch [option] filename
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
mkdir - Make directories
Create new directories.
mkdir [option] directory
rmdir - Remove empty directories
Delete empty directories.
rmdir [option] directory
Manipulating Text and Files
- cat - Concatenate and display file contents
Read and display text files.
cat [option] file
- grep - Search text using patterns
Hunt for specific patterns in text.
grep [option] pattern [file...]
grep 'hello' file.txt
- sed - Stream editor
A 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.
- top - Display dynamic real-time processes
Monitor system tasks in real time.
- 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.
- 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.
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
- gunzip - Decompress files
gunzip [option] file.gz
- ping - Network diagnostic tool
Check the network connection to a specific IP or domain.
ping [option] destination
- netstat - Network statistics
Display network connections, routing tables, and interface statistics.
- ifconfig - Display or configure network interfaces
Show or set network interfaces.
ifconfig [interface] [options]
- ssh - Secure shell remote login
Connect to remote servers securely.
ssh [option] user@host
- scp - Securely copy files between hosts
Transfer files between local and remote hosts securely.
scp [option] source destination
scp file.txt firstname.lastname@example.org:/path/
- wget - Non-interactive network downloader
Download files from the internet.
wget [option] [URL]
- 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...]
- date - Display or set the system date and time
Show the current date and time or set a new one.
- cal - Display a calendar
Showcase a simple calendar.
cal 12 2023
System Checkup and Reports
- df - Report file system disk space usage
Check available disk space.
- 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 ll='ls -la'
- unalias - Remove an alias
Remove a previously defined alias.
- which - Shows the full path of commands
Display where a particular program is located.
- passwd - Change user password
Modify the password for a user.
- wc - Print newline, word, and byte counts for a file
Count the number of lines, words, and bytes.
wc [option] [file...]
- 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.
- fg - Bring jobs to foreground
Retrieve a process to run in the foreground.
- jobs - List active jobs
Display the jobs currently running in the background.
- clear - Clear the terminal screen
Clean the console display.
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!