Home/Blogs/Important Linux Commands Everyone Should Know

Important Linux Commands Everyone Should Know

Important Linux Commands Everyone Should Know

Hamed Rohani

Author
July 13th 2024
89 views

Introduction

As everyone knows, there are many bash commands that can serve specific needs, but knowing some of those commands are foundation for everything.

I would like to list those command first to give a big picture.

ls
cd
mkdir
rm
cp
mv
chmod
grep
find
tar
vi
cat
top
ps
kill
du
ipconfig
ping

The ls Command

The ls command is used to list the contents of a directory. It displays the files and directories within the current directory by default, but it can also be used with various options to modify its output.

Usage Example

List all files and directories in the current directory

List all files, including hidden files, in the current directory

List files in the long format, showing details like permissions, owner, size, and modification date

List files in a specific directory

Example: Listing files in the current directory in long format including hidden files

The cd Command

The cd command is used to change the current working directory in Linux. It allows you to navigate between directories in the file system.

Usage Example

Change to the home directory

Change to a specific directory

Move up one directory level

Move up two directory levels

Change to the previous directory

The mkdir Command

The mkdir command is used to create new directories in Linux. You can create single or multiple directories at once with this command.

Usage Example

Create a single directory

Create multiple directories

Create a directory with parent directories

Example: Creating a nested directory structure

The rm Command

The rm command is used to remove files or directories in Linux. Be cautious when using this command, as it permanently deletes the specified files or directories.

Usage Example

Remove a single file

Remove multiple files

Remove an empty directory

Remove a directory and its contents recursively

Force remove a directory and its contents without prompting for confirmation

Example: Removing a file and a directory with its contents

The cp Command

The cp command is used to copy files and directories in Linux. It allows you to create duplicates of files or entire directory structures.

Usage Example

Copy a single file

Copy multiple files to a directory

Copy a directory and its contents recursively

Preserve the attributes of the files while copying

Provide verbose output showing the files being copied

Example: Copying a file and a directory with its contents

The mv Command

The mv command is used to move or rename files and directories in Linux. It can be used to relocate files and directories or to rename them.

Usage Example

Move a file to a different directory

Move multiple files to a different directory

Rename a file

Move a directory and its contents to a different location

Rename a directory

Example: Moving a file and renaming a directory

The chmod Command

The chmod command is used to change the file permissions in Linux. It allows you to set who can read, write, or execute a file or directory.

Usage Example

Change permissions to read, write, and execute for the owner, and read and execute for others

Change permissions to read and write for the owner, and read-only for others

Add execute permission for the owner

Remove write permission for the group

Set specific permissions using symbolic mode

Example: Changing permissions of a file and a directory

The grep Command

The grep command is used to search for patterns within files in Linux. It prints lines that match the specified pattern.

Usage Example

Search for a pattern in a file

Search for a pattern in multiple files

Perform a case-insensitive search

Display line numbers with matching lines

Search for a pattern recursively in a directory

Example: Searching for the word "example" in a file and a directory

The find Command

The find command is used to search for files and directories in a directory hierarchy in Linux. It provides a wide range of options for locating files based on various criteria.

Usage Example

Find files by name

Find files by type (e.g., directories)

Find files by size (e.g., larger than 50MB)

Find files by modification time (e.g., modified in the last 7 days)

Find files and execute a command on each file

Example: Finding files named "example.txt" and directories named "example_directory"

The tar Command

The tar command is used to create, maintain, modify, and extract files that are archived in the tar format. It is commonly used for backup and distribution of files.

Usage Example

Create a tar archive

Extract a tar archive

Create a compressed tar.gz archive

Extract a tar.gz archive

List the contents of a tar archive

Example: Creating and extracting a tar.gz archive

The vi Command

The vi command is used to open the vi text editor, which allows you to create and edit text files in Linux. It is a powerful editor with two primary modes: Command mode and Insert mode.

Usage Example

Open a file in vi

Basic Commands

Enter Insert Mode

Press i to switch to Insert mode and start editing the file.

Save and Exit

While in Command mode, type :wq and press Enter to save changes and exit vi.

Exit Without Saving

While in Command mode, type :q! and press Enter to exit vi without saving changes.

Save Without Exiting

While in Command mode, type :w and press Enter to save changes without exiting.

Example: Editing a file with vi

The cat Command

The cat command is used to concatenate and display the contents of files in Linux. It is commonly used to read and output the contents of files to the terminal.

Usage Example

Display the contents of a file

Display the contents of multiple files

Concatenate multiple files into a single file

Append the contents of a file to another file

Display line numbers along with the file contents

Example: Displaying and concatenating files

The top Command

The top command is used to display real-time information about the system's running processes. It provides a dynamic view of the system's resource usage, including CPU and memory usage.

Usage Example

Start the top command

Display help within top

Press h while top is running to display help information.

Sort processes by memory usage

Press M while top is running to sort processes by memory usage.

Kill a process

Press k while top is running, then enter the PID of the process you want to kill.

Quit top

Press q while top is running to quit the top command.

Example: Using top to monitor system processes

The ps Command

The ps command is used to display information about active processes in Linux. It provides a snapshot of current processes at the time of execution.

Usage Example

Display all running processes

Display detailed information about running processes

Display processes for the current user

Display processes in a hierarchical format

Display processes by a specific PID

Example: Displaying all processes and detailed information

The kill Command

The kill command is used to terminate processes in Linux. It sends a signal to a process, instructing it to terminate. By default, the kill command sends the SIGTERM signal, which requests a graceful shutdown of the process.

Usage Example

Kill a process by PID

Forcefully kill a process using SIGKILL

Send a specific signal to a process (e.g., SIGHUP)

Kill multiple processes by their PIDs

Example: Killing a process

The du Command

The du (disk usage) command is used to estimate file and directory space usage in Linux. It displays the amount of disk space used by the specified files and directories.

Usage Example

Display the disk usage of a directory

Display the disk usage in a human-readable format

Display the total disk usage of a directory

Display disk usage for all files and directories in the current directory

Display disk usage for a specified depth of directories

Example: Displaying disk usage

The ifconfig Command

The ifconfig command is used to configure and display network interface parameters for a network connected to the system. It is a powerful tool for network configuration and troubleshooting.

Usage Example

Display all network interfaces

Display a specific network interface

Enable a network interface

Disable a network interface

Assign an IP address to a network interface

Example: Displaying and configuring a network interface

The ping Command

The ping command is used to test the network connectivity between two hosts. It sends ICMP Echo Request packets to the target host and waits for an ICMP Echo Reply.

Usage Example

Ping a host by its hostname

Ping a host by its IP address

Specify the number of ping requests to send

Flood ping (send packets as fast as possible)

Set the time interval between ping requests

Example: Pinging a host with different options

The views and opinions expressed in this blog post are those of the author and do not necessarily reflect the official policy or position of iClasser