Top 20 Linux Commands You must Know

Top 20 Linux Commands You Must Know Today

Linux, the powerful open-source operating system, relies heavily on its command-line interface for efficient system management. Learning key commands can boost productivity and streamline workflows for users of all levels.

Top 20 Linux Commands You must Know

Top 20 Linux commands form the foundation of CLI proficiency. These essential commands enable users to navigate directories, manage files, monitor system resources, and perform various administrative tasks with ease. From basic operations like listing files with ‘ls’ to more advanced functions like process management with ‘top’, mastering these commands empowers users to harness the full potential of their Linux systems.

By familiarizing themselves with these crucial commands, users can gain better control over their Linux environment. Whether it’s creating directories with ‘mkdir’ or modifying file permissions using ‘chmod’, these commands provide the tools needed to interact effectively with the Linux terminal. As users become more comfortable with the CLI, they often find it faster and more precise than graphical interfaces for many tasks.

Getting Started with the Terminal and Top 20 Linux Commands

Top 20 Linux Commands You must Know

The Linux command line is a powerful tool for managing your system. It allows direct interaction with the operating system through text-based commands. Learning basic commands and navigation skills is key to using Linux effectively.

Understanding the Terminal

The terminal is the interface used to enter commands in Linux. It’s a program that reads and runs commands you type in. To open a terminal, look for an icon that says “Terminal” or press Ctrl+Alt+T on most Linux systems.

When you open the terminal, you’ll see a prompt. It usually shows your username, computer name, and current directory. The prompt ends with a $ sign for regular users or a # for the root user.

Commands are typed after the prompt and run by pressing Enter. Many commands have options that change how they work. These options start with a dash (-) or two dashes (–).

Moving around the file system is a basic skill for using Linux. The pwd command shows your current location. It stands for “print working directory”.

To change directories, use the cd command. Type “cd” followed by the directory name. For example, “cd Documents” moves you to the Documents folder.

The ls command lists files and folders in the current directory. Use “ls -l” for a detailed list or “ls -a” to see hidden files.

To create a new file, use the touch command. For example, “touch newfile.txt” creates an empty file named newfile.txt.

Linux Directory Structure

Linux uses a tree-like directory structure. The root directory (/) is at the top. All other directories branch off from it.

Some key directories include:

  • /home: User home directories
  • /etc: System configuration files
  • /var: Variable data like logs
  • /tmp: Temporary files
  • /usr: User programs and data

The mkdir command creates new directories. For example, “mkdir newdir” makes a new folder called newdir in your current location.

Understanding this structure helps you find files and manage your system more easily.

File Management Commands

Linux offers powerful tools for handling files and directories. These commands let users create, edit, move, and delete files efficiently. They also help manage permissions and inspect file contents.

Creating and Editing Files

The cat command is useful for creating small files. To make a new file, type “cat > filename” and press Enter. Type the content and press Ctrl+D to save.

For larger files, text editors like vi are better. To open a file in vi, type “vi filename”. Press “i” to enter insert mode and start typing. Press Esc and type “” to save and quit.

The touch command creates empty files quickly. Simply type “touch filename” to make a new file or update an existing file’s timestamp.

Copying, Moving, and Removing Files

The cp command copies files. Its basic syntax is “cp source destination”. To copy a file to a new location, use “cp file.txt /new/path/”.

To move or rename files, use the mv command. The syntax is “mv oldname newname” or “mv file /new/location/”.

The rm command deletes files. Use it carefully as deleted files can’t be recovered easily. The basic syntax is “rm filename”. Add the -r flag to remove directories and their contents.

Working with Directories

The mkdir command creates new directories. Type “mkdir dirname” to make a new folder. Use the -p flag to create parent directories if they don’t exist.

To remove empty directories, use the rmdir command. For non-empty directories, use “rm -r dirname”.

The cd command changes the current directory. Type “cd dirname” to move into a directory, or “cd ..” to go up one level.

The pwd command shows the current directory path.

File Permissions and Ownership

The chmod command changes file permissions. It uses numbers or letters to set read, write, and execute permissions. For example, “chmod 755 file” gives full access to the owner and read/execute to others.

Chown changes file ownership. The syntax is “chown user filename”. This command often requires root privileges.

The ls -l command shows file permissions and ownership. Each file listing starts with ten characters showing its type and permissions.

