Twenty Most Used Linux Commands

Who Created Linux and Its Commands?

Linux is a free and open-source operating system created by Linus Torvalds in 1991. Torvalds, who was a computer science student at the University of Helsinki at the time, wrote the original code for the Linux kernel (the core of the operating system) as a personal project. The kernel was released under the GNU General Public License (GPL), which allows anyone to use, modify, and distribute the software.

Since then, the Linux operating system has evolved and grown through the contributions of a large and diverse community of developers. Many of the commands that are used in the Linux command line were written by these developers, either as part of the core operating system or as part of additional software packages. There are also many third-party utilities and tools that have been developed for use on Linux systems.

Here are the Most Used Linux Commands:

  • ls: Lists the files and directories in the current directory.
  • cd: Changes the current working directory to the specified directory.
  • mkdir: Makes a new directory.
  • rmdir: Removes an empty directory.
  • touch: Creates a new empty file.
  • cp: Copies files and directories.
  • mv: Moves or renames files and directories.
  • rm: Deletes files and directories.
  • man: Shows the manual page for a command.
  • df: Shows information about the available disk space on the system.
  • du: Shows the size of a directory and its subdirectories.
  • free: Shows information about the available and used memory on the system.
  • top: Shows a real-time view of the running processes on the system.
  • ps: Shows information about the running processes on the system.
  • kill: Sends a signal to a process to terminate it.
  • chmod: Changes the permissions of a file or directory.
  • chown: Changes the owner of a file or directory.
  • grep: Searches for a pattern in a file or output.
  • find: Searches for files and directories matching specified criteria.
  • tar: Creates and manipulates tar archive files.

Examples:

ls:

The ls command is used to list the files and directories in the current directory. It has many options that allow you to customize the output and specify which files and directories to include.

Here is the basic syntax of the ls command:

ls [options] [files]

Some common options for the ls command include:

  • -a: Shows hidden files and directories (those that begin with a dot).
  • -l: Shows the files and directories in a long format, which includes additional information such as permissions, owner, group, size, and timestamps.
  • -h: Shows file sizes in “human-readable” format, using units such as K, M, and G for kilobytes, megabytes, and gigabytes, respectively.
  • -R: Recursively lists the files and directories in the specified directory and all of its subdirectories.

Here are some examples of using the ls command:

  • To list the files and directories in the current directory:
ls
  • To list the files and directories in the current directory in long format:
ls -l
  • To list the files and directories in the /etc directory:
ls /etc
  • To list the hidden files and directories in the current directory:
ls -a

cd:

The cd (change directory) command is used to change the current working directory in Linux. It takes the name of the directory you want to switch to as an argument.

Here is the basic syntax of the cd command:

cd directory

Some examples of using the cd command include:

  • To change to the Documents directory in the current directory:
cd Documents
  • To change to the /home directory:
cd /home
  • To change to the parent directory of the current directory:
cd ..
  • To change to the home directory of the current user:
cd ~

mkdir:

The mkdir (make directory) command is used to create a new directory in Linux. It takes the name of the directory you want to create as an argument.

Here is the basic syntax of the mkdir command:

mkdir directory

Some examples of using the mkdir command include:

  • To create a new directory called test in the current directory:
mkdir test
  • To create a new directory called test in the /home directory:
mkdir /home/test
  • To create multiple directories at once:
mkdir dir1 dir2 dir3
  • To create a new directory and set the permissions at the same time:
mkdir -m 755 test

rmdir:

The rmdir (remove directory) command is used to delete an empty directory in Linux. It takes the name of the directory you want to delete as an argument.

Here is the basic syntax of the rmdir command:

rmdir directory

Some examples of using the rmdir command include:

  • To delete the test directory in the current directory:
rmdir test
  • To delete the test directory in the /home directory:
rmdir /home/test

Note that the rmdir command can only delete empty directories. If you want to delete a directory that is not empty, you can use the rm command with the -r option, which stands for “recursive” and allows you to delete directories and their contents. For example:

rm -r test

touch:

The touch command is used to create a new empty file in Linux. It takes the name of the file you want to create as an argument. If the file already exists, the touch command updates the file’s access and modification timestamps to the current time.

Here is the basic syntax of the touch command:

touch file

Some examples of using the touch command include:

  • To create a new empty file called test.txt in the current directory:
