Tech Tips

Top 20 Common Linux Commands Users Should Know

Linux command is one of the features of the Linux OS (operating system). Users can perform all jobs, both simple and complex, by simply running commands. The Linux terminal is used to execute the commands. This terminal is basically a command-line interface to communicate with the system, which is similar to the command prompt in the Windows OS. Commands in Linux are case-sensitive.

Comparing Linux to other OS like Windows and MacOS, the latter offers a more robust command-line interface. Through its terminal, users can perform both basic and complicated tasks. Users can also perform several fundamental operations, including adding, removing, and moving files. Furthermore, users can capably handle complex jobs like networking (establishing SSH connections), security, administrative (installing packages, managing users), and many more.

Linux is an easy terminal to use since it offers a variety of support choices. Press the combination of “CTRL + ALT + T” to launch the Linux terminal, then hit the “ENTER” key to run a command.

Which are the Basic Linux Commands?

For both Linux users and developers, the command line is a vital tool. With so many commands available, it might be difficult to know from where to begin.

Let’s walk through some of the fundamental Linux commands that all users need to know. The ability to navigate directories and manipulate files is a vital tool for any Linux user. These commands are part of the set.

#1. The ‘ls command’

The ‘ls command’

The ls command is used to list the system’s directories and files. The syntax is as follows:

ls [/directory/folder/path]

root@srv:/# ls /directory/folder/path

file.txt

If the path is removed, the contents of the existing working directory will be shown by the ls command. The following options will allow users to change the command:

  • -R – provides a list of every file inside the subdirectories.
  • -a command – displays all the files, even hidden ones.
  • -lh command – all sizes can be converted to understandable formats like TB, GB, and MB.

#2. The ‘pwd command’

The ‘pwd command’

The pwd command prints the path to the system’s existing working directory like /home/directory/path. The command syntax is as follows:

pwd [option]

Users are given two choices. Symbolic links and environment variable content are printed with the -L or –-logical option. The real path of the current directory is printed with -P, often known as -physical.

The ‘pwd command’ opens the existing working directory in the Terminal.

root@srv/ directory/ folder/ path# pwd

/directory/folder/path

#3. The ‘cd command’

The ‘cd command’

The ‘cd command’ navigates through the Linux directories and files. To use this, use sudo rights to execute this syntax:

cd /directory/folder/path

root@srv/4 cd /directory/folder/path

root@srv:/directory/folder/pathiM

The ‘cd command’ shifts to the existing working directory, displayed by the command line.

It needs the directory name or the entire path, on the basis of where users are at that time. For instance, if users are already in it, just delete /username from /username/directory/folder.

If these parameters are removed, users will reach the home folder. The following are shortcuts for navigation:

  • cd ~[username] – moves to other home directory (another user’s).
  • cd .. – moves a directory up.
  • cd- – shifts to the former directory.

#4. The ‘mkdir command’

The ‘mkdir command’

The ‘mkdir command’ creates and sets permissions for one or more folders. Users will have to check if they have permission to create new folders in the parent folder or directory. The fundamental syntax is as below:

mkdir [option] [directory_name]

The path can be used as the command option to create a folder inside of a directory. One way to create a songs directory inside music is execute mkdir music/songs. The following are some common mkdir command options:

-p – makes a directory that lies between two already-existing directories. For instance, to create a new 2024 directory, use mkdir -p Music/2024/Songs.

  • -m – determines the permissions for the folder. For example, use mkdir -m777 directory to create a directory with write, read, and execute rights for every user.
  • -v – is used to print a note for every created directory.
  • The ‘mkdir command ‘ with the -p option confirms the creation of a new directory

root@srv:// mkdir -v now-folder

mkdir: created directory 'new-folder'

#5. The ‘rmdir command’

The ‘rmdir command’

In Linux, users can remove an empty directory with just ‘rmdir command’. Users must be granted with sudo rights in the parent directory. The syntax is as follows:

rmdir [option] directory_name

The command will produce an error if there is a subdirectory in the folder. Use the -p option to remove a non-empty directory forcefully.

#6. The ‘rm command’

The ‘rm command’

The ‘rm command’ permanently removes files from a directory. The standard syntax is as below:

rm [filename1] [filename2] [filename3]

Change the file count in the command as per required. Users must essentially have write rights to the directory in case they run into an issue.

To change the command, include the following parameters:

-i – asks for confirmation before deleting anything.

-f – removes the file without confirmation.

-r option erases directories and files recursively.

Note: Use the ‘rm command’ carefully since the deletion cannot be undone. The –f and -r arguments should not be used since they could erase all of the files. Always include the -i option to prevent inadvertent deletion.

#7. The ‘cp command’

The ‘cp command’

Use the ‘cp command’ to copy directories or files including their contents from one location to another. It has a range of applications, like,

  • Transferring a file from one folder to another within the current directory. Give the target path and file name as follows:

cp filename.txt /home/username/Documents

  • Doubling numerous files to the directory. Just type the file names and the path for the destination:

cp filename1.txt filename2.txt filename3.txt /home/username/Documents

  • Copies the content of a file to another in the same directory. Just type the destination and the source file:

cp filename1.txt filename2.txt

  • Duplicates the entire directory. Pass the destination and source directories after the -R flag:

cp -R /home/username/Documents /home/username/Documents_backup

#8. The ‘mv command’

The ‘mv command’

The mv command transfers or renames folders and files. Type the target directory after the file name to relocate objects:

mv filename.txt /home/username/Documents

Meanwhile, rename the file in Linux with the mv command with the following syntax:

mv old_filename.txt new_filename.txt

#9. The ‘touch command’

The ‘touch command’

The touch command allows users to create an empty file in the specified directory path. The syntax is as follows:

touch [option] /home/directory/path/file.txt

If the path is ignored, the file will be created in the existing folder. Touch is also used in the command line of Linux to create and edit a timestamp.

#10. The ‘file command’

The ‘file command’

Users can check the type of file, be it pictures, text, or binary with the file command. The syntax is as follows:

file filename.txt

To check multiple files at once, just list every file separately or use its path if all are in the same folder. Add the -k and -i options, to see more specific details and the file’s MIME type.

#11. The ‘zip, unzip commands’

The ‘zip, unzip commands’

The zip command compresses data into a ZIP file at the best compression ratio. The syntax is as follows:

zip [options] zipfile file1 file2….

For instance, the command is used to compress the note.txt in archive.zip in the existing working directory:

zip archive.zip note.txt

The compressed file can be extracted with the unzip command. The syntax is as follows:

unzip [option] file_name.zip

#12. The ’tar command’

The ’tar command’

The tar command archives multiple files into TAR files, which are similar to ZIP files but can optionally compress data. The syntax is as follows:

tar [options] [archive_file] [target file or directory]

For example, type the following to generate a fresh newarchive.tar archive in the /home/user/Documents folder:

tar -cvzf newarchive.tar /home/user/Documents

#13. The ‘nano, vi, jed commands’

The ‘nano, vi, jed commands’

Linux lets users edit files with text editors like vi, Nano, or Jed. Although vi and nano are included in most systems, users must manually install Jed. The command syntax for each of these tools is identical:

  • vi filename
  • nano filename
  • jed filename

These editors will generate the target file if it doesn’t already exist. If users wish to edit text files rapidly, its best to use Nano. Use Jed or vi for programming and scripting in the meantime.

#14. The ‘cat command’

The ‘cat command’

Concatenate or cat command is among the widely used Linux commands. It outputs file contents to the standard result after listing and combining them. The syntax is as follows:

cat filename.txt

There are several ways to use this cat command:

  • cat > filen.txt is used to create a fresh new file.
  • cat file1.txt file2.txt > file3.txt is used to merge file1.txt with the file2.txt and stores the result in filename3.txt.
  • cat file.txt is used to display content in the opposite order.

#15. The ‘grep command’

The ‘grep command’

Users can use tar grep or the ‘global regular expression’ command to search a file’s contents for a term. This Linux command can be used to filter big log files by printing all lines that contain the matched strings.

For instance, type the following to show lines having blue in the notepad.txt file:

grep blue notepad.txt

#16. The ‘sed command’

The ‘sed command’

Users can locate, swap out, and remove patterns in the file using the sed tool without making use of the text editor. This is the standard syntax:

sed [option] 'script' input_file

The script comprises searched regular expression pattern, subcommands, and the replacement string. Use the s or d subcommands to change or remove matching patterns.

Finally, just state which file has the pattern that needs to be changed. This is an illustration of a command that changes the color of red in hue.txt & colors.txt to blue:

sed 's/red/blue' colors.txt hue.txt

#17. The ‘head command’

The ‘head command’

In the command-line interface, the head command is used to print the first ten lines of piped data or a text file. The standard syntax is as below:

head [option] [file]

For example, to view the 1st ten lines of note.txt in the existing folder, type this:

head note.txt

The head command receives multiple options, like:

  • -n – is used to change the number of printed lines. For instance, head -n 5 displays the 1st five lines.
  • -c – is used to print the file’s 1st customized sum of bytes.
  • -q – is used to disable headers that specify file name.

