GO BACK TO BLOG'S MAIN PAGE

Linux Commands All Users Should Know 

Posted on

Article illustration

Linux commands are widely known for their impressive power. Understanding terminal commands is essential for maximizing your efficiency with Linux. Despite the presence of a graphical user interface, certain functionalities are more efficient when executed as commands in the terminal.

This guide presents fundamental Linux commands that all users must familiarize themselves with.

Requirements

  • A Linux-based system.
  • Ability to make use of the command line/terminal.

Introduction to Basic Linux Commands

Linux commands can be categorized into four main groups:

  • Shell builtins are commands that are directly built into the shell and have the fastest execution.
  • Shell functions are essentially shell scripts that consist of grouped commands.
  • Custom command shortcuts – aliases.
  • Executable programs are compiled and installed programs or scripts.

Reminder: To check any command type, simply run the command .

Here is a compilation of common Linux commands, along with detailed explanations and practical examples to help you understand their functionality. Open the terminal by pressing CTRL+ALT+T and follow along.

1. The pwd command

The pwd command, a shell builtin, prints the current location. The output displays a complete directory path, commencing from the root directory (/).

Here is the general syntax:

pwd <options> 

To understand the functionality of the command, execute the following code in the terminal:

pwd

The output displays the current location in the /home/ format.

2. The ls command

The ls command displays a list of the files and directories in the current directory. Please execute the following command:

ls

Extra choices offer versatility in the way the output is presented. Common usage involves combining the following options:

Show as a list: 

ls -l

Display the contents as a list, including any hidden files.

ls -la

Display sizes in a format that is easy for humans to understand:

ls -lah

3. Use the cd command

The cd command is a built-in shell command that allows you to change the current working directory.

cd

As an example, to navigate to the Document directory, execute the following command:

cd Documents

The working directory undergoes changes in the terminal interface. Using an alternative interface, utilize the pwd command to verify the present directory.

Simply use the “cd” command without any parameters to effortlessly return to the home directory (~).

4. Use the cat command

When you use the cat command, it shows the contents of a file in the terminal, also known as standard output or stdout. To utilize the command, please specify a file name located in the current directory.

cat <filename> 

Alternatively, you can include the file path and name:

cat <path>/<filename>

The command has additional capabilities:

Display the contents of multiple files:

cat <file 1> <file 2> 

Generate fresh files:

cat ><filename>

Ensure that the file is populated with the necessary information and use the CTRL+D command to exit.

Show line numbers:

cat -n <filename>

5. Use the touch command

The touch command’s main function is to alter an existing file’s timestamp. To utilize the command, execute:

touch <filename> 

If the file does not exist, the command will create an empty file. Thanks to this effect, touch can also be used as a fast method for creating a new file or a batch of files.

6. The cp command

Copying files and directories in Linux is typically done using the cp command (copy). Give the command a try using:

cp <source file> <target file> 

Ensure that the source and target files have distinct names to avoid any conflicts during the copying process within the same directory. Specify the directory where you want to copy the file to.

7. The mv command

Use the mv command (move) to relocate files or directories from one location to another. As an example, you can execute the following command to transfer a file from the current directory to ~/Documents:

mv ~/Documents/

8. mkdir command

Creating a new directory in the specified location is done using the mkdir command (make directory). Here is the command format:

mkdir <directory name>

You can create a directory in the specified location by providing the path. If you want to create multiple directories at once, you can use a space or comma-separated list.

9. rmdir command

To delete an empty directory, you can use the rmdir command. As an illustration:

rmdir <directory name> 

If the directory is not empty, the command will fail.

10. The rm command

The purpose of the rm command is to delete files or directories. To utilize the command for directories that are not empty, simply include the -r tag:

rm -r <file or directory>

Similar to the rmdir command, rm also deletes all the contents from the directory.

Caution: Deleting certain directories in Linux can be risky. It is important to have a clear understanding of the actions you are taking before executing potentially risky commands in the Linux terminal.

11. locate command

The locate command is a straightforward Linux tool for locating a file. The command examines a file database on a system to efficiently conduct the search. Nevertheless, the outcome may be unreliable if the database is not kept up to date.

To apply the command, you will need to install the locate tool and then give the following example a try:

locate <filename> 

The output displays the file’s location path. The matching criteria is ambiguous and returns all files that include the file name.

12. find command

Utilize the find command to conduct a comprehensive search on the system. To search for a file or directory by name, simply add the -name tag:

find -name <file or directory>