Inspecting File Content and Metadata

The less command views file contents page by page. It’s great for large files. Type “less filename” to use it.

The more command is similar but older. It shows file content one screen at a time.

The head and tail commands show the start or end of a file. Use “head -n 10 file” to see the first 10 lines.

The file command shows file type information. It’s useful when a file lacks an extension.

The find command searches for files in a directory hierarchy. It’s powerful for locating files based on various criteria.

Data Manipulation and Processing

Linux provides powerful tools for handling data. These commands let users search, sort, compare, and organize information efficiently. They also offer ways to compress files for storage or transfer.

Searching and Sorting Data

The grep command is essential for searching text in files. It finds lines that match a given pattern. For example:

grep "error" logfile.txt

This searches for “error” in logfile.txt.

The sort command arranges text alphabetically or numerically. It can sort files or command output. To sort a file by its second column:

sort -k2 data.txt

Users can combine grep and sort. This finds all errors and sorts them:

grep "error" logfile.txt | sort

Comparing and Filtering Content

The comm command compares two sorted files line by line. It shows lines unique to each file and lines they share. Its basic use is:

comm file1.txt file2.txt

The diff command finds differences between files. It’s useful for tracking changes. To compare two files:

diff oldfile.txt newfile.txt

Both comm and diff help in data analysis and version control.

Compressing and Archiving Files

The tar command creates archives of multiple files. It’s often used with compression. To create a compressed archive:

tar -czvf archive.tar.gz folder/

This command makes a gzipped tar archive of a folder.

The zip and unzip commands work with .zip files. To compress:

zip -r archive.zip folder/

To extract:

unzip archive.zip

These tools help manage large datasets and backups.

System Administration and Monitoring

Linux provides powerful tools for managing system resources, monitoring performance, and controlling user access. These commands help administrators keep systems running smoothly and securely.

Managing Processes

The ps command shows running processes. It displays process IDs, CPU usage, and more. To see all processes, use ps aux.

Top gives a real-time view of system activity. It shows CPU, memory, and process info. Press q to exit top.

Htop is an improved version of top with a color interface and mouse support. It allows easier process viewing and management.

The kill command stops processes. Use kill -9 PID to force quit a program that’s not responding.

Admins often need to switch users. The su command lets you become another user, like su john.

Monitoring System Performance

Free shows memory usage. The -h flag makes output human-readable: free -h.

Df reports disk space usage. Use df -h to see available space on all mounted filesystems.

Du estimates file and directory sizes. To check a folder’s size: du -sh /path/to/folder.

Iostat reports CPU stats and I/O stats for devices and partitions. It helps identify bottlenecks.

Netstat displays network connections. Use netstat -tuln to see listening ports.

User and Group Management

The useradd command creates new user accounts. Example: useradd -m john creates a user “john” with a home directory.

Groupadd makes new groups. To create a “developers” group: groupadd developers.

Passwd changes user passwords. To change your own password, simply type passwd.

Usermod modifies user accounts. To add a user to a group: usermod -aG groupname username.

Chage manages password expiry info. Set a password expiration: chage -E 2024-12-31 username.

Controlling Services and Daemons

The service command starts, stops, and restarts system services. To restart a service, use the following command: service apache2 restart.

Systemctl is the modern replacement for service. It manages system services and resources.

To start a service, type: systemctl start servicename.

Check a service’s status by running: systemctl status servicename.

Enable a service to start at boot using: systemctl enable servicename.

Networking Commands

Linux provides powerful tools for managing network connections and transferring files. These commands help users configure network settings, analyze network traffic, and download or upload data securely.

Network Configuration and Analysis

The ip command is a versatile networking tool for Linux systems. It allows users to view and configure routing, interfaces, and network devices. To see network interface details, type “ip addr show” in the terminal.

The ping command checks if a host is reachable. It sends small data packets to the target and measures response time. For example, “ping google.com” tests connectivity to Google’s servers.

Ifconfig, though older, remains useful for viewing and changing network interface settings. It displays IP addresses, netmasks, and other network information.

The ss command shows socket statistics. It lists active network connections and helps troubleshoot network issues. Use “ss -tuln” to see all TCP and UDP listening ports.

File Transfer and Download

SSH (Secure Shell) enables secure remote access to other computers. It encrypts data transmission, protecting sensitive information. To connect to a remote server, use “ssh username@hostname”.

