A Beginner's Guide to Key Linux Commands for DevOps
Basic File and Directory Operations
ls
Purpose: List files and directories.
Usage:
ls
Options:
-l
(long format): Provides detailed information.-a
(all): Includes hidden files.
Example:
ls -la
(lists all files, including hidden ones, with detailed info).
cd
Purpose: Change the current working directory.
Usage:
cd [directory]
Example:
cd /var/log
(changes to the/var/log
directory).
pwd
Purpose: Print the current working directory.
Usage:
pwd
Example:
pwd
(displays the full path of the current directory).
cp
Purpose: Copy files or directories.
Usage:
cp [source] [destination]
Options:
-r
(recursive): Copy directories recursively.
Example:
cp file.txt /backup/
(copiesfile.txt
to the/backup/
directory).
mv
Purpose: Move or rename files or directories.
Usage:
mv [source] [destination]
Example:
mv oldname.txt newname.txt
(renamesoldname.txt
tonewname.txt
).
rm
Purpose: Remove files or directories.
Usage:
rm [file]
Options:
-r
(recursive): Remove directories and their contents recursively.-f
(force): Ignore nonexistent files and never prompt.
Example:
rm -r /temp/
(removes the/temp/
directory and its contents).
mkdir
Purpose: Create a new directory.
Usage:
mkdir [directory]
Example:
mkdir newfolder
(creates a new directory namednewfolder
).
rmdir
Purpose: Remove an empty directory.
Usage:
rmdir [directory]
Example:
rmdir oldfolder
(removes the empty directoryoldfolder
).
File Viewing and Editing
cat
Purpose: Concatenate and display file content.
Usage:
cat [file]
Example:
cat file.txt
(displays the content offile.txt
).
less
Purpose: View file content interactively.
Usage:
less [file]
Example:
less largefile.txt
(openslargefile.txt
for interactive viewing).
head
Purpose: Display the beginning of a file.
Usage:
head [file]
Options:
-n [number]
: Show the firstnumber
lines.
Example:
head -n 10 file.txt
(shows the first 10 lines offile.txt
).
tail
Purpose: Display the end of a file.
Usage:
tail [file]
Options:
-n [number]
: Show the lastnumber
lines.
Example:
tail -n 10 file.txt
(shows the last 10 lines offile.txt
).
nano
/vim
/vi
Purpose: Text editors for modifying files.
Usage:
nano [file]
,vim [file]
,vi [file]
Example:
nano config.txt
(opensconfig.txt
in the Nano text editor).
File Permissions and Ownership
chmod
chown
Purpose: Change file owner and group.
Usage:
chown [owner:group] [file]
Example:
chown user:group file.txt
(changes owner touser
and group togroup
).
chgrp
Purpose: Change group ownership of a file.
Usage:
chgrp [group] [file]
Example:
chgrp admin file.txt
(changes the group offile.txt
toadmin
).
System Monitoring and Information
top
/htop
Purpose: Display real-time system processes.
Usage:
top
,htop
Example:
top
(shows a real-time view of system processes).
ps
Purpose: Display information about running processes.
Usage:
ps [options]
Options:
-e
or-A
: Show all processes.-f
: Full-format listing.
Example:
ps aux
(shows detailed information about all processes).
df
Purpose: Report file system disk space usage.
Usage:
df [options]
Options:
-h
: Human-readable format (e.g., GB, MB).
Example:
df -h
(shows disk usage in a human-readable format).
du
Purpose: Estimate file and directory space usage.
Usage:
du [options] [directory]
Options:
-h
: Human-readable format.
Example:
du -sh /home/user
(shows total disk usage of/home/user
in a human-readable format).
free
Purpose: Display memory usage.
Usage:
free [options]
Options:
-h
: Human-readable format.
Example:
free -h
(shows memory usage in a human-readable format).
uptime
Purpose: Show how long the system has been running.
Usage:
uptime
Example:
uptime
(displays system uptime along with load averages).
Networking
ping
Purpose: Test network connectivity.
Usage:
ping [host]
Example:
ping
google.com
(tests connectivity togoogle.com
).
ifconfig
/ip
Purpose: Display or configure network interfaces.
Usage:
ifconfig
,ip [options] [address]
Example:
ifconfig
(displays network interfaces and their details).ip a
(shows detailed information about all network interfaces).
netstat
/ss
Purpose: Display network connections, routing tables, and interface statistics.
Usage:
netstat [options]
,ss [options]
Options:
-t
: TCP connections.-u
: UDP connections.
Example:
ss -tuln
(shows all listening TCP and UDP ports).
curl
Purpose: Transfer data from or to a server.
Usage:
curl [options] [URL]
Example:
curl -I
http://example.com
(fetches HTTP headers fromexample.com
).
wget
Purpose: Download files from the web.
Usage:
wget [options] [URL]
Example:
wget
http://example.com/file.zip
(downloadsfile.zip
fromexample.com
).
Package Management
apt-get
/apt
Purpose: Manage packages on Debian-based systems (like Ubuntu).
Usage:
apt-get [options] [command]
,apt [options] [command]
Example:
apt-get install nginx
(installs thenginx
package).
yum
/dnf
Purpose: Manage packages on Red Hat-based systems (like CentOS or Fedora).
Usage:
yum [options] [command]
,dnf [options] [command]
Example:
yum install nginx
(installs thenginx
package).
Archive and Compression
tar
Purpose: Archive files (often used with compression).
Usage:
tar [options] [archive-file] [files]
Options:
-c
: Create a new archive.-x
: Extract an archive.-v
: Verbose output.-f
: Specify archive file.
Example:
tar -cvf archive.tar directory/
(creates a tarball ofdirectory/
).
gzip
/gunzip
Purpose: Compress/decompress files.
Usage:
gzip [file]
,gunzip [file.gz]
Example:
gzip file.txt
(compressesfile.txt
tofile.txt.gz
).
zip
/unzip
Purpose: Compress/decompress files.
Usage:
zip [options] [zip-file] [files]
,unzip [zip-file]
Example:
zip
archive.zip
file1.txt file2.txt
(creates a zip archive containingfile1.txt
andfile2.txt
).
Scripting and Automation
bash
cron
/crontab
Purpose: Schedule tasks to run at specific times.
Usage:
crontab -e
: Edit the cron table for the current user.
Example:
crontab -e
(opens the crontab file for editing).
Logs and Troubleshooting
grep
Purpose: Search for patterns within files.
Usage:
grep [options] [pattern] [file]
Options:
-i
: Ignore case.-r
: Recursively search directories.
Example:
grep -i error /var/log/syslog
(searches for "error" in/var/log/syslog
).
awk
Purpose: Pattern scanning and processing.
Usage:
awk [options] '{pattern}' [file]
Example:
awk '{print $1}' file.txt
(prints the first column offile.txt
).
sed
Purpose: Stream editor for filtering and transforming text.
Usage:
sed [options] 'script' [file]
Example:
sed 's/old/new/g' file.txt
(replaces "old" with "new" infile.txt
).
Others
history
Purpose: View command history.
Usage:
history
Example:
history
(lists previously executed commands).
alias
Purpose: Create shortcuts for commands.
Usage:
alias [name]='[command]'
Example:
alias ll='ls -la'
(creates a shortcutll
forls -la
).
These commands and tools will give you a solid foundation in Linux systems, helping you manage and automate tasks effectively in a DevOps environment.
Subscribe to my newsletter
Read articles from Mohi uddin directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by