The output displays the file’s path and executes a precise match. Explore additional options to further refine your search.

13. grep command

With the grep command, you can easily search through text in a file or standard output. Here is the basic syntax:

grep <search string> <filename> 

The output showcases all matches. Mastering advanced commands involves using grep for multiple strings or crafting grep regex statements.

14. Execute the sudo command

Using the sudo command grants users elevated permissions, allowing them to perform tasks as an administrator or root user. Commands that modify system settings require higher privileges.

To execute commands that require elevated privileges, simply add the prefix “sudo” before the command:

sudo <command>

Exercise caution when using the command to prevent unintended changes from becoming permanent.

15. df command

When using the df command, you can easily check the amount of available disk space on the file system. To understand the functionality of df, execute the following command:

df

The output displays the space utilization of various drives. To make the output more user-friendly, you can include the -h tag. This will display the output in a human-readable format, using kilobytes, megabytes, and gigabytes.

16. du command

Understanding the du (disk usage) command can provide valuable insights into the space occupied by files and directories. Execute the command with no additional specifications:

du

The output provides a comprehensive overview of the storage application of files and directories within the current directory. The size is displayed in blocks, and using the -h tag will convert the measurement into a format that is easier for humans to read.

17. Execute the head command.

Employ the head command to truncate lengthy outputs. This command has the ability to truncate files, as shown in the following example:

head <filename>

On the other hand, you can redirect the output of a command with a lengthy output to another command:

<command> | head

To view the initial ten lines of the du command, execute the following:

du | head

The output only displays the first ten lines, rather than showing the entire content.

18. tail command

The Linux tail command performs the opposite function of head. To display the last ten lines of a file, you can use the command:

tail <filename>

Alternatively, you can redirect the output of a command with a lengthy output to another command.

<command> | tail

For instance, utilize tail to view the final ten lines of the du command:

du | tail

Understanding how to use both the head and tail commands can be quite useful when analyzing Linux log files.

19. The diff command

When comparing two files, the diff command is used to identify and display any differences. To utilize the command, execute:

diff <file 1> <file 2> 

As an example, to compare files test1.txt and test2.txt, you can execute the following command:

diff file1.txt file2.txt

Developers frequently utilize diff to compare different versions of code.

20. tar command

The tar command is a useful tool for archiving, compressing, and extracting files.

The command handles and generates files called tarballs, which are commonly seen during installation procedures. The options offer a variety of functionalities based on the task at hand.

21. chmod command

Make use of the chmod command to modify file and directory permissions. To execute the command, you need to specify the permission code and the file or directory to which the permissions will be applied.

As an illustration:

chmod <permission> <file or directory>

The permission is a numerical code composed of three digits:

  • The first number is the permission of the current user (owner).
  • The group’s permission is represented by the second number.
  • The third number represents the permissions granted to all other users.

As an example, to modify the file permissions for a test.txt file in order to allow read, write, and execute access for anyone, execute the following command:

chmod 777 file.txt

It is generally not recommended to grant read, write, and execute permissions to everyone, as this can pose a security risk. Ensure maximum security on your system by implementing privileged access management.

22. The chown command

With the chown command, you can modify the ownership of a file or directory. To transfer ownership, you can utilize the following command with sudo privileges:

Change the specified file or directory ownership by using the “sudo chown” command and providing the new owner’s name or UID.

As an illustration:

sudo chown bob file.txt

Configuring ownership is a routine task that often arises during installations. Understanding the chown command is crucial for enabling daemons and processes to securely access files during the setup process.

23. Use the ps command.

The ps command provides a comprehensive list of the processes currently running on the system. Every task generates one or more processes that run in the background.

To view the running processes in the terminal session, simply run the ps command without any options.

ps

The output displays the process ID (PID), the terminal type, CPU time usage, and the command that initiated the process.

24. Making use of the top command

The top command, which displays a table of processes, is an expanded version of the ps command. Execute the command without any additional parameters to observe the outcome:

top

The output displays a comprehensive list of currently active processes in real-time. To exit the viewer, use the keyboard shortcut CTRL+C.

25. kill command

Terminate an unresponsive process by using the kill command. Here is the command syntax:

kill <signal option> <process ID> 

There are a total of sixty-four signal numbers, with some being more commonly used than others.

-15 – the process automatically saves all progress before closing.

-9 – results in an immediate halt.

Each program has a unique process ID (PID). Utilize the ps or top command to identify the PID of a process.

26. Execute the ping command