FTP (File Transfer Protocol) moves files between computers on a network. While not as secure as SSH, it’s still widely used. Many servers offer anonymous FTP access for public file downloads.

The wget command downloads files from the web. It supports HTTP, HTTPS, and FTP protocols. To download a file, simply type “wget” followed by the file’s URL. Wget can also download entire websites for offline viewing.

Advanced Command-Line Techniques

Linux power users use sophisticated techniques to boost productivity and system control. These methods leverage built-in tools and customization options to streamline workflows and manage software efficiently.

Shell Scripting and Task Automation

Shell scripting allows users to automate repetitive tasks and create complex workflows. The alias command enables quick shortcuts for frequently used commands.

To create an alias:

alias update='sudo apt update && sudo apt upgrade'

This creates a shortcut named “update” that runs system updates.

The crontab utility schedules tasks to run automatically. Users can set up recurring backups, system maintenance, or custom scripts.

To edit the crontab file, use the following command:

crontab -e

For example, here’s a crontab entry to run a script daily at 3 AM:

0 3 * * * /path/to/script.sh

Customizing the Shell Environment

Tailoring the shell environment enhances usability and efficiency. The env command displays current environment variables.

Users can modify the .bashrc or .zshrc file to set custom variables, aliases, and functions. This personalizes the command-line experience.

Here’s an example of .bashrc customization:

export PATH=$PATH:/custom/bin
PS1='\u@\h:\w\$ '

The history command shows previously executed commands. Users can search and rerun past commands using shortcuts like !n or !command.

Package Management

Linux distributions use package managers for software installation and updates. Common package managers include apt, dnf, and yum.

To update package lists, run the following command:

sudo apt update

To install a package, use the following command:

sudo apt install package_name

And to remove a package, use the following command:

sudo apt remove package_name

Package managers handle dependencies automatically, ensuring smooth software installation and removal.

Users can also add third-party repositories to access additional software packages not available in the default repositories.

Help and Documentation

Linux provides extensive resources to help users understand and use commands effectively. These tools offer detailed explanations and examples for command usage.

Accessing Manual Pages

The man command is a key tool for accessing manual pages in Linux. It gives detailed info on command syntax, options, and usage.

To use man, type “man” followed by the command name. For example, “man ls” shows the manual for the ls command.

Manual pages are divided into sections. The first section covers user commands, while others deal with system calls, libraries, and more.

Some commands have multiple manual pages. Use “man -k keyword” to search for related pages.

Info Pages and Online Resources

Info pages offer another way to get help in Linux. They often provide more detailed and structured information than man pages.

To access info pages, use the “info” command followed by the command or topic name. For example, “info grep” shows info for the grep command.

The which command helps find the location of a command in your system path. It’s useful for troubleshooting command issues.

Many Linux distributions maintain online documentation. These resources often include user guides, FAQs, and forums for community support.

Websites like the Linux Documentation Project offer comprehensive guides and HOWTOs for various Linux topics.

Security and Permission Management

Linux provides robust tools for managing security and access permissions. These commands help protect systems and data from unauthorized access.

The chmod command changes file permissions. It controls who can read, write, or execute files. Users can set permissions for the owner, group, and others.

Chown changes file ownership. It allows administrators to transfer file ownership between users or groups. This is useful for managing access to shared resources.

The sudo command grants temporary admin privileges. It lets users run commands with elevated permissions. This enhances security by limiting full root access.

SSH (Secure Shell) enables secure remote access. It encrypts connections between computers, protecting data from interception. SSH keys provide an extra layer of security for remote logins.

Umask sets default permissions for new files and directories. It determines the initial access rights when users create new items. This helps maintain consistent security policies.

Share this article:
As a passionate DevOps Engineer, I thrive on bridging the gap between development and operations. My expertise lies in crafting efficient, scalable infrastructure solutions, with a particular fondness for Linux and Ubuntu environments. I'm constantly exploring innovative ways to streamline processes, enhance system reliability, and boost productivity through automation. My toolkit includes a wide array of cutting-edge technologies and best practices in continuous integration, deployment, and monitoring. When I'm not immersed in code or fine-tuning server configurations, you'll find me staying up-to-date with the latest industry trends and sharing knowledge with the tech community. Let's connect and discuss how we can revolutionize your infrastructure!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *