Master Linux Command Line: Essential Tips and Tricks for System Operations
The article covers Linux commands, shortcuts, file and directory management, permissions, users, searching, software repositories, manual pages, advanced topics like redirection, pipelines, processes, daemons, compression, compilation, networking, backup, and system control, providing practical examples and code snippets.
1. Commands
1.1 Command Prompt
After entering the command line environment, the user sees the shell prompt, which usually ends with a dollar sign ($) indicating where to type commands.
1.2 Command Syntax
command parametersShort and long options
ls -a # a is short for "all"
ls -al # all files + long listing
ls --all # long form of -a
ls --reverse --all
ls --all -lOption values
command -p 10 # e.g., ssh root@host -p 22
command --parameters=102. Shortcuts
Before learning Linux commands, master these shortcuts that will be used throughout your Linux journey.
Use ↑ ↓ to scroll through command history.
Tab completes commands or parameters.
Ctrl+R searches command history.
Ctrl+L clears the screen.
Ctrl+C aborts the current command.
Ctrl+U cuts from cursor to line start.
Ctrl+K cuts from cursor to line end.
Ctrl+W cuts the word left of the cursor.
Ctrl+Y pastes what was cut with Ctrl+U/K/W.
Ctrl+A moves cursor to line start.
Ctrl+E moves cursor to line end.
Ctrl+D closes the shell session.
3. Files and Directories
3.1 File Organization
3.2 Viewing Paths
pwd – displays the current directory path.
# pwd
/rootwhich – shows the location of an executable.
A command is essentially an executable program.
3.3 Browsing and Switching Directories
ls – lists files and directories.
Common options
-a shows all files, including hidden ones.
-l shows a detailed list.
-h makes sizes human‑readable.
-t sorts by modification time.
-i shows the inode number.
cd – change directory ("change directory" abbreviation).
cd / # go to root directory
cd ~ # go to home directory
cd .. # go to parent directory
cd ./home # go to ./home
cd /home/lion # go to /home/lion
cd # without arguments, goes to homeNote: pressing Tab once after cd /home auto‑completes the path; pressing twice lists possible directories.
du – shows directory size information.
Common options
-h human‑readable.
-a includes file sizes.
-s shows only total size.
3.4 Creating Files
cat – displays entire file content (good for small files). cat cloud-init.log less – paginated view for large files. less cloud-init.log Quick operations
Space – forward one page.
b – back one page.
Enter – forward one line.
y – back one line.
↑/↓ – move one line.
d – forward half a page.
u – back half a page.
q – quit.
= – show current line numbers and details.
h – help.
/ – search (n for next, N for previous, supports regex).
head – shows the first few lines (default 10). head cloud-init.log Option -n specifies number of lines, e.g., head -n 2 cloud-init.log.
tail – shows the last few lines (default 10). tail cloud-init.log Option -n specifies number of lines; -f follows the file, optionally with -s to set interval.
touch – creates an empty file. touch new_file mkdir – creates a directory. mkdir new_folder Option -p creates parent directories as needed.
3.5 Copying and Moving Files
cp – copy files.
cp file file_copy
cp file one
cp file one/file_copy
cp *.txt folder # copy all .txt files to folderOption -r copies directories recursively.
mv – move or rename files.
mv file one
mv new_folder one
mv *.txt folder
mv file new_file3.6 Deleting Files and Links
rm – remove files or directories (no recycle bin).
rm new_file
rm f1 f2 f3Common options: -i ask for confirmation, -f force, -r recursive (e.g., rm -rf).
ln – create links.
Linux stores a file's name list separately from its content; each name points to an inode.
Two link types:
Hard link – shares the same inode; changes affect both names. Only files (not directories) can be hard‑linked.
Soft link (symbolic link) – similar to Windows shortcuts; points to a path.
ln file1 file2 # hard link
ln -s file1 file2 # soft linkView links with ls -l:
# ls -l
lrwxrwxrwx 1 root root 5 Jan 14 06:41 file2 -> file13.7 File Permissions
chmod – change access permissions. chmod 740 file.txt Option -R applies recursively.
Permission string example: drwxr-xr-x means a directory where owner has read/write/execute, group has read/execute, others have read/execute.
Numeric mode explanation:
chmod 640 hello.c # 6=4+2+0 (owner rw), 4=read for group, 0=no permissions for others
# results in -rw-r-----Symbolic mode example:
chmod u+rx file
chmod g+r file
chmod o-r file
chmod u=rwx,g=r,o=- file4. Users and Permissions
4.1 Users
Linux is multi‑user. The special root user has full privileges.
Regular users have limited permissions; sudo elevates privileges temporarily.
sudo – run a command as root. sudo date useradd – add a new user; passwd – set a user's password.
useradd lion
passwd lionuserdel – delete a user (option -r also removes home directory).
userdel lion
userdel lion -rsu – switch user.
sudo su # become root
su lion # become lion
su - # become root with login environment4.2 Group Management
Each user belongs to a primary group; groups can be created and managed.
groupadd – create a group. groupadd friends groupdel – delete a group. groupdel foo groups – list groups a user belongs to. groups lion usermod – modify user attributes.
-l rename user.
-g change primary group.
-G add secondary groups (use -a -G to append).
chgrp – change a file's group. chgrp bar file.txt chown – change file owner (option -R for recursive).
chown lion file.txt
chown lion:bar file.txt
chown -R lion:lion /home/frank4.3 File Permission Management
chmod – modify file permissions. chmod 740 file.txt Common option: -R for recursive changes.
5. Finding Files
locate
Searches a database of filenames.
yum -y install mlocate
updatedb
locate file.txt
locate fil*.txtNote: newly created files are not in the database until updatedb runs.
find
Searches the actual filesystem; very powerful.
find /path -name "file.txt"
find . -name "syslog"
find / -size +10M
find . -name "*.txt" -atime -7
find . -type f -name "file"
find . -type d -name "dir"
find *.txt -printf "%p - %u
"
find *.c -delete
find *.c -exec chmod 600 {} \;6. Software Repositories
Linux packages are distributed as archives (.rpm for Red Hat, .deb for Debian). The yum tool manages packages on CentOS/Red Hat.
yum update | yum upgrade – update packages.
yum search xxx – search packages.
yum install xxx – install.
yum remove xxx – remove.
To switch to a domestic mirror (e.g., Alibaba Cloud):
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache7. Reading Manuals
Use man to view command manuals.
sudo yum install -y man-pages
mandbSections of a man page (example man pwd):
NAME – command name and brief description.
SYNOPSIS – usage syntax.
DESCRIPTION – detailed description and options.
SEE ALSO – related commands.
Quick help with command --help or command -h.
8. Linux Advanced Topics
Text Operations
grep
Searches for patterns in files.
grep text file
grep -i path /etc/profile
grep -n path /etc/profile
grep -v path /etc/profile
grep -r hello /etcExtended regex with -E:
grep -E path /etc/profile
grep -E ^path /etc/profile
grep -E [Pp]ath /etc/profilesort
Sorts lines of a file.
sort name.txt
sort -o name_sorted.txt name.txt
sort -r name.txt
sort -R name.txt
sort -n numbers.txtwc
Word count – lines, words, bytes.
wc name.txt
wc -l name.txt
wc -w name.txt
wc -c name.txt
wc -m name.txtuniq
Removes duplicate consecutive lines.
uniq name.txt
uniq -c name.txt
uniq -d name.txtcut
Extracts sections from each line.
cut -c 2-4 name.txt
cut -d , -f 1 name.txt9. Redirection, Pipes, and Streams
Standard streams: stdin, stdout, stderr.
Redirection
Output redirection:
command > file # overwrite
command >> file # append
command 2> err.log # redirect stderr
command > out.txt 2>&1 # both stdout and stderr to fileInput redirection:
command < file
command << END
...content...
ENDPipes
Connect commands; output of left becomes input of right.
cut -d , -f 1 name.csv | sort > sorted_name.txt
du | sort -nr | head
grep log -Ir /var/log | cut -d : -f 1 | sort | uniqStreams
Data flows as a stream; concepts used in many languages (e.g., Angular pipes, Node.js streams).
10. Viewing Processes
w
Shows who is logged in and what they are doing.
# w
06:31:53 up 25 days, 9:53, 1 user, load average: 0.00, 0.01, 0.05ps
Snapshot of current processes.
# ps
PID TTY TIME CMD
1793 pts/0 00:00:00 bash
4756 pts/0 00:00:00 psCommon options: -ef, -efH, -u, -aux, --sort -pcpu, -axjf.
top
Dynamic view of processes sorted by CPU usage.
# topkill
Terminate a process by PID.
kill 956
kill -9 729111. Managing Processes
Process states: R (running), S (sleeping), D (uninterruptible), Z (zombie), T (stopped).
Foreground & Background
Append & to run a command in the background. cp name.csv name-copy.csv & Use bg to resume a stopped background job, fg to bring it to the foreground, jobs to list jobs, and nohup to keep a job running after logout.
12. Daemons
Daemons run in the background with PID 1 as parent (e.g., systemd, httpd).
systemctl commands:
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl status nginx
systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx
systemctl list-unit-files --type=service13. File Compression and Extraction
Archive with tar, compress with gzip or bzip2.
tar -cvf archive.tar files/
gzip archive.tar # creates archive.tar.gz
gzip -d archive.tar.gz
tar -zcvf archive.tar.gz files/
tar -zxvf archive.tar.gzView compressed files without extracting:
zcat archive.tar.gz
zless archive.tar.gz
zmore archive.tar.gzZIP format (common on Windows):
yum install zip unzip
unzip archive.zip
unzip -l archive.zip
zip -r sort.zip sort/14. Compiling and Installing Software
When a package is not in the repository, compile from source.
Download source code.
Extract archive.
Run ./configure to check dependencies.
Run make to compile.
Run make install to install.
Example installing htop:
scp htop-3.0.0.tar.gz root@host:.
tar -zxvf htop-3.0.0.tar.gz
cd htop-3.0.0
./configure
make
sudo make install15. Networking
ifconfig
Shows network interfaces; install with yum install net-tools if missing.
# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> ...
lo: flags=73<UP,LOOPBACK,RUNNING> ...host
Resolve hostnames to IPs and vice versa; install with yum install bind-utils.
# host github.com
github.com has address 13.229.188.59ssh
Secure remote login.
ssh [email protected] # default port 22
ssh -p 2222 user@hostConfigure per‑user ~/.ssh/config for shortcuts:
Host lion
HostName 172.x.x.x
User root
Port 22Enable key‑based (password‑less) login:
ssh-keygen # creates id_rsa and id_rsa.pub
ssh-copy-id root@host # copies public key to serverwget
Download files from the command line.
wget http://example.com/file.zip
wget -c http://example.com/large.iso # continue interrupted download16. Backup
scp
Secure copy over SSH.
scp file.txt [email protected]:/root
scp [email protected]:/root/file.txt ./rsync
Efficient remote synchronization (incremental backup).
yum install rsync
rsync -arv Images/ backups/
rsync -arv Images/ [email protected]:backups/
rsync -arv --delete Images/ backups/17. System Control
Shutdown, reboot, or power off the system.
halt # requires root
reboot # requires root
poweroff # can be run by any userSummary
After reading this guide, you should have a comprehensive understanding of Linux fundamentals and practical command‑line skills.
Give it a 👍 if you found it helpful.
Source: https://juejin.cn/post/6938385978004340744
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
