Linux Command Line Fundamentals: A Comprehensive Reference

Linux Directory Structure

The root directory (/) is the top-level directory in Linux. Unlike some operating systems, Linux has only one top-level directory: the root directory. Path relationships are described using the / separator. For example, /home/user/file.txt indicates that there is a file named file.txt in the user directory within the home directory, which is located under the root directory.

Basic File and Directory Commands

ls Command

Function: List directory contents

Syntax: ls [options] [directory]

  • directory: The directory to view. If not specified, shows the current working directory
  • -l: Display in list format with detailed information
  • -h: With -l, show file sizes in human-readable format
  • -a: Show hidden files (those starting with a dot)

Hidden Files and Directories

In Linux, any file or directory starting with a dot (.) is hidden. These files are not displayed by default and require the -a option to be visible.

pwd Command

Function: Display the current working directory

Syntax: pwd

cd Command

Function: Change the current working directory

Syntax: cd [target_directory]

Parameters: target_directory - The directory to switch to. If not specified, switches to the current user's HOME directory.

HOME Directory

Each user in Linux has a personal working directory called the HOME directory.

  • Regular users' HOME directories are typically located at: /home/username
  • The root user's HOME directory is at: /root

After logging into a terminal, the default working directory is the user's HOME directory.

Relative and Absolute Paths

  • Relative paths: Paths that do not start with / are relative paths. They describe the location relative to the current directory. For example, docs/report.txt refers to the report.txt file in the docs directory within the current working directory.
  • Absolute paths: Paths that start with / are absolute paths. They describe the location starting from the root directory.

Special Path Characters

  • .: Represents the current directory. For example, ./file.txt refers to file.txt in the current directory.
  • ..: Represents the parent directory. For example, ../ moves up one directory level, ../../ moves up two levels.
  • ~: Represents the user's HOME directory. For example, cd ~ returns to the user's HOME directory.

mkdir Command

Function: Create directories

Syntax: mkdir [options] directory_path

  • directory_path: The path of the directory to be created
  • -p: Create parent directories as needed

touch Command

Function: Create empty files

Syntax: touch file_path

  • file_path: The path of the file to be created

File Viewing Commands

cat Command

Function: Display file contents

Syntax: cat file_path

  • file_path: The path of the file to view

more Command

Function: View file contents with pagination support

Syntax: more file_path

  • file_path: The path of the file to view

While viewing:

  • Space key: Move to the next page
  • q: Exit the viewer

head Command

Function: Display the beginning of a file

Syntax: head [options] file_path

  • file_path: The path of the file to view
  • -n: Number of lines to display (default is 10)

tail Command

Function: Display the end of a file

Syntax: tail [options] file_path

  • file_path: The path of the file to view
  • -f: Follow file changes in real-time
  • -n: Number of lines to display from the end (default is 10)

File Manipulation Commands

cp Command

Function: Copy files and directories

Syntax: cp [options] source destination

  • source: The file or directory to copy
  • destination: The location to copy to
  • -r: Copy directories recursively

Examples:

  • cp document.txt backup.txt - Copy document.txt to backup.txt in the current directory
  • cp document.txt archive/ - Copy document.txt to the archive directory
  • cp -r project project_backup - Copy the project directory to project_backup

mv Command

Function: Move or rename files and directories

Syntax: mv source destination

  • source: The file or directory to move/rename
  • destination: The target location. If it doesn't exist, the source is renamed

rm Command

Function: Remove files and directories

Syntax: rm [options] file_or_directory...

  • file_or_directory: One or more files or directories to remove (space-separated)
  • -r: Remove directories recursively
  • -f: Force remove without confirmation prompts

Warning: The rm command is dangerous, especially when used as the root user.

Search and Filter Commands

which Command

Function: Find the location of a command's executable file

Syntax: which command_name

  • command_name: The command to locate

find Command

Function: Search for files

Syntax 1 (by name): find path -name search_pattern

  • path: The starting directory for the search
  • search_pattern: The search term, supports wildcards like * (matches any sequence of characters)

Example: find /home -name "*.txt" - Find all .txt files in the /home directory

grep Command

Function: Filter text using patterns

Syntax: grep [options] pattern file_path

  • options: -n to display line numbers in results
  • pattern: The search pattern (enclose in quotes if it contains spaces or special characters)
  • file_path: The file to search in

