Operations 23 min read

Essential Linux Command Cheat Sheet: From File Management to SELinux

This comprehensive guide compiles essential Linux command-line techniques, covering command connectors, system resource monitoring, file searching, editing with vi/Vim, file transfer, text processing tools, user and file management, NFS setup, scripting utilities, and SELinux policy management, providing practical examples and command syntax for system administrators.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Essential Linux Command Cheat Sheet: From File Management to SELinux

Command Connectors

Semicolon ; runs commands sequentially. Ampersand & runs the following command regardless of the previous command’s exit status. Double ampersand && runs the next command only if the previous command succeeded (exit status 0). Pipe | passes the standard output of the left command to the standard input of the right command. Double pipe || runs the right‑hand command only when the left command fails (non‑zero exit status).

System Resource Monitoring

df – Disk Free

Shows filesystem space usage, including space held by deleted files that are still open by processes.

du – Disk Usage

Example to list the size of each entry in the current directory and sort it descending:

du -sh * | sort -nr

ps – Process Status

List processes and filter, e.g.: ps -ef | grep sync Use grep -v grep to exclude the grep process itself.

Vim Editing Modes and Search‑Replace

Modes

Command mode : press ESC to enter; commands are not echoed.

Insert mode : i, a, o, c, r, s start text insertion.

Ex (last‑line) mode : press : to enter the command line, e.g. wq, q!.

Search and replace

General syntax:

:<range> s/<search_pattern>/<replace_string>/<flags>
range

– lines to operate on ( % for whole file, 200,250 for specific lines). search_pattern – regular expression to find. replace_string – replacement text. flagsg global, c confirm, [...c (or [...i) for case‑insensitive.

Examples:

:%s/python/Python3/g
:200,250 s/python/Python3/g
:%s/python\|py/Python3/g

File Search Commands

grep

# Search a specific file
grep 'string' filename

# Search all files in a directory
grep 'string' dirPath/*

# Recursive search
grep -r 'string' ./

find

# Find a file by name
find / -name install.log

# Files modified more than 10 days ago
find . -mtime +10

# Files larger than 25 KB
find . -size 25k

# Files between 20 KB and 50 KB and list details
find /etc -size +20k -a -size -50k -exec ls -lh {} \;

whereis & which

# Locate binaries and manuals
whereis command_name   # -b for binaries only, -m for manuals only
which command_name

locate

# Fast filename search using a pre‑built database
locate filename
# Update the database
updatedb

File Transfer

scp

# Download a single file
scp user@host:/remote/file.txt /local/path

# Download a directory recursively
scp -r user@host:/remote/dir /local/dir

# Upload a single file
scp /local/file.txt user@host:/remote/path

# Upload a directory recursively
scp -r /local/dir user@host:/remote/dir

rsync

# Pull a remote directory, excluding specific sub‑directories
rsync -azh -e ssh --exclude 'l_temp' --exclude 'l_tmp/claimImageList' user@host:/remote/dir /local/dir

# Sync with log and delete options
rsync -az /src/dir /dest/dir --log-file=./rsync.log --exclude='unwanted.jar' --delete

sftp

sftp user@host
put local_file remote_path
get remote_path local_path

Linux Text Utilities

sort

Common options include -n (numeric), -r (reverse), -k (key field), etc.

split

split -b 1024M large.tar.gz part_
cat part_* > large.tar.gz
md5sum large.tar.gz

Mount / Unmount and NFS

Mount utilities

# Show mounted devices
mount

# Mount all entries from /etc/fstab
mount -a

# Mount a specific filesystem
mount -t ext4 /dev/sdb1 /mnt/data

# Unmount
umount /dev/sdb1

NFS server (CentOS)

sudo yum install nfs-utils
sudo systemctl start rpcbind nfs-server nfs-lock nfs-idmap
sudo systemctl enable rpcbind nfs-server nfs-lock nfs-idmap

echo "/nfs/data *(rw,sync,no_root_squash,no_all_squash)" | sudo tee -a /etc/exports
sudo exportfs -rav

sudo firewall-cmd --permanent --zone=public --add-service=nfs
sudo firewall-cmd --permanent --zone=public --add-service=mountd
sudo firewall-cmd --permanent --zone=public --add-service=rpc-bind
sudo firewall-cmd --reload

NFS client (Ubuntu)

sudo apt update
sudo apt install nfs-common
sudo mount nfs-server:/srv/nfs/share /mnt

Automation with expect

#!/usr/bin/expect
spawn ssh [email protected]
expect "password:"
send "mypassword\r"
expect "$ "
send "ls -l\r"
expect eof

Key commands: spawn (start a process), expect (wait for output), send (send input), interact (hand control back to the user).

Other Useful Commands

openssl encryption / decryption

tar -zcf - host.conf | openssl des3 -salt -k ZYProduct@2022 | dd of=host.des
openssl des3 -d -k "ZYProduct@2022" -in host.des | tar zxf -

eval

Evaluates its arguments as a shell command: eval [argument].

xargs

# Find files containing "password"
find . -name "*.txt" | xargs grep "password"

# Pass each line as a separate argument
echo "hello world" | xargs -n 1

echo with colors

echo -e "\e[1;33m test \e[0m"

User and File Management

# Create group and user
groupadd mysql
useradd -u 544 -d /home/mysql -g mysql -m mysql

# Change ownership
chown zhuo:zhuogroup test.php
chown -R zhuo:zhuogroup testdir

SELinux Policy Management with semanage

List, add, modify, or delete port and file‑context definitions.

# List all ports
semanage port -l

# Add a TCP port 8081 for http_port_t
semanage port -a -t http_port_t -p tcp 8081

# Delete the entry
semanage port -d -t http_port_t -p tcp 8081

Check SELinux status with getenforce, edit /etc/sysconfig/selinux, and reboot if changes are made.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSysadminViBashfile-managementcommand-line
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.