touch test.txt
  • To create a new empty file called test.txt in the /home directory:
touch /home/test.txt
  • To create multiple files at once:
touch file1.txt file2.txt file3.txt
  • To update the timestamps of an existing file:
touch existing.txt

cp:

The cp (copy) command is used to copy files and directories in Linux. It takes the source file or directory and the destination as arguments.

Here is the basic syntax of the cp command:

cp source destination

Some examples of using the cp command include:

  • To copy a file called test.txt from the current directory to the /home directory:
cp test.txt /home
  • To copy a directory called test from the current directory to the /home directory:
cp -r test /home

Note that the -r option stands for “recursive” and is necessary to copy directories and their contents.

  • To copy a file and rename it at the same time:
cp test.txt newname.txt
  • To copy a file and preserve its attributes (such as permissions and timestamps):
cp -p test.txt newname.txt

mv:

The mv (move) command is used to move or rename files and directories in Linux. It takes the source file or directory and the destination as arguments.

Here is the basic syntax of the mv command:

mv source destination

Some examples of using the mv command include:

  • To move a file called test.txt from the current directory to the /home directory:
mv test.txt /home
  • To move a directory called test from the current directory to the /home directory:
mv test /home
  • To rename a file:
mv oldname.txt newname.txt
  • To move a file and overwrite an existing file with the same name:
mv -f test.txt /home

The -f option stands for “force” and tells the mv command to overwrite the existing file without prompting for confirmation.

rm:

The rm (remove) command is used to delete files and directories in Linux. It takes the names of the files and directories you want to delete as arguments.

Here is the basic syntax of the rm command:

rm file1 file2 file3

Some examples of using the rm command include:

  • To delete a file called test.txt in the current directory:
rm test.txt
  • To delete multiple files at once:
rm file1.txt file2.txt file3.txt
  • To delete a directory and its contents:
rm -r test

Note that the -r option stands for “recursive” and is necessary to delete directories and their contents.

  • To delete a file and ignore any errors that may occur:
rm -f test.txt

The -f option stands for “force” and tells the rm command to ignore errors and continue deleting the specified files.

man:

The man (manual) command is used to display the manual pages for Linux commands. It takes the name of the command you want to view the manual for as an argument.

Here is the basic syntax of the man command:

man command

Some examples of using the man command include:

  • To view the manual page for the ls command:
man ls
  • To search the manual page for a specific keyword:
man -k keyword

The -k option stands for “keyword” and tells the man command to search the manual pages for the specified keyword.

  • To view a specific section of the manual page:
man section command

For example, to view the “EXAMPLES” section of the manual page for the ls command:

man 5 ls

The number after the man command specifies the section of the manual page to view. The most common sections are:

  1. General commands
  2. System calls
  3. Library functions
  4. Special files (such as device nodes)
  5. File formats and conventions
  6. Games
  7. Miscellanea
  8. System administration commands and daemons

df:

The df (disk filesystem) command is used to show information about the available and used disk space on the system. It displays the sizes and usage of the filesystems on the system, including the mount points and the types of filesystems.

Here is the basic syntax of the df command:

df [options] [filesystem]

Some common options for the df command include:

  • -h: Shows the sizes in “human-readable” format, using units such as K, M, and G for kilobytes, megabytes, and gigabytes, respectively.
  • -T: Shows the types of the filesystems.
  • -i: Shows the number of inodes (i.e., index nodes, which are data structures that store information about files and directories) used and available on the filesystems.

Some examples of using the df command include:

  • To show the sizes and usage of all mounted filesystems:
df
  • To show the sizes and usage of the / filesystem in human-readable format:
df -h /
  • To show the types of all mounted filesystems:
df -T
  • To show the inode usage of the / filesystem:
df -i /

du:

The du (disk usage) command is used to show the sizes of directories and their subdirectories on a Linux system. It displays the sizes of the directories and the amount of disk space used by each directory and its contents.

Here is the basic syntax of the du command:

du [options] [directories]

Some common options for the du command include:

  • -h: Shows the sizes in “human-readable” format, using units such as K, M, and G for kilobytes, megabytes, and gigabytes, respectively.
  • -c: Shows a grand total of the sizes of all the specified directories and their contents.
  • -a: Shows the sizes of all files, including regular files and directories.

Some examples of using the du command include:

  • To show the sizes of the directories in the current directory:
