DEV Community

Cover image for Linux Commands in DevOps: A Practical Guide with Examples
Osagie Anolu
Osagie Anolu

Posted on

Linux Commands in DevOps: A Practical Guide with Examples

File System Navigation and Management

Basic Directory Operations

1. ls (List Directory Contents)

# Basic listing
ls
Documents Downloads Pictures

# Detailed listing with hidden files
ls -la
total 56
drwxr-xr-x  4 user group  4096 Dec 14 10:00 .
drwxr-xr-x 20 user group  4096 Dec 14 09:55 ..
-rw-r--r--  1 user group   220 Dec 14 09:55 .bash_logout
drwxr-xr-x  3 user group  4096 Dec 14 09:56 Documents

# Sort by size with human-readable format
ls -lSh
total 1.5G
-rw-r--r-- 1 user group 1.2G Dec 14 10:00 large_file.iso
-rw-r--r-- 1 user group 256M Dec 14 09:58 medium_file.zip
Enter fullscreen mode Exit fullscreen mode

2. cd (Change Directory)

# Navigate to home directory
cd ~

# Navigate to parent directory
cd ..

# Navigate to previous directory
cd -
/home/user/Documents

# Navigate to specific path
cd /var/log/nginx
Enter fullscreen mode Exit fullscreen mode

3. pwd (Print Working Directory)

pwd
/home/user/projects/webapp

# Print physical path (resolve symlinks)
pwd -P
/var/www/html
Enter fullscreen mode Exit fullscreen mode

File Operations

4. mkdir (Create Directory)

# Create single directory
mkdir project

# Create multiple nested directories
mkdir -p project/{src,tests,docs}/{lib,bin,data}

# Create directory with specific permissions
mkdir -m 755 secure_folder
Enter fullscreen mode Exit fullscreen mode

5. touch (Create File)

# Create single file
touch document.txt

# Create multiple files
touch file1.txt file2.txt file3.txt

# Update timestamp of existing file
touch -c existing_file.txt
Enter fullscreen mode Exit fullscreen mode

6. cp (Copy)

# Copy single file
cp source.txt destination.txt

# Copy directory recursively
cp -r /source/directory /destination/directory

# Copy with preservation of attributes
cp -a source.txt destination.txt

# Copy with backup
cp -b important.conf important.conf.new
Enter fullscreen mode Exit fullscreen mode

Text Processing and Editing

7. grep (Pattern Searching)

# Basic search
grep "error" log_file.txt
2023-12-14 10:00:01 ERROR Database connection failed

# Recursive search in directory
grep -r "TODO" ./src/
./src/main.js:// TODO: Implement error handling
./src/utils.js:// TODO: Add validation

# Search with context
grep -C 2 "exception" error.log
# Shows 2 lines before and after each match
Enter fullscreen mode Exit fullscreen mode

8. sed (Stream Editor)

# Replace text
sed 's/old/new/g' file.txt

# Delete lines matching pattern
sed '/pattern/d' file.txt

# Add line after match
sed '/pattern/a\New line text' file.txt

# In-place editing
sed -i 's/search/replace/g' file.txt
Enter fullscreen mode Exit fullscreen mode

System Administration

9. systemctl (Service Management)

# Check service status
systemctl status nginx
● nginx.service - A high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service)
   Active: active (running)

# Start/stop service
systemctl start mysql
systemctl stop mysql

# Enable/disable on boot
systemctl enable docker
systemctl disable docker
Enter fullscreen mode Exit fullscreen mode

10. journalctl (Log Viewing)

# View system logs
journalctl
Dec 14 10:00:01 server systemd[1]: Started System Logging Service.

# Follow new log entries
journalctl -f

# View logs for specific service
journalctl -u nginx.service

# View logs since last boot
journalctl -b
Enter fullscreen mode Exit fullscreen mode

Network Operations

11. ss (Socket Statistics)

# Show all TCP connections
ss -t
State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port
ESTAB    0         0             10.0.0.1:ssh             10.0.0.2:52414

# Show listening ports
ss -ltn
State    Recv-Q    Send-Q        Local Address:Port
LISTEN   0         128           0.0.0.0:80
LISTEN   0         128           0.0.0.0:22
Enter fullscreen mode Exit fullscreen mode

12. curl (Data Transfer)

# Basic GET request
curl https://api.example.com/data

# POST request with data
curl -X POST -d "name=value" https://api.example.com/create

# Download file
curl -O https://example.com/file.zip

# With headers
curl -H "Authorization: Bearer token123" https://api.example.com
Enter fullscreen mode Exit fullscreen mode

Process Management

13. ps (Process Status)

# Show all processes
ps aux
USER       PID  %CPU  %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1   0.0   0.1   8904  1444 ?        Ss   Dec13   0:00 /sbin/init

# Process tree
ps auxf

# Custom format
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu
Enter fullscreen mode Exit fullscreen mode

14. top (System Monitor)

top
top - 10:00:01 up 7 days, 2:12, 1 user, load average: 0.08, 0.03, 0.01
Tasks: 128 total,   1 running, 127 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.7 us,  0.3 sy,  0.0 ni, 98.9 id,  0.0 wa,  0.0 hi,  0.0 si
MiB Mem :   3919.1 total,    815.9 free,   1817.6 used,   1285.6 buff/cache
Enter fullscreen mode Exit fullscreen mode

File Permissions and Ownership

15. chmod (Change Permissions)

# Add execute permission
chmod +x script.sh

# Set specific permissions
chmod 755 file.txt

# Recursive permission change
chmod -R 644 ./public_html/
Enter fullscreen mode Exit fullscreen mode

16. chown (Change Owner)

# Change owner
chown user:group file.txt

# Recursive ownership change
chown -R www-data:www-data /var/www/html/
Enter fullscreen mode Exit fullscreen mode

Advanced Text Processing

17. awk (Text Processing)

# Print specific columns
awk '{print $1, $3}' file.txt

# Sum numbers in column
awk '{sum += $1} END {print sum}' numbers.txt

# Filter lines
awk '$3 > 100 {print $0}' data.txt
Enter fullscreen mode Exit fullscreen mode

Archive Management

18. tar (Archive Files)

# Create archive
tar -czf archive.tar.gz directory/

# Extract archive
tar -xzf archive.tar.gz

# List contents
tar -tvf archive.tar.gz
Enter fullscreen mode Exit fullscreen mode

SSH Operations

19. ssh-keygen (Key Generation)

# Generate key pair
ssh-keygen -t rsa -b 4096 -C "user@example.com"

# Generate key with specific file name
ssh-keygen -f ./keys/deploy_key

# Add key to agent
ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

File System Operations

20. df (Disk Space)

# Show disk usage
df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        20G   15G  4.0G  79% /
/dev/sdb1       100G   70G   30G  70% /data

# Show file system type
df -T
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate common usage patterns in DevOps environments. Each command can be further customized with additional options and flags for specific use cases.

Remember to:

  1. Always test commands in a safe environment first
  2. Use proper permissions and security practices
  3. Document complex commands and their purposes
  4. Create aliases for frequently used complex commands
  5. Consider impact on system resources for heavy operations

For production environments, it's recommended to:

  • Create scripts for repetitive tasks
  • Use version control for configuration files
  • Implement proper logging and monitoring
  • Follow security best practices
  • Document all custom implementations

Top comments (0)