Note: The file path can be replaced by input from a pipe

wc Command

Function: Count lines, words, and characters

Syntax: wc [options] file_path

  • options:
  • -c: Count bytes
  • -m: Count characters
  • -l: Count lines
  • -w: Count words
  • file_path: The file to analyze

Note: The file path can be replaced by input from a pipe

Advanced Command Features

Pipe Operator |

Usage: command1 | command2

Function: Pass the output of the left command as input to the right command

Example: cat document.txt | grep "important" - Search for "important" in document.txt

Pipes can be nested: cat document.txt | grep "important" | grep "urgent"

echo Command

Function: Display text or variables

Syntax: echo [text_or_variable]

  • text_or_variable: The text or variable to display

Command Substitution ` `

Function: Execute the command inside backticks and use its output

Example: echo `pwd` - Display the current working directory

Redirection Operators

Function: Redirect command output to a file

  • >: Overwrite output to the specified file
  • >>: Append output to the specified file

System Management Commands

Software Installation

For CentOS systems:

yum [install|remove|search] [-y] package_name

  • install: Install a package
  • remove: Uninstall a package
  • search: Search for packages
  • -y: Automatically confirm prompts

For Ubuntu systems:

apt [install|remove|search] [-y] package_name

  • install: Install a package
  • remove: Uninstall a package
  • search: Search for packages
  • -y: Automatically confirm prompts

Note: Both yum and apt require root privileges

systemctl Command

Function: Control system services

Syntax: systemctl start|stop|restart|disable|enable|status service_name

  • start: Start a service
  • stop: Stop a service
  • restart: Restart a service
  • disable: Disable auto-start on boot
  • enable: Enable auto-start on boot
  • status: Check service status

Symbolic Links

Function: Create symbolic links (shortcuts) to files or directories

Syntax: ln -s source link_name

  • source: The original file or directory
  • link_name: The name and location of the symbolic link

Date and Time

Syntax: date [-d] [+format_string]

  • -d: Display date according to a given string, often used for date calculations
  • format_string: Control the date format using specific markers

Common format markers:

  • %Y: Year (4 digits)
  • %y: Year (2 digits)
  • %m: Month (01-12)
  • %d: Day (01-31)
  • %H: Hour (00-23)
  • %M: Minute (00-59)
  • %S: Second (00-60)
  • %s: Seconds since 1970-01-01 00:00:00 UTC

Examples:

  • date +"%Y-%m-%d" - Display date as 2023-11-15
  • date +"%Y-%m-%d %H:%M:%S" - Display date and time as 2023-11-15 14:30:00

Time Zone

To set the time zone to China:

timedatectl set-timezone Asia/Shanghai

Time Synchronization (ntp)

Function: Synchronize system time

Installation: yum install -y ntp

Service management: systemctl start|stop|restart|status|disable|enable ntpd

Manual time sync: ntpdate -u ntp.aliyun.com

IP Address

Format: a.b.c.d where a, b, c, d are numbers between 0-255

Special IPs:

  • 127.0.0.1: Represents the local machine (localhost)
  • 0.0.0.0: Can represent the local machine or any IP, depending on context

View IP: ifconfig

Hostname

Function: The name of the Linux system

View: hostname

Set: hostnamectl set-hostname new_hostname

Configuring Static IP in VMware

  1. Modify VMware network settings according to documentation
  2. Set static IP in Linux by editing the file: /etc/sysconfig/network-scripts/ifcfg-ens33

Example file content:

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"        # Change to static for fixed IP
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="1b0011cb-0d2e-4eaa-8a11-af7d50ebc876"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.88.131"    # IP address, set within your network range
NETMASK="255.255.255.0"    # Subnet mask
GATEWAY="192.168.88.2"     # Gateway, must match VMware configuration
DNS1="192.168.88.2"        # DNS server, typically same as gateway

Network Configuration Commands

ps Command

Function: Display process information

Syntax: ps -ef

Can be combined with grep for filtering: ps -ef | grep process_name

kill Command

Function: Terminate processes

Syntax: kill [-signal] process_id

  • signal: The signal to send (default is SIGTERM, which allows graceful termination)
  • process_id: The ID of the process to terminate

For force termination: kill -9 process_id (sends SIGKILL signal)

nmap Command

Function: Network scanning and security auditing