The head command having ‘-n 5’ option gives out the 1st five lines of the file in the Terminal

root@srv:/directory/folder/path# head -n 5 filel.txt

This is the first line

This is the second line

This is the third line

This is the fourth line

This is the fifth line

#18. The ‘tail command’

The ‘tail command’

The tail command is used to display the last 10 lines of the file. This is helpful to check errors and new data. The syntax is as below:

tail [option] [file]

For instance, type the following to display the last 10 lines of colors.txt file:

tail -n colors.txt

#19. The ‘awk command’

The ‘awk command’

The awk command searches a file for regular expression patterns that match and manipulates the corresponding data. The syntax is as below:

awk '/regex pattern/{action}' input_file.txt

The examples of possible actions include mathematical procedures, if-else conditional statements, output expressions like print, and delete commands. The $n notation, which denotes a field in the current line, is also present in it.

To add more than one action, list them in their execution order, separate them with semicolons. For example, this command, includes output, conditional, and mathematical statements:

awk -F’:’ ‘{total += $2; students [$1] = $2} END {average = total/length(students); print “Average:”, average; print “Above average:”; for (student in students) if (students [student] > average) print student}’ score.txt

The result will be seen as below:

root@srv :/directory/folder/path awk -F’:’ ‘(total + $2; students[31] – S2) END (average total length (students); print “Average:”, average: print “Above average:”; for (student in students) if (students[student] > average) pri

Int student) score.txt

Average: 80.5

Above average:

Student B

Student C

#20. The ‘sort command’

The ‘sort command’

The lines of the file can be rearranged in a certain order with the sort command. It only prints the outcome as Terminal outputs and doesn’t alter the original file. The syntax is as follows:

sort [option] [file]

By default, this command will arrange the lines alphabetically, starting with A and ending with Z. Use these options to change the sorting:

  • -o – is used to redirect the command results to another file.
  • -r – is used to reverse the order of sorting to descending.
  • -n – is used to sort the file numerically.
  • -k – is used to rearrange the data in a particular field.

The sort command reorganizes the content of the file on the basis of its alphabet

root@srv/directory/folder/path# sort filel.txt

Line A

Line B

Line C

Line D

The Bottom Line

Hope this blog has helped readers to learn most of the Linux commands. The above-mentioned Linux commands let readers perform the basic as well as the administrative tasks effectively and quickly from the Terminal. It is essential to learn basic Linux commands in order to interact with the machine.

Frequently Asked Questions

Q. What is the Total Number of Commands in Linux?

There are basically, thousands of commands in all (and more are being written every day). But, users need not remember any of them, so relax! They can always be found on the internet.

Q. What are the Basic Commands of Linux?

Actually, there are a few fundamental commands that are ideal for someone who is just a beginner with Linux:

  • cat (for printing the file contents)
  • pwd (for printing the working directory)
  • cp (Copies directories and files)
  • rm (Remove directories and files)
  • mv (Renames and moves directories and files)
  • touch (Forms empty files)
  • mkdir (Forms directories)

Q. How to open ‘Terminal’ in Linux?

The terminal can be accessed with Ctrl + Alt + T shortcut or through the Utility menu in a GUI-based Linux system. While logging in to a Linux server, Terminal is automatically launched.

Q. How to see a list all the Linux Commands?

To see a list of all the Linux commands that are available on the system, use help, compgen -c, or man -k. Use –help to view a command’s usage and available options. To view the various choices for the apt command, for instance, enter apt –help.

Q. Why do Users Encounter a ‘Permission Denied’ Error?

If a permission for command is denied, it needs root or superuser privileges to be executed. Add sudo to every command to obtain the permission, or use sudo su to switch to the superuser. Use the chmod command to modify the permissions of any files or folders if the error is associated with them.

Q. What is the meaning of ‘Syntax Command -option Argument’?

It is the standard sequence of a Unix-like operating system like Linux, command-line utility. The program users wish to launch is referred to as a command, and the ‘-option’ option changes how it behaves. The input data that the utility is using in the interim is called an argument.

Q. What if any Command is ‘not found’?

The ‘Not found error’ notice indicates that the program isn’t installed on the Linux system, assuming the command provided is accurate. Install the command with the package manager on the system (apt for Debian, dnf for RHEL) to repair it.

Prashant Pakhare is the visionary behind GeekyBytes, a tech blog where geeks unite to explore and share the latest in technology. With a passion for delivering insightful how-to guides, tips, tricks, and reviews, Prashant aims to serve the best tech recipes found around the internet, making GeekyBytes a go-to resource for tech enthusiasts. You can reach him at Facebook.
Ad

Leave a Reply

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

Scroll to top