Useful Linux Commands

When hearing about Linux, most people think of a complicated operating system that is only used by programmers. But it’s not as scary as it seems. Linux is an entire family of open-source Unix operating systems, that are based on the Linux Kernel. This includes all of the most popular Linux based systems like Ubuntu, Fedora, Mint, Debian, and others. More accurately, they’re called distributions or distros.

P
Prem Mohan Tyagi
4 min | 767 words
Useful Linux Commands

Know Your Linux: Essential Commands for Ubuntu, CentOS, and Debian


System Information and Basic Commands:

  1. Check Linux Version:

    • cat /etc/redhat-release - Check Linux version (for CentOS/RedHat).
    • hostnamectl - Display or change the system's hostname.
    • uname - Display system information (OS, kernel version).
  2. Memory and Disk Usage:

    • cat /proc/meminfo - Check memory information.
    • df -Ph - Display disk space usage in human-readable format.
    • du -h /directory/ - Check disk usage of a directory.
    • du -hc --max-depth=1 /directory/ - Summary of disk usage.

File and Directory Operations:

  1. Listing and Navigating Directories:

    • ls - List directory contents.
    • ls -R - List contents recursively (including subdirectories).
    • ls -a - Show hidden files.
    • ls -al - Detailed file information (permissions, size, etc.).
    • pwd - Display the current directory path.
    • cd /path/to/directory - Change directories.
    • cd .. - Move up one directory.
    • cd - - Go to the previous directory.
    • cd - Go to the home directory.
  2. Creating and Removing Directories:

    • mkdir DirectoryName - Create a new directory.
    • rmdir DirectoryName - Remove an empty directory.
    • rm -r DirectoryName - Remove a directory and its contents.
  3. File Operations:

    • touch filename.txt - Create a blank file.
    • cat filename.txt - View file contents.
    • cat > filename - Create a new file.
    • cp filename /path/to/destination/ - Copy files.
    • mv filename /new/location/ - Move or rename files.
    • ln -s target linkname - Create symbolic links between files.
    • chmod 755 filename - Change file permissions.
    • chown username filename - Change file ownership.
  4. Comparing and Sorting Files:

    • diff file1 file2 - Compare files line by line.
    • cmp file1 file2 - Compare two files byte by byte.
    • comm file1 file2 - Compare two sorted files.
    • sort - Sort lines of a text file.

Advanced File and Archive Operations:

  1. Archiving and Compression:

    • tar -cvf archive.tar /directory/ - Create an archive.
    • tar -xvf archive.tar - Extract an archive.
    • zip filename.zip files - Compress files into a .zip archive.
    • unzip filename.zip - Extract files from a .zip archive.
  2. Concatenation and Case Conversion:

    • cat filename1 filename2 > filename3 - Merge two files.
    • cat filename | tr a-z A-Z > output.txt - Convert a file to uppercase.

Process and System Management:

  1. Managing Users:

    • adduser newuser - Add a new user.
    • userdel newuser - Delete a user.
    • passwd newuser - Change a user's password.
    • usermod -aG sudo newuser - Add user to sudo group (Ubuntu/Debian).
    • usermod -aG wheel newuser - Add user to sudo group (CentOS).
    • groups newuser - Verify user's group.
    • su - newuser - Access the system as a new user.
  2. Monitoring and Managing Processes:

    • ps - Display running processes.
    • kill PID - Terminate a process by its Process ID.
    • killall processname - Terminate all processes by name.
    • top - Monitor system resource usage in real time.
  3. Services and Network:

    • service service_name start/stop/restart - Manage system services.
    • ifconfig - Configure or view network interfaces.
    • ping hostname - Test network connectivity to a host.
    • traceroute hostname - Trace the path packets take to a host.

Searching, Networking, and Miscellaneous:

  1. Searching Files and Text:

    • locate filename - Find a file by name.
    • find /path -name filename.txt - Search for files within a directory.
    • grep "search_term" filename.txt - Search for patterns within files.
    • grep -r "search_term" /directory/ - Search recursively within a directory.
  2. Networking and Downloads:

    • wget URL - Download files from the internet.
    • ssh user@host - Securely connect to a remote system.
  3. System Updates and Package Management:

    • apt, yum, rpm, pacman - Install and manage software packages (varies by Linux distribution).
    • sudo command - Run commands with superuser privileges.
    • history - View command history of the current session.

Utilities and Miscellaneous:

  1. File and Data Management:

    • df -Ph - Check disk space usage in human-readable format.
    • head -n 5 filename.txt - View the first five lines of a file.
    • tail -n 5 filename.txt - View the last five lines of a file.
    • cal - Display a calendar.
  2. Firewall and Security:

    • ufw - Manage firewall rules (Ubuntu).
    • iptables - Manage packet filtering rules.
  3. System and Command Information:

    • alias - Create shortcuts for commands.
    • wheris command - Locate the binary, source, and manual pages for a command.
    • whatis command - Display a one-line description of a command.
  4. Low-level Operations:

    • dd if=input of=output - Convert and copy files at a low level.
    • export - Set environment variables for the current session.

Final Tips:

  • Run Multiple Commands:

    • Use Command1; Command2 to run multiple commands sequentially.
    • Use Command1 && Command2 to run the second command only if the first is successful.
  • Clear Terminal:

    • Use clear to clean the terminal screen.

Share this article

P

Written by

Prem Mohan Tyagi