Blog

50+ Essential Linux Commands You Should Know

>

File And System Management Commands

File and system management commands let you, well, manage files and certain parts of your system with ease.

12. pwd – Print Working Directory

If you ever get lost in the Linux filesystem, run the pwd command. It stands for “print working directory” and as the name suggests, it shows your current position in the entire file system. The syntax to run the command is:

pwd

13. ls – List Storage

ls command is used to show the contents of any directory specified, if the user has read permission. By default, it shows the contents of the current directory if no other parameter is specified. It is one of the most useful commands in Linux. The syntax to use the ls command is:

ls

There are many options to use with the command to provide more information about the contents of a directory. The options which you can pair up with the ls command are:

Option Description
-a show all hidden files and directories in the directory
-A show all files, including hidden files, except the top directory
-h displays the file sizes in human-readable format like 1K, 234M, 2G etc.
-S shows the contents in decreasing order of file size
-l show more information like file permissions, modification date, file size, etc about each entry

14. cd – Change Directory

This command is used to switch to a different directory from the current one. The syntax to use the command is:

cd

If the command is used without any arguments, it will redirect you to the home directory, i.e. /home//

There are two types of paths which can be used with this command:

  1. Absolute path: An absolute path of the directory is specified with respect to the root directory or in other words the complete path of the file or directory.
  2. Relative path: In this mode, the path is specified with respect to the current directory. In the relative path, the current directory is specified with a “.” and the parent directory is specified using “..”.

15. mkdir – make a directory

This command creates the directory(ies) if they do not already exist in the specified path. The syntax to create a new directory using mkdir is:

mkdir

You can also use both absolute and relative paths to specify the directory name while creating a new sub-directory inside a different directory.

16. rmdir – Remove Directory

Generally, this command is used to delete empty directories, but can also remove directories with content using some special flags. The syntax to use this command is:

rmdir

Some of the options which the command can take are:

Option Description
--ignore-fail-on-non-empty used to delete non-empty directories
-p, --parents used to delete the directory along with its children specified
-v, --verbose used to get a diagnostic message for every directory

17. rm – Remove

This command is more versatile than the rmdir as it can remove both folders as well as files and also has many options to work with. The syntax to use the rm command is:

rm

Some of the options that the rm command can take are:

Option Description
-f When this flag is used, the command will never prompt the user and it will ignore all the nonexistent files and directories.
-i When this flag is used, the command will prompt the user for each deletion.
-r When this flag is used, the command will remove all the contents of the directory specified.
-d This flag is used to remove empty directories.
-v This flag is used to get an explanation of what is being done currently.
delete all png files using the rm command

18. cp – Copy

This command works the same as the COPY-PASTE in Windows and is used to make copies of files and directories and store them in the specified directory. Here, the syntax to use the command is:

cp

19. mv – Move

The mv command works the same as the CUT – PASTE in Windows and is used to move a file or directory into a different directory. The syntax to use the mv command is:

mv

The links in Linux are similar to shortcuts in windows. To create a link in Linux, use the ln command as per the syntax shown below:

ln -s

Here, the -s flag is used to create a soft link. Soft links can be used to link to anything in a system including directories, file systems, etc. Any changes made to the original file are not reflected in the linked file.

If the -s flag is not used, a hard link will be created which has numerous limitations, such as you cannot link directories, file systems, etc. Any changes made to the hard-linked file will be reflected in the original file also.

21. xdg-open – Opens Files and Directory

This command is used to open any file or directory from the terminal with the default application selected. The syntax to use the command is:

xdg-open

22. tar, zip, unzip, gzip, gunzip – Compress and Decompress Files

The tar command stands for “tape archive” and is used to combine multiple files into a single archive file. It is one of the most important commands in Linux and can be used to both compress and decompress files. So, the archive file thus formed is with the extension .tar. The syntax to use the tar command is:

tar

So, the options to use with the tar command are:

Option Description
-c creates an archive file
-x extracts the archive file
-f creates an archive file with the given file name
-t displays the contents of an archive file
-v displays more information about the current operation being done
-A combines the archive files
-W verify an archive file
-r update an existing tar file
compress all files using tar command

The zip command is used to compress a set of files and directories into a .zip archive file without losing the quality. Here, the syntax to use the zip command is:

zip

command to zip files in Linux

The unzip command is used to extract .zip files. Syntax to use the unzip command is:

unzip

decompress files using unzip comamnd

The gzip command works similarly to the zip command, except it has a higher compression rate and creates smaller archive files. So, the archive files are created with .gz file extension. The syntax to use the gzip command is:

gzip

gzip command

To extract .gz files, use the gunzip command. It works similarly to the unzip command, except it takes more time to extract as it has a higher compression rate. The syntax to use the gunzip the command is:

gunzip

gunzip command

23. mount, unmount – Mounts File Systems

In Linux, everything is treated as a file, even storage devices and file systems also. The mount command is quite useful as it can help in mounting various types of storage devices and file systems with the default mount location as /etc/fstab. The standard form of the mount command is:

mount -t

To remove a file system or a storage device from the system, use the umount command. The syntax to use the umount command:

umount

24. du – Disk Usage

At times, you must have seen the annoying error message for lack of storage but can never find the file which is occupying the most disk space. For such a situation, you should use the du command which can help to monitor the files occupying the most disk space and further reveal some internal problems. The basic syntax to use the du command is:

du

Some of the options you can use with the command are:

Option Description
-a counts the disk usage for all files and directories
-h displays the sizes in human-readable format and not in bytes
-c displays the total disk usage for a particular directory
showing disk usage using du command

25. df – Disk Free

This command works similarly to the du command, except it shows you the complete summary of the total available space in the file system. The syntax to use the df command is:

df

The common options which you can use are:

Option Description
-a used to include all files and directories
-h shows sizes in human-readable format
-l shows information about the local file system only
-t shows the total disk size and availability
available disk space using df comamnd

26. wget

wget command is used to download files from the internet using various protocols like HTTP, HTTPS, FTPS, etc. The basic syntax to use the wget command is:

wget

Some of the essential options to use with the command are:

Options Description
-i Download from multiple URLs stored inside a file
-O Save the downloaded file with a new name
-b Run the downloading task in the background
-P Save the downloaded file to a specific directory
downloading file using wget command

File Modification Commands

This section contains every basic file creation and modification command you need to know to effortlessly create files.

27. cat – Concatenate

The cat command is used to print the entire contents of any given file(s). To view the contents of any number of files, use the following syntax:

cat

cat command output

28. sort – Prints Sorted Output

This command works similarly to the cat command, except it prints the contents of a file in ascending order. The syntax to use the sort command is:

sort

The common options to use with the sort command are:

Option Description
-u removes duplicates from the output
-r sorts the output in descending order
-o writes the sorted output to a file and not to the output screen
sort command output

29. uniq – Prints Unique Lines of a File

This command removes the duplicate lines from the file’s contents or from another command’s output. The syntax to use the uniq command is:

uniq

uniq comamnd output

30. wc – Word Count

The wc command stands for word count. It can count the total number of words, characters, number of lines, etc. The syntax to use the command is:

wc

wc command outpu

31. chmod – Change Mode

This is probably one of the most important Linux commands and is used by every Linux user. The chmod command is used to modify the permissions for a particular file or directory. Syntax to use the command is:

chmod

There are 3 types of permissions you can specify, using the permission set:

  • read: Allows the users to view the contents of a file or directory, abbreviated as ‘r’.
  • write: Allows the users to make changes to the file or directory abbreviated as ‘w’.
  • execute: Allows the users to execute a file abbreviated as ‘x’.
changing file permissions using chmod command

32. chown – Change Owner

This command works similarly to chmod, except it changes the file owner by modifying the user group. Only the owner and the root user can change the file ownership. The syntax to use this command is:

chown

33. diff, cmp, comm – Difference, Compare, Combined Checking

In Linux, if you ever need to compare two files, there are three commands to use – diff, cmp, and comm. The diff command displays the differences in two files line by line. The syntax to use the diff command is:

diff

showing difference between two files using diff command

The cmp command works similarly to the diff command except it compares the two files byte-wise. If the command finds any disparity between two files, then it returns the first location of the disparity. So, the syntax to use the cmp command is:

cmp

comparing two files bytewise using the cmp command

The comm command combines the output of both the diff and the cmp commands. It shows both line-by-line comparison as well as the byte-wise comparison. The syntax to use the command is:

comm

combing the diff and the cmp command as comm command

34. head, tail – Print the Beginning or the End of the File

Suppose you have a very large file like an access log file and want to see the first entries or the latest entries in it. You can always use the cat command to print out the file contents but it can become very cumbersome to scroll through thousands of lines.

In such a situation, you can use either the head command or the tail command. The head command will print the first 10 lines of the file by default, and the tail command will print the last 10 lines of the file, making it easier to see the desired lines. Syntax to use the head command is:

head

The syntax to use the tail command is:

tail

35. less

As we said above, viewing the contents of a large file like a log file via cat command is not the best way as it may take a pretty long time to print the contents. Here, less command will be your saviour as it will show the file contents without cluttering the terminal screen and it also works much faster than the cat command or any other text editors. It also has syntax highlighting and scrolling using the arrow keys or the page up/down keys. The syntax to use the less command is:

less

To exit out of the less view, press the “q” key.

36. touch – Create a New File

This command is useful if you want to create multiple empty files quickly. You can also use the touch command to change the access time of a file in the logs. The syntax to use the touch command is:

touch

37. nano, vi – Terminal-Based Text Editor

These text editors have a variety of use cases like creating new files, searching for content inside files, viewing file content, etc. apart from editing files. The nano command-line text editor is the easiest text editor for any beginner to use. When you open any file in nano, you can straight away start making changes. All other shortcuts are mentioned below in the bottom pane. To open any file using the nano text editor, use the following syntax:

nano

If the file does not exist, an empty file will be created, else the existing file will be opened in the editor.

nano text editor

The vi text editor has a relatively steep learning curve. It has several modes which tend to confuse new users. To open a file using the vi text editor, use the following syntax:

vi

This will open the file in default mode. To make any changes, press “i” on the keyboard. This will change to INSERT mode. To save the changes made and exit out of vi, first press ESC on the keyboard and then press “ZZ”.

vi text editor

38. find, locate – Shows Where a File is Located

At times you need to work on a file, but you don’t know where it is located in the entire file system. To search for files in the system, you can either use the find command or the locate command. The find command has more options but takes more time to search for the query. The syntax to use the find command is:

find

Some of the options, which the find command can take:

Option Description
-name Search by exact name or partial name
-type Search by particular file type
-maxdepth Search for the item by limiting the depth of search space
searching for files using find command

The locate command does not have many features but works very fast as it indexes every file and folder inside a database. The syntax to use the locate command is:

locate

searching for files using locate command

39. grep – Search Using Patterns

grep is one of the most important commands for Linux. It stands for “Global Regular Expression Print”. It helps to search for a particular string based on a pattern. The command uses both regular expressions as well as normal strings to search inside files or another command’s output. The syntax to use the grep command:

grep

the most common options to use with the grp command are:

Option Description
-e to use regular expressions as the search pattern.
-f used to specify the file to search for the pattern.
looking for patterns using grep command

Process Management Commands

Process management commands are crucial to ensure you have the important processes running and the less important ones discarded. It’s also useful when you want to manage system resources to allocate memory to a process that’s more important than the other.

40. ps – Shows Running Processes

This command shows the list of running user processes in the system for the current user or for other users. By default, it shows the current running process id, terminal ID, status, running time, and command name. The syntax to use the command is:

ps

Some of the options that this command can take are:

Option Description
-a Shows all running processes.
-x show the running processes except for the current session headers.
-f displays the full format listing for all running processes.
-u displays processes initiated by a particular user.
showing running processes using ps command

41. top, htop – Shows the Top Processes

The top and the htop commands work similarly to the ps command, with the difference being they display more information such as CPU utilization, memory usage, etc., which gets updated in real-time. You can use the top and htop commands to kill a process in Linux. The top command can be viewed as a CLI version of the Windows Task Manager. Syntax to use the command is:

top

showing running processes using top command

The htop is similar to the top command, except it has more features and a user-friendly console but it does not come preinstalled. To install htop on Debian-based systems, use the following command:

sudo apt install htop

To use the htop console, type htop in the terminal and press enter on the keyboard. To exit out of either top or htop console, press q on the keyboard.

showing running processes using htop command

42. kill, killall – Kill Processes

There may be times when some program is not working properly or freezing the system by consuming a ton of system resources. In such a situation, the best solution is to terminate the process. To kill a process, use the kill or the killall commands. Both commands work the same. However, there are certain differences. kill takes the process id of the process you want to terminate, which you can fetch either from ps or top command, Whereas, the killall command outputs all the processes running with the name given as input. The syntax to use these commands is:

sudo kill

sudo killall

43. History – history of commands executed

If you have executed a really big combination of commands and want to execute it again but can’t remember what you used, there’s a command for it. To see a list of commands you have already executed, use the history command. The history command stores generally a list of 500 commands. The syntax to use the command is:

history

To execute a command from the history list, use the syntax:

!

44. jobs, fg, bg

In Linux, processes that are currently managed by the shell and have not yet finished execution are known as “jobs” and are assigned a unique sequence number to identify them. If you use the ampersand symbol “&” along with any command, the job will run in the background and will not occupy the terminal window. Such a job is known as a “Background Job”.

If you run the command without the ampersand symbol, then the job will execute in the foreground and will occupy the terminal window. Such a job is known as a “Foreground Job”. To see all the running jobs in your system, use the jobs command. This will list all the jobs along with their job IDs. The syntax to use the command is:

jobs

These two parameters are optional to control the output of the command. The options which the command can take are:

Options Descriptions
-l Shows more information about each job listed.
-p Display only the process IDs for the process group leaders of the selected jobs.
showing current jobs using jobs command

To place a job in the background and assign the status of “running” until it finishes execution, use the following syntax:

bg %

To bring back a running background job, use the following syntax:

fg %

45. export – Create Environment Variables

The export command is used to create and view all the environment variables. To create environment variables with the export command, use the following syntax:

export =

Environment variables so created are temporary in nature and get removed as soon as you reboot the system. To permanently create environment variables, add the above command to the .bashrc file using any text editor of your choice.

46. crontab – Automated Tasks

The crontab command is an actual boon for Linux users as it can run any task automatically periodically. Any task which runs at specific time intervals by the crontab command is known as cronjobs. This command can be very useful for taking backups or cleaning the system, etc. To use the crontab command, use the following syntax:

crontab

Some of the options which the crontab command can take are:

Option Description
-l Lists all the cronjobs for the current user
-e edit the crontab to add new tasks or modify the time interval inside the default text editor specified
-r removes the current crontab

When you use the crontab -e command, it opens the default text editor to edit the current crontab. The syntax to schedule a task using the command is:

In the command portion of the above syntax, you can use any command or even run custom scripts to perform a series of tasks.

crontab editor to create cronjobs

47. service – Control Services

A service is a program or an application that runs in the background. To manage such background services in Linux, you need to use the service command, as shown in the syntax:

service

The common options are:

Option Description
start start a service
stop stop a service
status check the status of a service
showing the status of bluetooth service using the service command

Terminal Management Commands

These commands help you manage the inputs and outputs and the history inside the terminal for a cleaner, more personalized experience.

48. clear

This command is often overlooked but a very essential command in Linux. After running many different commands, the terminal screen may feel cluttered. To clear the screen of all the command outputs, use the clear command as shown below:

clear

You can also the shortcut CTRL+L to clear the screen.

49. echo

The echo command is used to print the parameter passed to it. The syntax to use the echo command is:

echo

50. alias, unalias

At times, you may have to use a very big set of commands multiple times. So, to save time from typing the big commands again and again, use the alias command. It is used to replace the big commands with a smaller character set. The syntax to create an alias is:

alias =""

To remove an alias, use the unalias command as shown below:

unalias

51. bc

If you are stuck in a system without a GUI and need to do some simple calculations, you can use the bc command. It will open a terminal-based calculator. To open the bc calculator, use the following syntax:

bc

52. exit

The exit command is used to exit out of the current terminal session. Use the following syntax to use the exit command:

exit

Package Installation Commands

Linux uses package management and each Linux distribution has its own package manager. As a result, Package Installation Commands may differ from distro to distro. Here are all the basic package installation commands you need to know.

53. apt, dnf, yum, pacman, snap, flatpak

These are probably the most essential Linux commands you will ever need. In Linux, you can install almost all packages and software using the distro’s respective package managers. For Debian-based systems, you have apt package manager, for Fedora and RedHat-based systems, you have dnf package manager, and for Arch-based systems, you can use the pacman package manager. Apart from these distro-specific package managers, you can also use snap or flatpak which can work on any Linux distro. To install any packages using the apt package manager, use the syntax:

sudo apt install

For installing packages using the dnf, use the following syntax:

sudo dnf install

To install any package using the pacman package manager, use the given syntax:

sudo pacman -S

To install any package using the snap package manager, use the given syntax:

sudo snap install

For flatpak, use the given syntax:

sudo flatpak install

Network Management Commands

Linux can also handle network management with ease using a variety of commands. It makes the overall process less tedious and a breeze.

54. ip, ifconfig

If you ever need to know what is your IP address, MAC address, etc. you can use either the ip command or the ifconfig command. The ifconfig has more features than ip command but has to be installed separately with the command:

sudo apt install wireless-tools

To see the IP address of your network interface, using the IP command:

ip addr

showing the ip address using ip comand

And the syntax for using the ifconfig command is as follows:

ifconfig

55. ping

Use the ping command to test the network strength, as shown below:

ping

testing network connection using ping command

Bonus Linux Commands

56. Shell Operators

The shell operators are a fantastic way to combine Linux commands and run as a single one. There are a few important ones that you’ll be using such as:

‘&’ operator

The ‘&’ operator allows you to run any command in the background so that you can use the terminal for other tasks. For example, you need to copy a huge file which will take a lot of time to finish, thereby, blocking your terminal for any other use. In such a situation, you can use the regular cp command as shown above but add an ampersand symbol (&) at the end to let the command run in the background.

‘&&’ operator

Even though the ‘&&’ symbol looks similar to the ‘&’ but it works completely differently. The ‘&&’ allows us to run two or more commands at the same time. Syntax to use the ‘&&’ operator is:

command_1 && command_2

Here, we have combined two commands to run in a single line, but the command_2 will get executed once the command_1 finishes executing.

‘>’ operator

The ‘>’ operator works as an output redirector and redirects the output of one command to another command and is known as the “overwrite operator”. This operator will help you to overwrite the contents of a file without even opening a text editor. For example:

echo "Test line" > test.txt

This command will overwrite the contents of the test.txt file with the line “Test line”.

‘>>’ operator

This operator is known as the “append operator” and works similarly to the “overwrite operator,” except this does not overwrite a file with new contents but helps you to add new content at the end of the file. For example:

echo "Hello World" >> test.txt

So, this command will add the output of the first command at the end of the file “test.txt” without overwriting the contents of the file.

We have covered several useful Linux commands which will help you to use the command line easily. If you ever feel stuck or can’t remember the flags or the command syntax, be sure to check the official man pages. Hope this article has helped you to learn some new Linux commands. In case of any problem, do let us know in the comments below.


Source link

Related Articles

Back to top button
close