20 Basic Linux Commands With Examples: A Beginner’s Guide

20 Basic Linux Commands With Examples: A Beginner's Guide
Image by Freepik

As a beginner, you must be familiar with basic Linux commands to complete an operation. This is because, while performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly.

Linux comes with such commands which are one to two words. Using these commands, you can perform several operations in no time.

According to a StackOverflow survey, Linux is the most-used operating system by professional developers, with an impressive 55.9% market share. It isn’t just a coincidence. Linux is free and open-source, has better security than its competitors, and boasts a powerful command line that makes developers and power users more effective. You also have access to a powerful package manager and a bunch of development tools.

What are Linux commands?

A Linux command is a program or utility that runs on the command line. A command line is an interface that accepts lines of text and processes them into instructions for your computer.

Any graphical user interface (GUI) is just an abstraction of command-line programs. For example, when you close a window by clicking on the “X,” there’s a command running behind that action.

flag is a way we can pass options to the command you run. Most Linux commands have a help page that we can call with the flag -h. Most of the time, flags are optional.

An argument or parameter is the input we give to a command so it can run properly. In most cases, the argument is a file path, but it can be anything you type in the terminal.

You can invoke flags using hyphens (-) and double hyphens (--), while argument execution depends on the order in which you pass them to the function.

Linux commands are a type of Unix command or shell procedure. They are the basic tools used to interact with Linux on an individual level. Linux operating system is used on servers, desktops, and maybe even your smartphone. It has a lot of command line tools that can be used for virtually everything on the system.

All users should be familiar with most of these commands as they are required for most operating system tasks and computer programming. Linux commands are used to perform a variety of tasks, including displaying information about files and directories.

Top 20 basic Linux commands for beginners

1. Is command

ls is probably the first command every Linux user typed in their terminal. It allows you to list the contents of the directory you want (the current directory by default), including files and other nested directories.

The ls command is commonly used to identify the files and directories in the working directory. This command is one of the many often-used Linux commands that you should know.

This command can be used by itself without any arguments and it will provide us the output with all the details about the files and the directories in the current working directory. There is a lot of flexibility offered by this command in terms of displaying data in the output.

Check the below image for the output.

Ls Command Default

As you can see in the above image, using the command by itself without any arguments will give us an output with all the files and directories in the directory. The command offers a lot of flexibility in terms of displaying the data in the output.

2. pwd command

The pwd command stands for “print working directory,” and it outputs the absolute path of the directory you’re in. For example, if your username is “jimmy” and you’re in your Documents directory, its absolute path would be: /home/jimmy/Documents.

This command is mostly used to print the current working directory on your terminal. It is also one of the most commonly used commands. 

Pwd Default Output

Now, your terminal prompt should usually have the complete directory anyway. But in case it doesn’t, this can be a quick command to see the directory that you’re in. Another application of this command is when creating scripts where this command can allow us to find the directory where the script has been saved.

3. mkdir command

To create folders in the shell, you use the mkdir command. Just specify the new folder’s name, ensure it doesn’t exist, and you’re ready to go.

This mkdir command allows you to create fresh directories in the terminal itself. The default syntax is mkdir <directory name> and the new directory will be created.

root@ubuntu:~# mkdir <folder name>

For example, if you want to create a directory as “BusinessYield” then the basic syntax would be:

mkdir BusinessYield

In case you want to create another directory inside the main directory BusinessYield to store projects, you can use the following command to do so. mkdir BusinessYield/projects

4. cp command

The cp command of Linux is equivalent to copy-paste and cut-paste in Windows. It’s so easy to copy files and folders directly in the Linux terminal that sometimes it can replace conventional file managers.

To use the cp command, just type it along with the source and destination files:

cp file_to_copy.txt new_file.txt

You can also copy entire directories by using the recursive flag:

cp -r dir_to_copy/ new_copy_dir/

Remember that in Linux, folders end with a forward slash (/).

5. cd command

The cd command is highly popular, along with ls. It refers to “change directory” and, as its name suggests, switches you to the directory you’re trying to access.

For instance, if you’re inside your Documents directory and you’re trying to access one of its subfolders called Videos, you can enter it by typing:

cd Videos

You can also supply the absolute path of the folder:

cd /home/BusinessYield/Documents/Videos

There are some tricks to the cd command that can save you a lot of time when playing around with it:

1) Go to the home folder
cd
2) Move a level up
cd ..
3) Return to the previous directory
cd -

The cd command is used to navigate between directories. It requires either the full path or the directory name, depending on your current working directory. If you run this command without any options, it will take you to your home folder. Keep in mind that it can only be executed by users with sudo privileges.

6. mv command

You use the mv command to move (or rename) files and directories through your file system.

To use this command, you’d type its name with the source and destination files:

mv source_file destination_folder/

mv command_list.txt commands/

To utilize absolute paths, you’d use:

mv /home/kinsta/BestMoviesOfAllTime ./

…where ./ is the directory you’re currently in.

You also can use mv to rename files while keeping them in the same directory:

mv old_file.txt new_named_file.txt

7. rm command

You can use the rm command to remove files and directories. Be careful while using it, though, because it’s very difficult (yet not impossible) to recover files deleted this way.

To delete a regular file, you’d type:

rm file_to_copy.txt

If you want to delete an empty directory, you can use the recursive (-r) flag:

rm -r dir_to_remove/

On the other hand, to remove a directory with content inside of it, you need to use the force (-f) and recursive flags:

rm -rf dir_with_content_to_remove/

The rm command is used to delete permanently an empty directory. To perform this command the user running this command must have sudo privileges in the parent directory. 

8. man command

Another basic Linux command is man. It displays the manual page of any other command (as long as it has one).

To see the manual page of the mkdir command, type:

man mkdir

You could even refer to the man manual page:

man man
The manual page of “man.”

9. alias command

The alias command lets you define temporary aliases in your shell session. When creating an alias, you instruct your shell to replace a word with a series of commands.

For example, to set ls to have color without typing the --color flag every time, you would use:

alias ls="ls --color=auto"

As you can see, the alias command takes one key-value pair parameter: alias NAME="VALUE". Note that the value must be inside quotes.

If you want to list all the aliases you have in your shell session, you can run the alias command without argument.

alias
The alias command.

10. unalias command

As the name suggests, the unalias command aims to remove an alias from the already defined aliases. To remove the previous ls alias, you can use:

unalias ls

11. chmode command

The chmod command lets you change a file’s mode (permissions) quickly. It has a lot of options available with it. The basic permissions a file can have are:

  • r (read)
  • w (write)
  • x (execute)

One of the most common use cases for chmod is to make a file executable by the user. To do this, type chmod and the flag +x, followed by the file you want to modify permissions on:

chmod +x script

You use this to make scripts executable, allowing you to run them directly by using the ./ notation.

12. ./ Command

Maybe the ./ notation isn’t a command itself, but it’s worth mentioning in this list. It lets your shell run an executable file with any interpreter installed in your system directly from the terminal. No more double-clicking a file in a graphical file manager!

For instance, with this command, you can run a Python script or a program only available in .run format, like XAMPP. When running an executable, make sure it has executable (x) permissions, which you can modify with the chmod command.

Here’s a simple Python script and how we would run it with the ./ notation:


#! /usr/bin/python3

# filename: script

for i in range(20):

print(f"This is a cool script {i}")

Here’s how we’d convert the script into an executable and run it:

chmod +x script

./script

13. uname command

The uname command is used to check the complete OS information of the system. Check out the command and the output below

Command: 

uname command in linux

Output:

output of uname command in linux

14. locate command

The locate command is generally used to locate the files in the database. Use an asterisk (*) to search for content that contains two or more words. As an example: locate first*file. This command will search the database for the files that contain these two names first and file.

Command: 

locate command in linux

Output:

output of locate command in linux

We first used the rm command to delete the file and then used locate command to find the file in the database which in return has given the output with a -e as the file was removed.

15. exit Command

The exit command does exactly what its name suggests: With it, you can end a shell session and, in most cases, automatically close the terminal you’re using:

exit

16. ln command

The ln command is used to create a shortcut link to another file. This is among the most important Linux commands to know if you want to operate as a Linux administrator.

Command:

Output:

output of ln command in linux

Here we used mkdir to create two directories and then we used ln with an -s to create a soft link in it.

17. cat command

The cat command is the simplest command to use when you want to see the contents of a particular file. The only issue is that it simply unloads the entire file to your terminal. If you want to navigate around a huge file, you can use less command alternatively.

Command:

Output:

output of cat command in linux

18. touch command

The touch command creates an empty file when put in the terminal in this format as touch <file name> 

Command: 

touch command in linux

Output: 

We used the ls command to check the current directories in the terminal and then used the touch command to create an empty file and then again we used ls to find out the created file in the terminal.

19. sudo Command

This command stands for “superuser do,” and it lets you act as a superuser or root user while you’re running a specific command. It’s how Linux protects itself and prevents users from accidentally modifying the machine’s filesystem or installing inappropriate packages.

Sudo is commonly used to install software or to edit files outside the user’s home directory:

sudo apt install gimp

sudo cd /root/ 

It’ll ask you for the administrator’s password before running the command you typed after it.

20. shutdown Command

As you may guess, the shutdown command lets you power off your machine. However, it also can be used to halt and reboot it.

To power off your computer immediately (the default is one minute), type:

shutdown now

You can also schedule to turn off your system in a 24-hour format:

shutdown 20:40

To cancel a previous shutdown call, you can use the -c flag:

shutdown -c

References

0 Shares:
Leave a Reply

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

You May Also Like