Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Core Linux System Fundamentals and Essential Commands

Tech 1

Linux Distributions and Advantages

Linux offers a diverse range of distributions tailored to different use cases. The Red Hat ecosystem includes Red Hat Enterprise Linux (RHEL), Fedora, and CentOS. Ubuntu (widely adopted in automotive and cloud environments), SUSE, and Debian are other prominent distros.

Key benefits of Linux:

  • Open-source and free (low-cost for enterprise support plans)
  • Excepsionally stable with minimal resource overhead
  • Native multi-user, multi-tasking capabilities
  • Continuously improved by a global community of developers and enthusiasts

Common remote administration tools include PuTTY, SecureCRT, Xshell, and MobaXterm, which enable SSH connections to Linux systems.

Using Linux Systems

  1. Configure a SUSE virtual machine via VMware (follow official setup guides for VM deployement)
  2. Retrieve the system's IP address with:
ip addr show
  1. Establish an SSH connection to the VM using MobaXterm by entering the IP, username, and authentication credentials.

Linux Directory Structure

The Linux filesystem follows a hierarchical structure with specialized directories:

  • /bin: Stores user-executable commands like cd and ls accessible to all users
  • /sbin: Contains system administration commands restricted to privileged users for service management
  • /boot: Houses kernel and bootloader files—do not modify these critical system components
  • /dev: Virtual directory for device files (CPU, disks, network adapters); no manual edits required
  • /etc: Central repository for system-wide configuration files (network settings, service configs)
  • /home: User-specific directories for personal files and settings
  • /lib & /lib64: Shared libraries required for system and application execution
  • /media: Mount point for removable devices (USB drives, optical disks)
  • /mnt: Temporary mount directory for external storage volumes
  • /opt: Default installation path for third-party software applications
  • /proc: Virtual filesystem with real-time system and process data—no persistent storage
  • /run: Runtime data for active services, cleared on system reboot
  • /srv: Data files for services provided by the system (e.g., web server content)
  • /sys: Virtual filesystem exposing kernel and hardware configuraton details
  • /tmp: Temporary file storage, with contents automatically cleaned periodically
  • /usr: Contains user-facing utilities, applications, and shared resources
  • /var: Stores variable data like logs, cache files, and database content

Essential Linux Commands

File & Directory Navigation

Command Description
ls List contents of the current directory
pwd Print the absolute path of the current working directory
cd [path] Change the current directory to the specified path
touch [filename] Create an empty file or update its modification timestamp
mkdir [dirname] Create a new directory
rm [file] Delete the specified file
clear Clear all content from the terminal screen
ls Command Options
  • -a: Display all files, including hidden files (starting with .)
  • -l: Show detailed file metadata (permissions, owner, size, modification time) in list format
  • -h: Use human-readable units (KB, MB) for file sizes when paired with -l

The ll command is a common alias for ls -l. Each entry starts with a permission string:

  • First character: d for directory, - for regular file
  • Next 3 chars: Owner permissions (read r, write w, execute x)
  • Middle 3 chars: Group permissions
  • Last 3 chars: Permissions for all other users

For example, drwxr-xr-x denotes a directory where the owner has full access, while group and others have read/execute only.

cd Command Variations
  • cd ~: Navigate to the current user's home directory (/root for root users)
  • cd /: Jump to the system's root directory
  • cd .: Remain in the current directory
  • cd ..: Move up one directory level
  • cd -: Toggle between the last two working directories

Relative vs Absolute Paths:

  • Relative paths do not start with / or ~ and resolve to the current directory
  • Absolute paths start with / or ~ and specify the full path from the root or home directory
mkdir Command

Use the -p flag to create nested directories recursively:

mkdir -p project/docs/api-specs

New directories cannot share names with existing files/directories in the same location.

File Creation & Editing

The touch command creates empty files or updates timestamps. To edit files directly, use vi:

  1. Run vi filename to open/create the file
  2. Press i or a to enter insert mode for editing
  3. Press Esc to exit insert mode
  4. Use :wq to save and exit, :q! to discard changes, or :w to save without exiting

Use cat filename to view file contents, or cat -n filename to show line numbers.

rm Command

Use -f for force deletion without prompts, -r for recursive directory deletion:

# Delete .log files with confirmation prompts
rm -i *.log

# Force delete all files starting with 'f'
rm -rf f*

# Delete files containing '1' in the name
rm -rf *1*

Copy & Move Operations

cp Command

Copy files/directories with these options:

  • -i: Prompt before overwriting existing files
  • -r: Recursively copy directories and contents
  • -a: Preserve file attributes (timestamps, permissions) during copy

Examples:

