Shell Scripting: Basic Commands to just get you start scripting

Basanta Bhusan Khadka - Nov 4 - - Dev Community

Shell scripting is a powerful scripting language that operates within a shell environment, allowing users to automate repetitive tasks, manage files and directories, configure systems, and execute a series of commands in a single script.

Essential Shell Commands: A Practical Guide

Mastering shell commands provides the foundation to harness the power of shell scripting. Here’s a comprehensive guide to the key commands you’ll rely on for efficient file management, system monitoring, text processing, and more.

File and Directory Management

Create a Directory

mkdir new_directory
Enter fullscreen mode Exit fullscreen mode

: Creates a new directory in the current location.

Navigate Directories

cd new_directory
Enter fullscreen mode Exit fullscreen mode

: Changes to the specified directory.

cd ..
Enter fullscreen mode Exit fullscreen mode

: Returns to the previous directory.

List Directory Contents

ls
Enter fullscreen mode Exit fullscreen mode

: Lists files and folders.

ls -l
Enter fullscreen mode Exit fullscreen mode

: Lists detailed information about files (permissions, size, date).

ls -a
Enter fullscreen mode Exit fullscreen mode

: Lists all files, including hidden ones.

Copy Files and Directories

cp source_file.txt destination.txt
Enter fullscreen mode Exit fullscreen mode

: Copies a file to a new location.

cp -r source_directory destination_directory
Enter fullscreen mode Exit fullscreen mode

: Recursively copies an entire directory.

Move or Rename Files

mv oldname.txt newname.txt
Enter fullscreen mode Exit fullscreen mode

: Renames a file.

mv file.txt /path/to/destination/
Enter fullscreen mode Exit fullscreen mode

: Moves a file to a specified directory.

Delete Files and Directories

rm file.txt
Enter fullscreen mode Exit fullscreen mode

: Deletes a file.

rm -r directory/
Enter fullscreen mode Exit fullscreen mode

: Deletes a directory and its contents (use with caution).

File Content Operations
Display File Contents

cat file.txt
Enter fullscreen mode Exit fullscreen mode

: Displays file contents.

head file.txt
Enter fullscreen mode Exit fullscreen mode

: Shows the first 10 lines of a file.

tail file.txt
Enter fullscreen mode Exit fullscreen mode

: Shows the last 10 lines of a file.

Search within Files

grep "search_term" file.txt
Enter fullscreen mode Exit fullscreen mode

: Searches for specific text within a file.

grep -r "search_term" directory/
Enter fullscreen mode Exit fullscreen mode

: Searches within a directory recursively.

Edit a File

vim file.txt
Enter fullscreen mode Exit fullscreen mode

: Opens a file in Vim editor.

nano file.txt
Enter fullscreen mode Exit fullscreen mode

: Opens a file in Nano editor.

File Permissions and Ownership
Set File Permissions

chmod 755 file.txt
Enter fullscreen mode Exit fullscreen mode

: Sets read, write, execute permissions for the owner and read/execute for others.

Change File Ownership

chown user
Enter fullscreen mode Exit fullscreen mode

:group file.txt: Changes file ownership.

System Information and Monitoring
Print Working Directory

pwd
Enter fullscreen mode Exit fullscreen mode

: Shows the current directory path.

Check Disk Usage

df -h
Enter fullscreen mode Exit fullscreen mode

: Displays disk space usage in a human-readable format.

du -h directory/
Enter fullscreen mode Exit fullscreen mode

: Shows the size of a specific directory.

System Information

uname -a
Enter fullscreen mode Exit fullscreen mode

: Provides system information.

top
Enter fullscreen mode Exit fullscreen mode

: Real-time process monitor.

ps -ef
Enter fullscreen mode Exit fullscreen mode

: Lists running processes.

Network and Connectivity Commands
Network Connectivity Check

ping example.com
Enter fullscreen mode Exit fullscreen mode

: Checks if a host is reachable.

View Network Configuration

ifconfig
Enter fullscreen mode Exit fullscreen mode

: Displays network interfaces (use ip a on newer systems).

Compression and Archiving
Create Archives

tar -cvf archive.tar directory/
Enter fullscreen mode Exit fullscreen mode

: Creates a tar archive.

tar -czvf archive.tar.gz directory/
Enter fullscreen mode Exit fullscreen mode

: Creates a compressed (gzip) tar archive.

Extract Archives

tar -xvf archive.tar
Enter fullscreen mode Exit fullscreen mode

: Extracts a tar archive.

tar -xzvf archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

: Extracts a gzip-compressed tar archive.

Advanced Utilities
Output Redirection

command > output.txt
Enter fullscreen mode Exit fullscreen mode

: Redirects output to a file (overwrites).

command >> output.txt
Enter fullscreen mode Exit fullscreen mode

: Appends output to a file.

Command Chaining and Piping

ls -l | grep "search_term"
Enter fullscreen mode Exit fullscreen mode

: Pipes output of one command into another.

Set Environment Variables

export VAR_NAME="value"
Enter fullscreen mode Exit fullscreen mode

: Sets an environment variable.

Command History

history
Enter fullscreen mode Exit fullscreen mode

: Displays command history.

With these essential commands, you’re well-equipped to start scripting effectively in the shell environment. Mastering these commands opens up the potential for automation, efficient file handling, and streamlined system administration through shell scripting.

. .
Terabox Video Player