Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential Linux Command Reference

Tech May 9 4

Check network interface address configuration

ip address show
# shorthand
ip a

ip address output

Test network connectivity

ping www.example.com

ping output

Modify network interface address through a graphical TUI

nmtui

nmtui interface

Shutdown commands

# Shut down immediately
shutdown now
# Schedule shutdown in 5 hours
shutdown -h 5
# Power off directly
poweroff

Reboot commands

# Reboot immediately
shutdown -r now
# Alternatively
reboot

List files, including hidden ones

  • a stands for all
  • l stands for long listing format
ls -al

ls -al output

Create directories with mkdir

  • -p creates nested directories
mkdir -p projects/app

mkdir output

Open the manual for a command

man ls

man page

Switch to the home directory or current user

cd
# or
cd ~

Vi/Vim editing commands

:wq --- save and quit
:wq!--- force save and quit
:q! --- force quit without saving

Search within Vim

command mode -> ex mode
:           enter commands
/           search forward (n moves to next match)
?           search backward (n moves to next match)

vim search

Vim special shortcuts

Delete (cut) a line          dd
Delete (cut) multiple lines  3dd
Paste                        p
Paste multiple times         3p
Copy (yank) a line           yy
Copy multiple lines          3yy
Undo                         u

Print text to screen or redirect to a file (echo)

# Print to screen
echo hello
# Write to file
echo hello > output.txt

echo usage

Display filee content with cat

  • -n shows line numbers
cat -n data.txt

cat output

Copy files or directories (cp)

  • Use cp -r to copy directories recursively
  • Use \cp to force overwrite if the destination file exists

Remove files or directories

  • rm -r deletes recursively
  • rm -rf forcefully deletes without confirmation (frequently used)

Move or rename files (mv)

# Rename
mv oldname.txt newname.txt
# Move (use -rf for directories)
mv oldname.txt /tmp

Mount storage devices

mount device mountpoint
# Unmount
umount mountpoint

View and modify hostname

hostname

hostname output

Permanently change hostname (CentOS 7)

hostnamectl set-hostname myserver

hostnamectl output

Check disk usage and mount points

df -h

df output

Check memory usage

free -h

free output

Apply configuration file changes immediately (source)

Files often sourced:

/etc/profile
/etc/bashrc
~/.bashrc
~/.bash_profile
/etc/sysconfig/i18n  --- CentOS 6 locale config
/etc/locale.conf     --- CentOS 7 locale config

Show full path of a command

which ls

which output

Create command aliases (alias)

  • alias shortname='long command'
# Define an alias to check web server status
alias webstatus='systemctl status nginx'
# Use the alias
webstatus

alias usage

Remove an alias

unalias webstatus

View the last lines of a file (tailf / tail)

  • -n specifies how many lines
# Show last 3 lines
tail -3 /etc/passwd
# or
tailf -3 /etc/passwd

tail output

View the first lines of a file (head)

  • Default is 10 lines; -n specifies count
# Show first 3 lines
head -3 /etc/passwd

head output

Basic Yum commands (CentOS 7)

yum install -y package_name     # Install a package
yum repolist                    # List enabled repositories
yum list                        # List available/installed packages
yum --help                      # Show help options
yum provides locate             # Find which package provides a command

Process management

ps displays process information in the current shell.

ps output

Show all running processes in detail

# Pipe with grep to filter for a web server
ps -ef | grep httpd

ps -ef output

Terminate processes

kill -9 PID           # Force kill a process by PID
killall process_name  # Kill processes by name

View CPU information

lscpu

lscpu output

Check system load and logged-in users (w)

w

w output

Monitor system load interactively

top

top output

Monitor system status with vmstat

  • r — processes waiting for CPU time
  • b — processes in uninterruptible sleep
  • swpd — virtual memory used (KB)
  • si — swap-in from disk (KB)
  • so — swap-out to disk (KB)
  • bi — blocks received from a block device
  • bo — blocks sent to a block device
  • wa — CPU time spent waiting for I/O
vmstat

vmstat output

Monitor network interface bandwidth (nload)

yum install -y epel-release
yum install -y nload
nload

nload output

Create a user

useradd john
# Set a password
passwd john

Switch user (su)