du
  • To show the sizes of the /etc and /var directories:
du /etc /var
  • To show the sizes of the directories in the current directory in human-readable format:
du -h
  • To show the grand total of the sizes of the directories in the current directory:
du -c

free:

The free command is used to show information about the available and used memory on a Linux system. It displays the amount of total, used, and free memory in the system, as well as the amount of shared, buffered, and cached memory.

Here is the basic syntax of the free command:

free [options]

Some common options for the free command include:

  • -h: Shows the sizes in “human-readable” format, using units such as K, M, and G for kilobytes, megabytes, and gigabytes, respectively.
  • -m: Shows the sizes in megabytes.
  • -s: Repeats the display every specified number of seconds.

Some examples of using the free command include:

  • To show the available and used memory in the system:
free
  • To show the available and used memory in the system in human-readable format:
free -h
  • To show the available and used memory in the system in megabytes:
free -m
  • To show the available and used memory in the system every 5 seconds:
free -s 5

top:

The top command is used to display information about the processes running on a Linux system. It shows a real-time view of the processes and their resource usage, including CPU, memory, and I/O.

Here is the basic syntax of the top command:

top

When you run the top command, it will show a list of processes sorted by CPU usage, with the most CPU-intensive processes at the top. You can use the following keys to navigate and interact with the top command:

  • q: Quits the top command.
  • k: Kills a process. You will be prompted to enter the process ID of the process you want to kill.
  • r: Renices a process. You will be prompted to enter the process ID of the process you want to renice, as well as the new nice value (a number between -20 and 19, with lower values indicating higher priority).
  • s: Changes the delay between updates. You will be prompted to enter the new delay in seconds.

Some examples of using the top command include:

  • To view the processes sorted by CPU usage with 3 seconds interval:
top -d 3
  • To view the processes sorted by memory usage:

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

  • To view the processes belonging to a specific user:

Press u while top is running, then enter the username.

ps:

The ps (process status) command is used to show information about the processes running on a Linux system. It displays a snapshot of the current processes, including the process IDs, the command names, and the users that own the processes.

Here is the basic syntax of the ps command:

ps [options]

Some common options for the ps command include:

  • -e: Shows all processes.
  • -f: Shows the full command line for each process.
  • -u: Shows the user and group IDs for each process.
  • -C: Shows only processes with the specified command name.

Some examples of using the ps command include:

  • To show the processes running in the current shell:
ps
  • To show all processes on the system:
ps -e
  • To show the full command line for each process:
ps -f
  • To show the user and group IDs for each process:
ps -u
  • To show only processes with the command name bash:
ps -C bash

kill:

The kill command is used to send a signal to a process in Linux, which can cause the process to terminate or perform other actions depending on the signal. It takes the process ID of the process you want to send the signal to as an argument.

Here is the basic syntax of the kill command:

kill [options] process

Some common options for the kill command include:

  • -s: Specifies the signal to send. The default signal is SIGTERM, which terminates the process.
  • -l: Lists the available signals.
  • -9: Sends the SIGKILL signal, which cannot be ignored by the process and terminates it immediately.

Some examples of using the kill command include:

  • To send the SIGTERM signal to a process with the process ID 123:
kill 123
  • To send the SIGINT signal (which is equivalent to pressing Ctrl+C) to a process with the process ID 123:
kill -s SIGINT 123
  • To send the SIGKILL signal to a process with the process ID 123:
kill -9 123

Note that you need to have the proper permissions to send signals to a process.

chmod:

The chmod (change mode) command is used to change the permissions of files and directories in Linux. It allows you to set the read, write, and execute permissions for the owner, the group, and others (i.e., users who are not the owner or members of the group).

Here is the basic syntax of the chmod command:

chmod mode file

The mode argument specifies the permissions to set, and can be either an octal number or a symbolic representation of the permissions.

In the octal representation, each digit represents the permissions for the owner, the group, and others, respectively. The digits range from 0 to 7, and each digit is the sum of the permissions:

  • 4: read permission
  • 2: write permission
  • 1: execute permission

For example, the octal mode 755 represents read, write, and execute permissions for the owner, and read and execute permissions for the group and others.

In the symbolic representation, you can use the following characters to specify the permissions:

  • u: owner
  • g: group
  • o: others
  • a: all (i.e., owner, group, and others)
  • +: adds a permission
  • -: removes a permission
  • =: sets a permission