# Copy a.txt to /test, preserve attributes, and prompt on overwrite
cp -ai a.txt /test

# Create a symbolic link to a.txt
cp -s a.txt link_a.txt

mv Command

Move files to a directory or rename them:

# Rename test.log to test1.txt
mv test.log test1.txt

# Move multiple log files to /test3
mv log1.txt log2.txt log3.txt /test3

# Move all current directory files to the parent directory
mv * ../

File Viewing for Large Datasets

For large files (GBs in size), use more or less:

  • more /etc/profile: Page down with Space, page up with b, exit with q
  • less /etc/profile: Navigate with arrow keys, exit with q
tail & head Commands
  • tail -n 20 filename: Show the last 20 lines
  • tail -f filename: Track live updates (ideal for monitoring logs)
  • head -n 20 filename: Show the first 20 lines
  • head -n -10 filename: Show all lines except the last 10

To extract specific lines (e.g., lines 20-25):

head -25 filename | tail -5

Log Analysis

To monitor application logs (e.g., Tomcat's catalina.out):

  1. Connect to the server via SSH (IP: 192.168.8.XX, user: root, password: 123456)
  2. Navigate to the log directory:
cd /opt/tomcat/logs
  1. Track live log updates:
tail -f catalina.out
grep Command for Filtering

Common grep options:

  • -i: Ignore case sensitivity
  • -n: Show line numbers for matches
  • -v: Invert search to show non-matching lines
  • -w: Match whole words only

Examples:

# Search for "error" in catalina.out, ignore case, show line numbers
tail -f catalina.out | grep -in error

# Highlight lines containing both "order" and "product" in real time
tail -8000f a.log | grep --color --line-buffer "order" | grep --color --line-buffer "product"
Pipelines & Redirection
  • Pipeline (|): Pass output from one command to another:
# Find lines with "j" in the first 31 lines of tt
head -31 tt | grep j
  • Redirection:
    • >: Overwrite a file with command output: cat fileA > fileB
    • >>: Append output to a file: cat fileA >> fileB

System & File Statistics

wc Command

Count lines, words, and bytes:

  • wc -l filename: Count lines
  • wc -w filename: Count words
  • wc -c filename: Count bytes
  • wc filename: Show lines, words, bytes (e.g., 5 108 1000 test.txt)

System Monitoring Commands

  • top: Real-time system resource usage (CPU, memory, processes)
  • free -h: View memory usage in human-readable units
  • du -h: Calculate disk usage of files/directories
  • df -h: Display total/used/free disk space for mounted volumes
  • ps -ef: List all running processes; filter with grep (e.g., ps -ef | grep tomcat)
  • kill -9 <PID>: Force terminate a process using its Process ID
  • netstat -nlp: List open network ports and associated processes

File Search

Use find to locate files/directories:

# Search Desktop for files containing "1"
find ~/Desktop -name "*1*"

# Find directories starting with "1" in /www
find /www -type d -name "1*"

# Find regular files named "config" in /home
find /home -type f -name "config"

File Compression & Extraction

Use tar for packaging and compression:

  • Compress: tar -zcvf archive.tar.gz file1 dir1
    • -z: Gzip compression, -c: Create archive, -v: Verbose output, -f: Specify filename
  • Extract: tar -zxvf archive.tar.gz
    • Extract to a specific directory: tar -zxvf archive.tar.gz -C /path/to/dir
  • List Contents: tar -ztvf archive.tar.gz

Examples:

# Package all .log files into log.tar
tar -cvf log.tar *.log

# Compress /etc into /tmp/etc.tar.gz
tar -zcvf /tmp/etc.tar.gz /etc

# Compress /home and /etc excluding /home/dmtsai
tar --exclude /home/dmtsai -zcvf myarchive.tar.gz /home/* /etc

Remote File Transfer

Use scp to transfer files between Linux systems (requires SSH access):

  • Remote to Local:
# Copy a single file
scp root@192.168.21.103:/opt/public ~/home
# Copy a directory recursively
scp -r root@192.168.21.103:/opt/public ~/home
  • Local to Remote:
# Copy a single file
scp ./9999 root@192.168.21.103:/opt/
# Copy a directory recursively
scp -r ./9999 root@192.168.21.103:/opt/

File Permissions Management

Use chmod to modify permissions. Permissions are defined for:

  • u: File owner, g: File group, o: All other users

Numeric permission values:

  • r (read): 4, w (write): 2, x (execute): 1

Examples:

# Add execute permission for the owner
chmod u+x script.sh
# Set full owner access, no access for others (700)
chmod 700 confidential.txt
# Add read/write for the group
chmod g+rw data.csv

A permission string like -rw-r--r-- means the owner can read/write, while group and others can only read.

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.