Fundamentals 33 min read

Master Essential Linux Commands: From File Management to System Monitoring

This guide compiles key Linux command fundamentals—including command connectors, system resource monitoring, file searching, text editing, user and file management, network transfers, SELinux policy handling, and useful scripting snippets—providing a concise reference for administrators and developers alike.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Essential Linux Commands: From File Management to System Monitoring

After studying Linux for a while, I began learning basic commands; this document records important knowledge points for future reference.

0. Command Connectors

1) ;

Commands are executed sequentially; maintain order, and if changing directories, it must be done within a single command line.

2) &

The following command runs regardless of the success of the preceding one; both commands are executed.

3) &&

The next command runs only if the previous command succeeds; both commands are executed.

4) |

The output of the preceding command is piped to the following command, which is the only one executed.

5) ||

The next command runs only if the previous command fails.

1. System Resource Monitoring Commands

1) File system inspection command df

The df command reports file system usage, considering not only file space but also space used by processes (commonly, files deleted but still held by processes).

image
image
image
image

2) Directory or file size command du

The du command is file‑oriented and calculates space used by files and directories:

du -sh * | sort -nr
image
image

For example, to view the top six directories (excluding files) and files (including directories) consuming the most space under the Tomcat directory:

View the top six directories (excluding files) and files (including directories) consuming the most space under the Tomcat directory:
image
image
image
image

3) Process inspection command ps

[publisher@shfttppfbdb ~]$ ps -ef|grep sync
publish+  15990      1 97 Jun30 ?        33-12:38:36 python3.6 -u sync_bmcapp_newversion.py
publish+  40899      1 99 Jun29 ?        34-16:32:24 python3.6 -u sync_hxapp_newversion.py
publish+ 129897 129351  0 15:16 pts/1    00:00:00 grep --color=auto sync
[publisher@shfttppfbdb ~]$ ps -ef|grep sync |awk $'{print $2}'
15990
40899
129914
[publisher@shfttppfbdb ~]$

The grep -v grep pattern filters out the grep process itself from the output.

#!/bin/sh
app_name="manage"   #应用名
app_path="/app/Assistant"        #应用位置
pid=''  #应用运行的pid
MAX_TIMEOUT=10 #最大等待次数
#重启方法
function stop(){
    echo "开始停止${app_name}"
    pid=`ps aux|grep ${app_name}|grep -v grep|awk '{print $2}'` #获取jar包运行的pid
    echo $pid
    if [ -z $pid ] ;then  #如果pid的长度为空,说明没有在运行
        echo "${app_name}未运行,直接启动"
    else
        echo "${app_name}正在运行中,进程号:"${pid}
        kill -15 $pid
    fi
    #等待60S进行强杀
    for((i=0;i<$MAX_TIMEOUT;i++))
    do
        pid=`ps aux|grep ${app_name}|grep -v grep|awk '{print $2}'`
        if [ -z $pid ]; then
            break
        else
            echo "${app_name}停止中..."
        fi
        sleep 6
    done
    if [ -z $pid ] ;then
        echo "${app_name}已停止"
    else
        kill -9 $pid
        echo "${app_name}已停止"
    fi
}
stop

2. Editing command vi

1) Command mode

At any time, pressing ESC returns Vi to command mode; when launching Vi from the shell prompt ($), it starts in this mode. In this mode, typed characters are interpreted as Vi commands; valid commands are executed without being displayed, while invalid ones trigger a beep.

如:G跳到文本末尾,gg跳到文本开头

2) Insert mode

In command mode, entering i (insert), a (append), o (open), c (change), r or s switches to insert mode, where typed characters become file content and are shown on screen. Press ESC to return to command mode.

如,i,a,o,c等命令

3) Ex (last line) mode

Pressing : in command mode enters Ex mode, displaying a colon at the bottom line and awaiting a command (e.g., q! to quit without saving, wq to write and quit).

如:(shift+:键,进入此模式)q!强制退出,wq保存退出等命令。

4) Vim string substitution