Syntax: nmap [options] target

  • target: IP address, hostname, or network range to scan

Example: nmap -sV 192.168.1.1 - Scan host 192.168.1.1 and detect service versions

netstat Command

Function: Display network connections, routing tables, interface statistics, etc.

Syntax: netstat [options]

To check port usage: netstat -anp | grep port_number

ping Command

Function: Test network connectivity

Syntax: ping [-c count] target

  • -c: Number of packets to send (default is unlimited)
  • target: IP address or hostname to ping

wget Command

Function: Download files from the web

Syntax: wget [options] url

  • url: The URL of the file to download

curl Command

Function: Transfer data from or to a server

Syntax: curl [options] url

  • url: The URL to interact with

Process Management Commands

top Command

Function: Display system processes and resource usage

Syntax: top

Interactive mode shortcuts:

  • q: Quit
  • M: Sort by memory usage
  • P: Sort by CPU usage
  • k: Kill a process

df Command

Function: Show disk space usage

Syntax: df [options]

  • -h: Show sizes in human-readable format

iostat Command

Function: Display CPU and I/O statistics

Syntax: iostat [options] [interval] [count]

  • interval: Time between reports in seconds
  • count: Number of reports to generate

sar Command

Function: Collect, report, and store system activity information

Syntax: sar [options] [interval] [count]

Environment Variables

Setting Environment Variables

Temporary setting: export variable_name=value

Permanent settings:

  • For the current user: Add to ~/.bashrc file in the user's HOME directory
  • For all users: Add to /etc/profile file

PATH Variable

The PATH variable stores directories where executable programs are located.

You can add custom paths to PATH to enable execution of custom commands from any location.

Syntax: export PATH=$PATH:/new/path

$ Operator

Function: Retrieve the value of an environment variable

Syntax: $variable_name

Examples:

  • echo $PATH - Display the value of the PATH variable
  • echo ${PATH}/bin - Display PATH value followed by /bin

Use {} when the variable name might be confused with surrounding text.

Compression and Archiving

tar Command

Function: Create and extract archives

Compression: tar -zcvf archive_name.tar.gz file1 file2 ... fileN

  • -z: Use gzip compression
  • -c: Create archive
  • -v: Verbose mode (show files being processed)
  • -f: Specify archive filename

Extraction: tar -zxvf archive_name.tar.gz [-C destination]

  • -z: Use gzip compression
  • -x: Extract archive
  • -v: Verbose mode
  • -f: Specify archive filename
  • -C: Optional, specify extraction directory

zip Command

Function: Create and extract zip archives

Compression: zip [-r] archive_name.zip file1 file2 ... fileN

  • -r: Recursively include directories

Extraction: unzip [-d destination] archive_name.zip

  • -d: Optional, specify extraction directory

User and Permission Management

su Command

Function: Switch user

Syntax: su [-] [username]

  • -: Create a login shell with the target user's environment
  • username: The user to switch to (default is root)

sudo Command

Function: Execute commands with superuser privileges

Configuration is done in the /etc/sudoers file using the visudo command

Example configuration:

username ALL=(ALL) NOPASSWD: ALL

This allows the specified user to execute any command with sudo without entering a password.

chmod Command

Function: Change file or directory permissions

Syntax: chmod [-R] permissions file_or_directory

  • permissions: The permission settings (e.g., 755 for rwxr-xr-x)
  • file_or_directory: The file or directory to modify
  • -R: Apply permissions recursively to directory contents

chown Command

Function: Change file or directory ownership

Syntax: chown [-R] [user][:group] file_or_directory

  • user: The new owner
  • group: The new group (optional)
  • file_or_directory: The file or directory to modify
  • -R: Apply ownership recursively to directory contents

User Group Management

Function: Manage user groups

Common commands:

  • groupadd: Create a new group
  • groupdel: Delete a group
  • groupmod: Modify a group

User Management

Function: Manage user accounts

Common commands:

  • useradd: Create a new user
  • userdel: Delete a user
  • usermod: Modify a user

getent Command

Function: Get entries from system databases

Examples:

  • getent group: View all system groups
  • getent passwd: View all system users

env Command

Function: Display all environment variables

Syntax: env

Tags: Linux Command Line System Administration Shell File Management

Posted on Sat, 05 Sep 2026 16:15:08 +0000 by linkskywalker