For example, the symbolic mode u+x adds execute permission for the owner, and the symbolic mode a-w removes write permission for all.

Some examples of using the chmod:

  • To give read, write, and execute permissions to the owner and read and execute permissions to the group and others for a file called test.txt:
chmod 755 test.txt
  • To give read and write permissions to the owner and read permission to the group and others for a file called test.txt:
chmod 644 test.txt
  • To give read, write, and execute permissions to the owner and no permissions to the group and others for a file called test.txt:
chmod 700 test.txt
  • To add execute permission for the owner and remove write permission for the group and others for a file called test.txt:
chmod u+x,g-w,o-w test.txt

chown:

The chown command in Linux is used to change the ownership of a file or directory. It allows you to specify a new owner user and/or owner group for a specified file or directory.

Here is the basic syntax for using the chown command:

chown [OPTION]... [OWNER][:[GROUP]] FILE...

OWNER and GROUP are the names of the new owner user and group, respectively. FILE is the name of the file or directory whose ownership you want to change.

Here are some examples of how you can use the chown command:

To change the owner of a file to a new user, use the following syntax:

chown new_user file.txt

To change the owner and group of a file to a new user and group, use the following syntax:

chown new_user:new_group file.txt

To recursively change the ownership of all files and directories in a directory, use the -R option:

chown -R new_user:new_group directory

It’s important to note that you need to have appropriate permissions to use the chown command. For example, you need to be the owner of the file or directory, or have superuser privileges (using sudo), in order to change the ownership of a file or directory.

grep:

The grep command in Linux is a utility for searching text. It searches for a specified pattern in a given file, and outputs all lines that contain that pattern.

Here is the basic syntax for using the grep command:

grep [OPTION]... PATTERN [FILE]...

PATTERN is the text pattern that you want to search for. FILE is the name of the file (or files) in which you want to search for the pattern. If you omit the FILE argument, grep will search for the pattern in the standard input.

Here are some examples of how you can use the grep command:

To search for a pattern in a single file, use the following syntax:

grep pattern file.txt

To search for a pattern in multiple files, use the following syntax:

grep pattern file1.txt file2.txt file3.txt

To search for a pattern in all files in a directory (and its subdirectories), use the -R option:

grep -R pattern directory

To search for a pattern and ignore case, use the -i option:

grep -i pattern file.txt

To search for a pattern and only show the file names that contain the pattern, use the -l option:

grep -l pattern file1.txt file2.txt file3.txt

find:

The find command in Linux is a utility for searching for files and directories in a file system. It can search for files based on various criteria such as name, type, size, and permission.

Here is the basic syntax for using the find command:

find [OPTION]... [FILE]... [EXPRESSION]

FILE is the starting point for the search, and EXPRESSION is a set of conditions that the files must meet in order to be returned in the search results. If you omit the FILE argument, find will start the search at the current directory.

Here are some examples of how you can use the find command:

To search for files with a specific name, use the following syntax:

find . -name "filename"

To search for files with a specific file extension, use the following syntax:

find . -name "*.txt"

To search for files with a specific size, use the -size option:

find . -size +100M

To search for files with a specific permission, use the -perm option:

find . -perm 644

To search for files that have been modified within the last 24 hours, use the -mtime option:

find . -mtime -1

To search for directories, use the -type option:

find . -type d

tar:

The tar command in Linux is a utility for creating, managing and extracting tarballs (collections of files bundled together into a single file). It can be used to create backups, transfer files over a network, and more.

Here is the basic syntax for using the tar command:

tar [OPTION]... [FILE]...

FILE is the name of the tarball file that you want to create, extract, or manage.

Here are some examples of how you can use the tar command:

To create a tarball from a list of files and directories, use the -c option:

tar -cvf archive.tar file1.txt file2.txt directory

To extract the contents of a tarball, use the -x option:

tar -xvf archive.tar

To view the contents of a tarball without extracting it, use the -t option:

tar -tf archive.tar

To add a file to an existing tarball, use the -r option:

tar -rvf archive.tar file3.txt

To compress a tarball using gzip, use the -z option:

tar -zcvf archive.tar.gz file1.txt file2.txt directory

To compress a tarball using bzip2, use the -j option:

tar -jcvf archive.tar.bz2 file1.txt file2.txt directory