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
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
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>
killall
Example:
killall firefox
3. Disk Management
Linux treats disks as virtual directories, requiring manual mounting.
Mounting a Device:
mount -t vfat /dev/sdb1 /mnt/usb
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.
Example:
sort -n -r data.txt
Searching Files with grep
:
grep "error" logs.txt
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. ---
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
Persistent Variables:
Add to one of the following files for persistence:
$HOME/.bash_profile
$HOME/.bash_login
$HOME/.profile
6. Advanced Shell Features
Background Processes:
command &
jobs # List background jobs
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)