5 Shell commands
5.1 What are Shell Commands in Linux?
A shell in Linux is a program that serves as an interface between the user and the operating
system. It accepts commands from the user, interprets them, and passes them to the
operating system for execution. The commands can be used for a wide range of tasks,
from file manipulation to system management.
Some of the essential basic shell commands in Linux for different operations are:
File Management -> cp, mv, rm, mkdir
Navigation -> cd, pwd, ls
Text Processing -> cat, grep, sort, head
System Monitoring -> top, ps, df
Permissions and Ownership -> chmod, chown, chgrp
Networking – > ping, wget, curl, ssh, scp, ftp
Compression and Archiving – > tar, gzip, gunzip, zip, unzip
Package Management – > dnf, yum, apt-get
Process Management -> kill, killall, bg, killall, kill
5.2 Basic Shell Commands for File and Directory
Management
Command Description Example
ls Lists files and directories ls
cd Changes the current directory cd
/home/user/Documents
pwd Displays the current directory path pwd
mkdir Creates a new directory mkdir new_directory
rm Removes files or directories rm file.txt
cp Copies files or directories cp file1.txt file2.txt
mv Moves or renames files and directories mv old_name new_name
touch Creates an empty file or updates file touch newfile.txt
timestamps
5.2.1 Examples:
1. List files in a directory:
ls
2. Change directory:
cd/home/user
3. Create a new directory:
mkdir new_directory
4. Copy a file from one location to another:
cp source.txt destination.txt
5. Remove a file:
rm file.txt
5.3 Text Processing Commands in Linux
Command Description Example
cat Displays the contents of a file cat file.txt
grep Searches for a pattern in a file grep "error" log.txt
sort Sorts the contents of a file sort file.txt
head Displays the first few lines of a file head file.txt
tail Displays the last few lines of a file tail file.txt
wc Counts the lines, words, and characters in a file wc file.txt
5.3.1 Examples:
1. Display the contents of a file:
cat file.txt
2. Search for a pattern in a file:
grep "error" file.txt
3. Sort the contents of a file:
sort file.txt
4. Display the first 10 lines of a file:
head file.txt
5. Display the last 10 lines of a file:
tail file.txt
5.4 File Permissions and Ownership Commands
Command Description Example
chmod Changes file permissions chmod 755 file.txt
chown Changes file owner and group chown user:group file.txt
chgrp Changes file group ownership chgrp group file.txt
5.4.1 Examples:
1. Change permissions of a file:
chmod 755 file.txt
2. Change the owner of a file:
chown user:group file.txt
5.5 System Monitoring and Process Management
Commands
Command Description Example
top Displays real-time system information (CPU, memory) top
ps Displays the list of running processes ps aux
kill Terminates a process by its ID kill 1234
df Displays disk space usage df -h
5.5.1 Examples:
1. View running processes:
ps aux
2. Display real-time system statistics:
top
3. Kill a process by its ID:
kill 1234
4. Check disk space usage:
df -h
5.6 Networking Shell Commands
Command Description Example
ping Checks the network connection to ping example.com
a server
wget Retrieves files from the web wget http://example.com/file.zip
curl Transfers data from or to a server curl http://example.com
ssh Opens SSH client (remote login ssh
[email protected] program)
scp Securely copies files between scp file.txt
hosts
[email protected]:/path/
ftp Transfers files using the File ftp ftp.example.com
Transfer Protocol
5.6.1 Examples
1. Check the network connection to a server:
Command: ping
Example: ping example.com
2. Retrieve files from the web:
Command: wget
Example: wget http://example.com/file.zip
3. Transfer data from or to a server:
Command: curl
Example: curl http://example.com
4. Open SSH client (remote login program):
Command: ssh
Example: ssh
[email protected]5. Securely copy files between hosts:
Command: scp
Example: scp file.txt [email protected]:/path/
6. Transfer files using the File Transfer Protocol:
Command: ftp
Example: ftp ftp.example.com