# Switch to root
su
# Switch to a specific user
su - john

Display current username

whoami

whoami output

RPM package management

rpm -qa package_name    # Check if a package is installed
rpm -ql package_name    # List files in a package
rpm -qf /path/to/file   # Find which package owns a file

Manage services with systemctl

systemctl start   service  # Start a service
systemctl stop    service  # Stop a service
systemctl restart service  # Restart a service
systemctl status  service  # Show detailed status
systemctl disable service  # Disable service at boot
systemctl enable  service  # Enable service at boot
systemctl is-active  service  # Check if the service is running
systemctl is-enabled service  # Check if the service is enabled

Set locale to support Chinese (CentOS 7)

localectl set-locale LANG=zh_CN.UTF-8

Locate binary and manual files for a command (whereis)

whereis ls

whereis output

Find files by name quickly (locate)

locate pycharm.desktop

locate output

Update the locate database (updatedb)

Run updatedb when new files have been created, otherwise locate may not find them.

Determine file type

file mydir/

file output

Display detailed file metadata (stat)

stat notes.txt

stat output

Archiving and compression with tar

Options:

  • -z : compress with gzip
  • -c : create archive
  • -v : verbose
  • -f : specify archive filename
  • -x : extract
  • -t : list archive contents
  • --exclude : exclude specific files
  • --exclude-from
# Create archive of configs
tar -zcvf bundle.tar.gz notes.txt my.cnf

tar create

# Extract archive
tar -xvf bundle.tar.gz

tar extract

Display directory tree

  • tree -L 1 : limit depth to 1 level
  • tree -d : show directories only

tree output

Display and format date/time

date "+%F_%T"

date output

Create links

  • ln source link : create a hard link (directories cannot have hard links)
  • ln -s source link : create a symbolic (soft) link

Line count with wc

cat /etc/passwd | wc -l

wc output

Change file permissions (chmod)

  • u : user (owner)
  • g : group
  • o : others
  • r : read, w : write, x : execute
  • + grants permission, - revokes it
# Grant execute permission to others
chmod o+x data.txt

Change ownership (chown)

  • -R : apply recursively
chown alex:alex data.txt

chown output

Disk usage of a directory (du)

  • -sh : summarize total size in human-readable format
du -sh projects/

du output

Search for files (find)

find starts from a given path, -type specifies type (f for file, d for directory), -name matches a pattern (supports glob).

find /etc/ -type f -name passw*

find output

Filter text with grep

  • -B N : show N lines before the match
  • -A N : show N lines after the match
  • -C N : show N lines before and after
  • -c : count matching lines
  • -v : invert match
  • -E / egrep : extended regex
  • -o : show only matching part
  • -n : show line numbers
  • -i : case-insensitive
# Show line containing 'halt' and two lines before it
grep -B 2 'halt' /etc/passwd

grep output

Special shell symbols

  • ~ : home directory
  • .. : parent directory
  • . : current directory
  • > : stdout redirection (overwrite)
  • >> : stdout redirection (append)
  • 2> : stderr redirection (overwrite)
  • 2>> : stderr redirection (append)
  • < : stdin redirection
  • << : here document
  • && : run next command only if previous succeeds
  • ; : run commands sequentially regardless of exit status
  • # : comment (or root prompt when used at the start of a line)
  • $ : variable expansion; end of line in regex; denotes a regular user prompt
  • ! : force / history expansion
  • ` (backticks) : command substitution
  • | (pipe) : send stdout of left command to stdin of right command

Brace expansion for sequences

touch {1,2}.txt

brace expansion

Keyboard shortcuts

  • Ctrl+C – interrupt the current command
  • Ctrl+L – clear the screen
  • Ctrl+D – logout (EOF)
  • Tab – auto-complete paths, filenames, commands
  • Up/Down arrows – browse command history
  • Ctrl+A – move cursor to beginning of line
  • Ctrl+E – move cursor to end of line
  • Ctrl+Left/Right – move cursor by word
  • Esc+. – insert the last argument from the previous command
  • Ctrl+U – delete from cursor to start of line (cut)
  • Ctrl+K – delete from cursor to end of line (cut)
  • Ctrl+Y – paste the cut text
  • Ctrl+R – search command history interactively

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.