DEV Community

KALPESH
KALPESH

Posted on

Linux Filesystem & Essential Linux Commands

Linux Directory Structure

/ - Root (base) directory 🌳

/root - Home directory for root user πŸ‘‘

/home - User home directories (/home/username) 🏠

/bin - Symlink to /usr/bin (user command binaries) βš™οΈ

/sbin - Symlink to /usr/sbin (system admin binaries) πŸ› οΈ

/usr/bin - Actual location of user binaries (ls, cat, docker) πŸ“¦

/usr/sbin - System administration binaries (useradd, nginx) πŸ”§

/usr/local/bin - Manually installed binaries (not managed by package manager) πŸ“₯

/lib - Essential system libraries for /bin and /sbin πŸ“š

/opt - Third-party/commercial software packages (self-contained) 🧩

/var - Runtime data, logs, application state (Docker, Jenkins) πŸ“Š

/etc - Configuration files (editable text configs) πŸ“

/tmp - Temporary files (cleared on reboot) ⏳

/proc - Virtual filesystem with process/system information πŸ”

/sys - Virtual filesystem with hardware/kernel information πŸ–₯️

/boot - Boot loader files and kernel πŸš€

/dev - Device files (hardware interfaces) πŸ”Œ

/mnt - Manual mount point for filesystems πŸ’½

/media - Auto-mount point for removable devices πŸ“€

/run - Runtime process data (PIDs, sockets) πŸƒ

The PATH Variable

The PATH variable in Linux is an environment variable that specifies a list of directories where executable programs are located. When you run a command in the terminal, the system searches through these directories to find the executable file corresponding to that command.

Format of the PATH Variable

The PATH variable consists of a colon (:) separated list of directories. Here's an example:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
Enter fullscreen mode Exit fullscreen mode

Example of Setting the PATH Variable

To include a new directory, such as where you have installed Terraform, you can modify the PATH variable as follows:

export PATH=$PATH:/usr/local/terraform
Enter fullscreen mode Exit fullscreen mode

After editing the file, you can apply the changes immediately by sourcing the file:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Linux Commands

Gaining Root User Access

sudo su -
logout
Enter fullscreen mode Exit fullscreen mode

Finding path

which
Enter fullscreen mode Exit fullscreen mode

Piping Commands

# Use the output of the first command as input for the second
|
Enter fullscreen mode Exit fullscreen mode

Debugging Commands

# Print command in debug mode
set -x
Enter fullscreen mode Exit fullscreen mode

Error Handling in Scripts

# Exit the script when there is an error
set -e

# Exit the script when there is a pipe failure
set -o pipefail
Enter fullscreen mode Exit fullscreen mode

Disk and Memory Usage

# Disk usage in human-readable format
df -h

# Memory usage
free

# Number of processing units
nproc
Enter fullscreen mode Exit fullscreen mode

Real-Time Performance Monitoring

# Monitor performance in real-time
top

# All processes running in the background
ps -ef
Enter fullscreen mode Exit fullscreen mode

Filtering Output

# Basic filtering command
grep

# Powerful filtering and text processing
awk
Enter fullscreen mode Exit fullscreen mode

Retrieving and Downloading Information from the Internet

# Retrieve information from the internet
curl

# Download files from the internet
wget
Enter fullscreen mode Exit fullscreen mode

Searching for Files

# Search for the location of a file
find / -name test.sh
Enter fullscreen mode Exit fullscreen mode

Trapping Signals in Linux

trap
Enter fullscreen mode Exit fullscreen mode

Working with Processes

# List all processes
ps -ef

# Print errors from remote logs
curl ... | grep ...
Enter fullscreen mode Exit fullscreen mode

String Manipulation

# Write number of occurrences of 's' in the word 'missi'
x=missi
grep -o "s" <<<"$x" | wc -l
Enter fullscreen mode Exit fullscreen mode

Scheduling Tasks with Crontab

# Crontab in Linux
Alarm
Enter fullscreen mode Exit fullscreen mode

Editing Files

# Open a file in read-only mode
vim -r test.txt
Enter fullscreen mode Exit fullscreen mode

Soft Links vs. Hard Links

Soft Link

  • Acts as a shortcut reference to the original file

  • The link can be broken if the original file is deleted or moved

Hard Link

  • Acts as a copy

  • Deleting the original file does not affect the hard link

Network Troubleshooting Utilities

Traceroute
Tracepath
Enter fullscreen mode Exit fullscreen mode

Managing Large Log Files

Logrotate
Enter fullscreen mode Exit fullscreen mode

Filtering JSON Data

# Filter data from JSON
jq
Enter fullscreen mode Exit fullscreen mode

Filtering YAML Data

# Filter data from YAML
yq
Enter fullscreen mode Exit fullscreen mode

Feel free to share and spread the knowledge! 🌟😊 Enjoy Learning! 😊

Top comments (0)