Find and replace is a common feature in any text editor. Vim implements it with the following syntax:

:<range> s/<search_string>/<replace_string>/<modifier>
range

– defines the scope (e.g., % for the whole file or 10,20 for specific lines) search_string – the text to find replace_string – the new text modifier – controls the replacement behavior (e.g., g for global, gc for confirmation, gn to highlight only)

% – apply to the entire file \ \ – apply to a specific line range
g – global replace gc – ask for confirmation before each change gn – highlight matches without replacing

Example: replace all occurrences of python with Python3 in the whole file: :%s/python/Python3/g To limit the operation to lines 200‑250: :200,250 s/python/Python3/g Multiple keywords can be replaced simultaneously: :%s/python\|py/Python3/g By default, replacements are case‑sensitive. Append \c to ignore case:

/<search_term>\c
:%s/<search_term>\c/<replace_term>/g

3. File Search Commands

1) String search command grep

# 查找指定字符串string
grep 'string' filename
# 若需要在文件夹下所有文件查找
grep 'string'  dirPath/*
For recursive search in nested directories, use grep -r "string" ./

Use regular expressions for pattern matching.

2) File search command find

#搜索文件
find [搜索范围] [搜索条件]
find / -name install.log
# Avoid large‑scale searches as they consume resources

Examples:

# Find files not modified in the last 10 days
find . -mtime +10
# Find files of size 25KB
find . -size 25k
# Find inode 262422
find . -inum 262422
# Find files in /etc larger than 20KB and smaller than 50KB
find /etc -size +20KB -a -size -50KB
# Show detailed info for those files
find /etc -size +20k -a -size -50k -exec ls -lh {} \;
Wildcard patterns: * matches any string ? matches a single character [] matches any character inside the brackets

3) Command search whereis and which

# Search for command location and manual pages
whereis command_name
Options: -b only binaries, -m only manuals

4) File search command locate

# Fast name‑based search using a database
locate filename
Database location: /var/lib/mlocate Update with updatedb
image
image

4. File Transfer Commands

Prerequisites: server must allow write access; both local and remote machines need the scp package installed.

Transfer methods: use scp, rsync, or rz.

1) Download a file from the server

scp username@servername:/remote/path/filename /local/path

Example:

scp [email protected]:/var/www/test.txt .

2) Download a directory from the server

scp -r username@servername:/remote/dir /local/dir

Example:

scp -r [email protected]:/var/www/test .

3) Upload a local file to the server

scp /local/file username@servername:/remote/dir

Example:

scp /var/www/test.txt [email protected]:/var/www/

4) Upload a local directory to the server

scp -r /local/dir username@servername:/remote/dir

Example:

scp -r /var/www/local_dir [email protected]:/var/www/

5) rsync to fetch a remote directory

Exclude specific sub‑directories if needed.

rsync -azh -ssh --exclude 'l_temp' --exclude 'l_tmp/claimImageList' [email protected]:/t/system_temp /t/system_temp
Excludes l_temp and l_tmp/claimImageList from the copy.
rsync -az /tpsys/applications/oceanbase_check /tpsys/applications/ --log-file=./rsync_oceanbase_check.log --exclude='oceanbase-client-1.1.10.jar' --delete

rsync -az /tpsys/applications/oceanbase_check [email protected]:/tpsys/applications --log-file=./rsync_oceanbase_check.log --exclude-from /tpsys/applications/conf/sync_exclude.conf --delete

6) Transfer files from the local PC using rz

image
image

7) Transfer using sftp

sftp username@ip
put [local_path] [remote_path]
get [remote_path] [local_path]
# The local directory after login is the remote's home directory

5. Linux Text Operations

1) The Linux "three swords": grep, sed, awk

Refer to the linked article for details.

6. Other Common Commands

1) Sorting command sort

Linux provides sort for various sorting needs, such as top‑N problems. Common options are shown below.

image
image

2) File splitting command split

split -b 1024M bkce_src-5.1.28.tar.gz bkce_
cat bkce_* >bkce_src-5.1.28.tar.gz
md5sum bkce_src-5.1.28.tar.gz

3) Mount command

(1) Query and auto‑mount

# List mounted devices
mount
# Auto‑mount based on /etc/fstab
mount -a
# Show NFS exports from a server
showmount -e 192.168.161.128

(2) Mount command syntax

mount [-t filesystem] [-o options] device mount_point
-t specifies the filesystem type (e.g., ext3, ext4, iso9660). -o provides additional mount options.
image
image

(3) Unmount command

umount device_or_mount_point

(4) Install NFS server on CentOS

Install NFS utilities:

sudo yum install nfs-utils

Start services and enable them at boot:

sudo systemctl start rpcbind
sudo systemctl start nfs-server
sudo systemctl start nfs-lock
sudo systemctl start nfs-idmap
sudo systemctl enable rpcbind
sudo systemctl enable nfs-server
sudo systemctl enable nfs-lock
sudo systemctl enable nfs-idmap

Add a share to /etc/exports (e.g., share /data to all hosts):

/nfs/data *(rw,sync,no_root_squash,no_all_squash)

Apply configuration and restart NFS:

sudo exportfs -rav
sudo systemctl restart nfs-server
# Reload daemon if needed

If a firewall is active, allow NFS services:

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

Client temporary mount:

sudo mount <nfs-server-ip>:/nfs/data /nfs/data

Permanent client mount entry in /etc/fstab:

192.168.44.132:/nfs/data /nfs/data nfs defaults 0 0

(5) Install NFS server on Ubuntu

Update package list and install kernel NFS server:

sudo apt update
sudo mkdir -p /srv/nfs/share

Edit /etc/exports (replace <client_IP> with the client address):

/srv/nfs/share <client_IP>(rw,sync,no_subtree_check)

Apply changes and start the service:

sudo exportfs -ra
sudo systemctl start nfs-kernel-server
sudo systemctl enable nfs-kernel-server

Install client utilities:

sudo apt install nfs-common

Mount the share temporarily:

sudo mount <nfs_server_ip>:/srv/nfs/share /mnt

Add to /etc/fstab for permanent mounting:

192.168.44.132:/applications /applications/nfs nfs defaults 0 0

4) expect command

expect

automates interactive tasks without human intervention. yum install -y expect Core commands: spawn (start process), expect (receive output), send (send input), interact (hand over to user).

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

5) openssl encryption/decryption

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

6) eval command

eval

evaluates its arguments as a shell command in the current environment.

eval [argument]

7) xargs command

xargs

builds and executes command lines from standard input.

-n: number of arguments per command

-d: delimiter character

-p: prompt before execution

-t: display command before execution

-I: replaceable placeholder (default {})

-r: do not run if input is empty

Examples:

# Find txt files containing "password"
find . -name "*.txt" -exec grep "password" {} \;
# Or using xargs
find . -name "*.txt" | xargs grep "password"
# Pass one argument at a time
echo "hello world" | xargs -n 1
# Split on '#'
echo "hello#world" | xargs -d "#"
# Use placeholder
echo "hello#world" | xargs -I {} echo {}

8) echo command

Usage:

echo [options] [text]
-e enables interpretation of backslash‑escaped characters.
image
image

Set text color

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

7. Linux User Management

1) Linux user types

image
image

2) Linux users

image
image

3) groupadd / useradd

Add a mysql group and user:

groupadd mysql
useradd -u 544 -d /home/mysql -g mysql -m mysql

4) chown – change owner and group

Set owner to zhuo and group to zhuogroup for test.php: chown zhuo:zhuogroup test.php Recursively change owner for a directory:

chown -R zhuo:zhuogroup testfile

7. Linux File Management

1) Linux file types

image
image

9. Other Common Functions

1) SSH remote login example

ssh -tt [email protected] << EOF
sh $proddomaindir/stopappserver1.sh
sh $proddomaindir/startappserver1.sh
exit
EOF

Or:

ssh [email protected] "rm -rf /tsys/applications/nginx/log/*"

2) Monitor file changes within the last minute

#!/bin/bash
if [ -e "/tsys/applications//web" ]; then
    for i in `find /tsys/applications//web -type f -cmin 1 ! -name "*.log*" ! -name "*.out*" ! -name "*.err*" ! -name "*.ser" ! -name "*.tar.gz"`
    do
        echo `date +%F%t%T`"   "$i" >>/tsys/applications/showChangeFile.log;
    done
fi

3) File tamper‑monitoring

#!/bin/bash
original_publish_dir='/tsys/applications//web'
current_publish_dir='/tsys/applications//web'
original_MD5='original_MD5.txt'
current_MD5='current_MD5.txt'
if [ "$1" == "original" ]; then
   cd /tsys/applications && rm -rf ./$original_MD5
   find $original_publish_dir -type f ! -name "*.rdb*" ! -name "*.zip*" ! -name "*.xls*" ! -name "*.rar*" ! -name "*.txt*" ! -name "*.pdf*" ! -name "*.log*" ! -name "*.out*" ! -name "*.err*" ! -name "*.ser" ! -name "*.tar.gz" -exec md5sum {} \;| sort > /tsys/applications/$original_MD5
fi
if [ "$1" == "current" ]; then
   cd /tpsys/applications && rm -rf ./$current_MD5
   find $current_publish_dir -type f ! -name "*.rdb*" ! -name "*.zip*" ! -name "*.xls*" ! -name "*.rar*" ! -name "*.txt*" ! -name "*.pdf*" ! -name "*.log*" ! -name "*.out*" ! -name "*.err*" ! -name "*.ser" ! -name "*.tar.gz" -exec md5sum {} \;| sort > /tsys/applications/$current_MD5
   backcode=`diff -q $original_MD5 $current_MD5 | awk '{print $1}'`
fi
if [ "$backcode" != "" -a "$1" == "current" ]; then
  echo `date +%F%t%T`"  "`diff $original_MD5 $current_MD5 |awk '{print $3}'|xargs`" >/tsys/applications/showDiffFile.log
fi

4) Read CSV, create directories, copy files

#!/bin/bash
tr -d '\r' < "pensionmain.csv" > "output.csv"
csv_file="./output.csv"
while IFS=, read -r column1 column2
do
    dir_name="$column1"
    if [ ! -d "$dir_name" ]; then
        echo "mkdir $dir_name"
        mkdir "$dir_name"
        echo "./$dir_name/ $column2"
        cp $column2 ./"$dir_name"/
    else
        echo "./$dir_name/ $column2"
        cp $column2 ./"$dir_name"/
    fi
done < "$csv_file"

10. Managing SELinux Security Policies with semanage

The semanage tool manages SELinux policies (e.g., ports, file contexts).

1) Common parameters

port – manage network port types fcontext – manage file context types -l – list records -a – add record -m – modify record -d – delete record -t – specify type -p – specify protocol (tcp/udp) for ports -e – use existing context as reference for file contexts

2) Verify semanage installation

yum install policycoreutils-python -y

3) Check if SELinux is enabled

egrep -i selinux /etc/sysconfig/selinux | egrep -v "#"
# Output example:
SELINUX=enforcing
SELINUXTYPE=targeted

If not enabled, edit /etc/sysconfig/selinux and set SELINUX=enforcing, then reboot.

vim /etc/sysconfig/selinux
# Change to:
SELINUX=enforcing
reboot

4) View service port configuration

Example for Apache:

egrep -i listen /etc/httpd/conf/httpd.conf | egrep -v "#"

5) List all defined ports

semanage port -l

6) List ports of a specific type

semanage port -l | grep -w http_port_t
semanage port -l | grep -w 443

7) Add or delete a port

# Add TCP port 8081 as http_port_t
semanage port -a -t http_port_t -p tcp 8081
# Delete the same entry
semanage port -d -t http_port_t -p tcp 8081
Source: https://www.cnblogs.com/hoaprox/p/18246830 (© original author)
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.

LinuxScriptingcommand-linesystem-administration
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.