1080*80 ad

Linux Command Gems

Master Your Command Line: Powerful Linux Commands You Should Be Using

In a world dominated by graphical user interfaces (GUIs), the Linux command line remains the undisputed domain of power, speed, and precision for developers, system administrators, and enthusiasts alike. While commands like ls, cd, and grep are part of the daily toolkit, a deeper arsenal of utilities can transform your workflow from productive to truly exceptional.

Mastering these commands allows you to diagnose problems, manage resources, and automate tasks with an efficiency that GUIs simply cannot match. Here are some indispensable command-line gems that will elevate your skills and give you deeper control over your system.

System Monitoring and Analysis

Understanding what’s happening under the hood is the first step to managing a healthy system. These tools give you an unparalleled view of your machine’s resources.

1. htop – The Interactive Process Viewer

While top is a reliable classic, htop is its modern, user-friendly successor. It provides a real-time, color-coded overview of your system’s processes, CPU usage, and memory status.

  • Why it’s essential: Unlike top, htop allows you to scroll vertically and horizontally to see all processes and their full command lines. You can also kill processes, change their priority (renice), and filter them interactively without ever leaving the interface. It’s a must-have for any sysadmin.
  • How to use it: Simply type htop in your terminal. Use the arrow keys to navigate and F9 to send a kill signal to a selected process.

2. ncdu – NCurses Disk Usage

Ever wondered where all your disk space has gone? The du -sh * command is useful, but it can be slow and cumbersome to navigate. Enter ncdu, an incredibly fast and interactive disk usage analyzer.

  • Why it’s essential: ncdu scans a given directory and presents a navigable, size-ordered list of files and folders. This allows you to quickly drill down into the largest directories to identify space hogs. It’s significantly faster than traditional du on subsequent runs because it caches information.
  • How to use it: Run ncdu / to scan your entire filesystem or ncdu in your current directory. Use the arrow keys to navigate and d to delete a selected file or folder.

3. lsof – List Open Files

In Linux, nearly everything is treated as a file, including network connections, devices, and pipes. The lsof command gives you a snapshot of which processes have which files open.

  • Why it’s essential: This is a powerhouse for debugging. You can instantly see which process is using a specific port, find out why a disk cannot be unmounted (“device is busy”), or inspect the files a particular user has open.
  • How to use it: To find out what process is using port 80, run:
    bash
    sudo lsof -i :80

Advanced Network Troubleshooting

Network issues can be complex and frustrating. These commands provide the clarity needed to diagnose and resolve them quickly.

4. ss – The Socket Statistics Powerhouse

For years, netstat was the standard for inspecting network connections. However, ss has replaced it as the faster, more informative modern tool. It pulls its information directly from the kernel, making it much more efficient.

  • Why it’s essential: It can display more information than netstat and does so with incredible speed, which is critical on servers with thousands of connections. You can easily filter for specific states like LISTEN, ESTABLISHED, or by port number.
  • How to use it: To list all listening TCP and UDP ports, run:
    bash
    ss -tuln

    • -t: Show TCP sockets.
    • -u: Show UDP sockets.
    • -l: Show listening sockets.
    • -n: Do not resolve service names (shows port numbers instead).

5. dig – The DNS Authority

When you need detailed information about DNS records, nslookup is fine, but dig (Domain Information Groper) is the professional’s choice.

  • Why it’s essential: dig provides highly detailed, easy-to-read output about DNS queries. It is the go-to tool for troubleshooting DNS issues, checking MX records for email delivery, or verifying that a DNS change has propagated.
  • How to use it: To get a detailed A record lookup, simply use:
    bash
    dig example.com

    To query for mail exchange (MX) records:
    bash
    dig example.com MX

Mastering File and Text Manipulation

The true power of the command line lies in its ability to manipulate data and automate operations across thousands of files.

6. find – The Ultimate File Search Tool

The find command is infinitely more powerful than a simple search. It can locate files and directories based on a vast array of criteria, including name, size, modification time, owner, and permissions.

  • Why it’s essential: Its true power comes from its ability to execute commands on the files it finds. You can build powerful, one-line scripts to perform bulk operations, such as deleting all .tmp files older than 7 days or changing permissions on all files within a directory tree.
  • How to use it: To find all files ending in .log in the /var directory and delete them, run:
    bash
    find /var -name "*.log" -type f -delete

7. xargs – The Command Line Supercharger

xargs is a brilliant utility that reads streams of data from standard input and then executes a specified command using that data as arguments.

  • Why it’s essential: It acts as a bridge between commands. Many commands can only accept a few arguments at a time. xargs breaks up long lists of inputs into manageable chunks, making it possible to perform operations on thousands of files found with find or ls.
  • How to use it: A classic combination is using find to locate files and xargs to perform an action on them. To find all PNG files and optimize them with optipng:
    bash
    find . -name "*.png" -print0 | xargs -0 optipng

    (The -print0 and -0 flags handle filenames that contain spaces.)

A Crucial Note on Security and Permissions

No discussion of powerful commands is complete without mentioning security. The ability to control access to files and directories is a cornerstone of the Linux operating system.

chmod and chown – Your Security Gatekeepers

  • chown (change owner): This command changes the user and/or group ownership of a given file or directory.
  • chmod (change mode): This command modifies the read, write, and execute permissions for the owner, group, and all other users.

Actionable Security Tip: Always adhere to the Principle of Least Privilege. Files and directories should only have the permissions they absolutely need to function. For example, website files should be owned by your web user and should not be world-writable. A common secure permission set for web directories is 755 and for files is 644.

  • To recursively set directory permissions:
    bash
    find /var/www/html -type d -exec chmod 755 {} \;
  • To recursively set file permissions:
    bash
    find /var/www/html -type f -exec chmod 644 {} \;

By integrating these powerful commands into your regular workflow, you not only increase your efficiency but also gain a more profound understanding and control of the systems you manage. The command line is a deep and rewarding environment, and these gems are just the beginning.

Source: https://linuxhandbook.com/ebooks/linux-command-nuggets/

900*80 ad

      1080*80 ad