Opt for the ping command to assess internet connectivity. This tool is extremely useful for troubleshooting networking issues. Include an address to evaluate its functionality, such as:

ping google.com

The output displays the website’s response time. To stop the ping, simply press CTRL+C. If there is no response, it indicates a connectivity issue with the host.

27. Using the wget command

The wget command is commonly used to retrieve files from the internet. Here is the syntax you can use to download a file:

wget <URL> 

This command is highly reliable and can seamlessly resume downloads even in challenging and sluggish network conditions.

28. uname command

Utilize the uname command to display system information. Include the -a option to generate a comprehensive overview:

uname -a

The output provides essential details about the system, including the kernel version, operating system, processor type, and more.

29. history command

The terminal session maintains a log of past commands. To access the list, utilize the history command.

history

To limit the number of entries in the list, simply add a number after the command.

30. man command

The man command (manual) is a helpful resource accessible in the terminal. To check the manual reference for any command, simply add “man” as a prefix:

man <command> 

As an example, to access the manual for the man command, execute the following command:

man man

To exit the manual, press the letter q.

31. The echo command

Employ the echo command to display arguments on the terminal. Here is the syntax:

echo <argument>

As an example, to print Hello, world! to the terminal, you can execute the following command:

echo Hello, world!

This command is useful for adding text to files, printing program results, and showing Linux environment variables.

32. Execute the hostname command.

To determine the DNS name of the current machine, you can utilize the hostname command:

hostname

The hostname is displayed in the terminal as an output. Additional functionalities encompass modifying the hostname, accessing and modifying the system’s domain, and verifying the IP address.

33. useradd command

Creating a new user on a Linux system is done using the useradd command. To enable access for adding new users, it is necessary to make changes to system files. Therefore, the sudo command should be added.

The syntax is generally:

sudo useradd <username>

This command is used to create a non-login user. Further configuration is required to fully activate the user account.

Just a heads up: The adduser command is a more user-friendly alternative to useradd. Discover the distinction between useradd and adduser.

34. userdel command

Use the userdel command to remove a user from the system. To enable elevated privileges, you can add “sudo” before the command. For instance:

sudo userdel <username>

No output is displayed in the result.

35. Execute the file command.

With the file command, you can easily obtain details about a file, including its type and contents. To utilize the command, execute:

file <filename> 

The command does not consider the file’s extension. Instead, the file tests the file contents to determine the type.

36. The wc command

With the wc command, you can easily determine a file’s number of lines, words, and bytes. Please enter a filename so that we can count the elements within the file:

wc <filename> 

You can perform advanced counts when combined with other commands like cat, find, and ls.

37. Command to determine current user

To display the currently logged-in user for the shell session, you might use the whoami command.

whoami

The name of the user who is currently logged in will be displayed on the screen. Apply the command in Bash scripts to display the user who is executing a script.

38. Execute the ip command.

The ip command offers a wide range of networking functionalities that can be quite useful. As an illustration, display the private IP address of the machine using the following command:

ip addr

The command provides additional networking capabilities, including IP and routing table management.

39. Familiar with package managers such as apt, yum, RPM, and pacman

Package managers are essential for installing, deleting, and managing software packages on Linux systems. Various Linux distributions utilize different package managers.

A typical installation would resemble the following:

1. Use the APT package manager for Ubuntu:

apt install 

2. To install packages on CentOS and RHEL, you can utilize yum or RPM:

yum install 
rpm -i .rpm

3. To install packages on Arch, utilize pacman:

pacman -S 

40. Use the passwd command

Use the passwd command to change your password from the terminal. Execute without any arguments:

passwd

In addition, the command can assist in generating a login for a newly added user via useradd. Modifying the password of a different account necessitates higher privileges.

41. Execute the mount command

Using the mount command, you can easily attach additional devices to the file system. Here is the syntax for mounting:

mount -t <type> <device> <directory>

Make use of the command to mount ISO files, USB drives, NFS, and other similar devices.

42. Execute the reboot command.

The reboot command allows for an immediate system restart from the terminal. Before proceeding, make sure to save any changes made to all files. Once saved, execute the following command:

reboot

The system restarts immediately.

43. which command

The which command displays the location of an executable program or command. To view the path for a command, execute:

which command

As an illustration:

which cat

The result displays the command’s location. Use which to troubleshoot installed programs that are not functioning properly.

44. nano command

GNU nano is a Linux text editor that is focused on keyboard usage. To create a new file or open an existing one, you can utilize Nano by executing the following command:

nano <filename> 

