DEV Community

Theerej C
Theerej C

Posted on

Mastering Linux: Essential Commands for System Management and Shell

Linux offers a wealth of commands for managing processes, files, and system resources. In this post, we'll explore key Linux commands in process management, disk management, file operations, and environment variables with examples.


1. Process Management with ps and top

Monitoring processes is crucial in Linux. The ps command displays process information, while top offers real-time monitoring.

Key ps Options:

  • a - Displays all processes.
  • T - Shows processes associated with the current terminal.
  • l - Long format output.
  • r - Running processes only.
  • S - Includes child processes in parent stats.

Example:

ps -alx
Enter fullscreen mode Exit fullscreen mode

top Command:

  • 1st line: System load averages (1, 5, 15 minutes). A high 15-min load indicates system stress.
  • f - Customize displayed fields.

Example:

top
Enter fullscreen mode Exit fullscreen mode

2. Killing Processes with kill and killall

The kill command terminates processes using their process ID (PID).

Signals:

  • TERM (15) - Default, graceful termination.
  • KILL (9) - Forceful termination.

Example:

kill -9 <PID>
Enter fullscreen mode Exit fullscreen mode

killall Example:

killall firefox
Enter fullscreen mode Exit fullscreen mode

3. Disk Management

Linux treats disks as virtual directories, requiring manual mounting.

Mounting a Device:

mount -t vfat /dev/sdb1 /mnt/usb
Enter fullscreen mode Exit fullscreen mode

Key Commands:

  • mount - Lists mounted devices.
  • umount /mnt/usb - Unmounts devices.
  • df -h - Disk usage summary.
  • du -c - Directory size with total.

4. File Management

Sorting, searching, and compressing files are frequent tasks.

Sorting Files with sort:

  • -n - Numerical sort.
  • -r - Descending order.
  • -t - Specify delimiter.
  • -M - Month sort.

Image description

Example:

sort -n -r data.txt
Enter fullscreen mode Exit fullscreen mode

Searching Files with grep:

grep "error" logs.txt
Enter fullscreen mode Exit fullscreen mode

Zipping and Archiving:

  • gzip file.txt - Compress a file.
  • gunzip file.txt.gz - Decompress a file.
  • tar -cvf archive.tar files/ - Create an archive.
  • tar -xvf archive.tar - Extract files from the archive. Image description ---

5. Environment Variables

Environment variables define system behavior.

View and Set Variables:

  • printenv / env - List environment variables.
  • set - Lists all variables.
  • unset VAR - Removes a variable.

Example:

export MY_VAR="Hello Linux"
echo $MY_VAR
Enter fullscreen mode Exit fullscreen mode

Persistent Variables:

Add to one of the following files for persistence:

$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
Enter fullscreen mode Exit fullscreen mode

6. Advanced Shell Features

Background Processes:

command &
jobs  # List background jobs
Enter fullscreen mode Exit fullscreen mode

Command History:

  • history - Show command history.
  • !! - Re-run the last command.

By mastering these Linux commands, you’ll boost productivity and manage your system efficiently. Happy coding! 🚀

Top comments (0)