The editor opens up, giving you the option to add text or code to the file. To save and close, use the keyboard shortcut CTRL+X, followed by Y, and confirm by pressing Enter.

45. vim command

Vim, a Linux text editor, operates within the terminal environment. Here’s an example of utilizing a Vim command to create a new file or access an existing one:

vim <filename> 

Press the I key to enter insert mode and input your desired text. To save changes and exit Vim, you can press the Escape key, type :wq, and then press Enter.

46. What is the “whatis” command?

The whatis command provides a concise explanation of a command’s functionality. Simply add it as a prefix to any command, for example:

whatis cat

The output provides a concise description of the cat command from the documentation.

47. alias command

Use the alias command to display and configure personalized command shortcuts. By default, the alias command displays the current shortcuts.

alias

To create a new alias, follow this format:

alias <name>=<command>

As an example, to set “meow” as the alias for the “cat” command, execute the following:

alias meow=cat

The meow command serves as a convenient alternative to the cat command, allowing users to easily view the contents of files.

48. unalias command

To delete a specified alias, utilize the unalias command. As an illustration, to eliminate the meow alias, execute the following command:

unalias meow

Executing the identical command again will result in an error.

49. clear command

To quickly clear the contents of the terminal, you can use the clear command.

clear

This command resets the visual display and positions the terminal line at the top.

50. exit command

To end the current terminal session, execute the exit command:

exit

If there are no other sessions open, the command will also close the terminal.

In summary

Having examined the examples in this guide, you have gained knowledge about crucial Linux commands. There are numerous commands available to accomplish a wide range of tasks directly from the terminal.

Mastering Linux commands is essential for maximizing efficiency and productivity on Linux-based systems. This guide has provided a comprehensive overview of fundamental Linux commands, categorizing them into four main groups: shell builtins, shell functions, custom command shortcuts (aliases), and executable programs. By understanding and utilizing these commands, users can easily navigate the terminal, manage files and directories, manipulate text, and perform various system tasks. With practice and experience, these commands will become second nature, enabling users to harness the full power of Linux.

HAVE A QUESTION OR WANT TO GET A CUSTOM SOLUTION?

CONTACT SALES

FAQs

Some basic Linux commands you should know include pwd, ls, cd, cat, touch, cp, mv, mkdir, rmdir, rm, locate, find, grep, sudo, df, du, head, tail, diff, tar, chmod, chown, ps, top, kill, ping, wget, uname, history, man, echo, hostname, useradd, userdel, file, wc, whoami, ip, package managers, passwd, mount, reboot, which, nano, vim, whatis, alias, unalias, clear, and exit.

You can navigate the Linux terminal using commands like pwd, ls, and cd. The pwd command displays your current directory, ls lists the contents of the current directory, and cd allows you to change directories.

To create a file, use the touch command. To copy a file, use the cp command. To move a file, use the mv command. To delete a file, use the rm command. Be cautious when using the rm command, as it permanently deletes files.

You can manage permissions and ownership of files in Linux using the chmod and chown commands. The chmod command allows you to change file permissions, while the chown command enables you to change the ownership of a file.

You can search for files using the locate and find commands. To search for text within files, use the grep command. These commands provide various options to refine your search and make it more efficient.

You can monitor system processes using the ps and top commands. The ps command displays a list of currently running processes, while the top command provides a real-time view of system processes and resource usage. Additionally, use the df and du commands to monitor disk space usage.

You can manage users and groups in Linux using commands like useradd, userdel, groupadd, and groupdel. These commands allow you to create, delete, and modify users and groups, as well as manage their permissions and access to resources.

You can edit text files in Linux using terminal-based text editors like nano and vim. These editors provide various features and shortcuts to make editing efficient and customizable to your preferences.

You can manage software packages in Linux using package managers specific to your distribution, such as apt for Ubuntu, yum for CentOS, and pacman for Arch Linux. These package managers allow you to install, update, and remove software packages with ease.

You can get help with Linux commands using the man command, which provides a manual reference for each command. Additionally, you can use the –help option with most commands to display a brief summary of their usage and available options.

Author photo

Stas Sereda

Sereda Stas has an impressive mixture of technical education and practical work experience. In brief, Stas is responsible for the following ensuring robust and trustworthy IT infrastructure security; proactive and persistent network and infrastructure monitoring; preventing any possible security breach; other security-related tasks and issues. His unique set of skills demonstrates an ability to operate at the edge of development, administration, and security simultaneously.

What else to read on the topic