Skip to main content

Command Palette

Search for a command to run...

Find your way around the Linux terminal

The Linux terminal can be a daunting place for new users, especially if you are used to using a GUI-based operating system like Windows or macOS.

Updated
8 min read
Find your way around the Linux terminal

What is the Linux command line?

The Linux command line is a text-based interface that allows you to interact with the operating system. It is a lot like the DOS prompt in Windows, but it is much more powerful.

There are several different shells on Linux, these are just a few popular ones:

  • Bourne-again shell (Bash)

  • C shell (csh or tcsh, the enhanced csh)

  • Korn shell (ksh)

  • Z shell (zsh)

On Linux, the most common one is the Bash shell. We will mainly focus on Bash in this series.

To access the Linux command line, open a terminal window. On most Linux distributions, you can do this by pressing Ctrl+Alt+T shortcut.

Once you are in the terminal window, you can start typing commands.

Root and non-root shell

The main difference between root and non-root shells is the level of privileges they have. The root shell has full access to the system, while the non-root shell has limited access.

The easiest way to tell if you are in a root shell or a non-root shell is to look at the prompt. The prompt for a root shell starts with a number sign (#), while the prompt for a non-root shell starts with a dollar sign ($).

On Ubuntu or Debian GNU/Linux, the prompt for a regular user will likely look like this:

nguyenducchinh@VM:~$

If you are logged in as root, your prompt will look like this:

root@VM:~#

You can also use the whoami command to check your current user ID.

nguyenducchinh@VM:~$ whoami
nguyenducchinh

If you are logged in as root, the output of the whoami command will be root.

root@VM:~# whoami
root

It is important to note that the root shell can be dangerous if used incorrectly. For this reason, it is important to only use the root shell when necessary and to take precautions to avoid making mistakes.

Structure of a Linux command

The structure of a Linux command is as follows:

~$ command [options] [arguments]
  • Command: The command is the name of the program or function that you want to run.

  • Options: Options are modifiers that can be used to change the behavior of the command.

  • Arguments: Arguments are the data that the command needs to work.

In the below case, filename is the argument needed to specify which file you will delete.

~$ rm filename

Some commands can accept multiple arguments.

~$ rm filename1 filename2 filename3

Here are some acceptable options you can add for rm:

  • -i prompts system confirmation before deleting a file.

  • -f allows the system to remove without a confirmation.

  • -r deletes files and directories recursively.

Options can be accessed in a short and a long form. For example, -l is identical to --format=long and -a is the same as --all.

Multiple options can be combined as well and for the short form, the letters can usually be typed together. For example, the following commands all do the same:

~$ ls -la 
~$ ls -l -a
~$ ls --format=long --all

Variables

In Linux shell, a variable is a named location in memory that can be used to store data. Variables can be used to store text, numbers, or other types of data.

Local Variables

To declare a local variable you use the following syntax:

~$ variable_name=value

For example, the following command declares a variable called my_variable and assigns it the value 123:

nguyenducchinh@VM:~$ my_variable=123

Once a variable has been declared, you can use it in commands by preceding its name with a dollar sign ($). You can display any variable using the echo command.

nguyenducchinh@VM:~$ echo $my_variable
123

To remove a variable, use the command unset:

clayton@VM:~$ unset my_variable
clayton@VM:~$ echo $my_variable

clayton@VM:~$

The problem is, that when you open another terminal window, this variable is not accessible.

Local variables only work in the instance they were declared. If want to access it across the shell environment, you need to turn it into a global variable.

Global Variables

Turning local to global variables is done by the command export. When it is invoked with the variable name, this variable is added to the shell’s environment:

clayton@VM:~$ greeting=hello
clayton@VM:~$ export greeting

The PATH Variable

The PATH variable is a colon-separated list of directories that tells the shell where to look for executable programs. When you type a command in the shell, the shell searches the directories in the PATH variable for an executable file with the same name.

clayton@VM:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
clayton@VM:~$

Let's say you have an executable file called my_script.sh in your /home directory. By default, the shell will not be able to find this file, because it is not in one of the directories in the PATH variable.

To make the shell find this file, you can add the home directory to the PATH variable. You can do this by editing your shell configuration file, typically ~/.bashrc or ~/.profile. In the file, add the following line:

PATH=$HOME/bin:$PATH

This will tell the shell to look for executable files in the home directory, in addition to the directories that are already in the PATH variable.

If you don't want to edit can also set the PATH variable temporarily by using the export command.

Now, you can run the my_script.sh file by just typing the following command:

clayton@VM:~$ my_script.sh

The shell will find the file in the home directory and execute it.

By setting the PATH variable to include the directories where your executable files are located, you can avoid having to type the full path to the file every time you want to run it.

Most used Linux commands

Here are some of the top Linux commands frequently used by developers and system administrators:

1. ls – List directory contents

  • Displays files and directories in the current directory.
$ ls -l
total 32
drwxr-xr-x 2 user user 4096 Sep 22 10:00 Documents
drwxr-xr-x 3 user user 4096 Sep 21 09:00 Downloads
-rw-r--r-- 1 user user  614 Sep 22 11:00 example.txt

2. cd – Change directory

  • Used to navigate between directories.
$ cd Documents

3. pwd – Print working directory

  • Displays the current directory you are in.
$ pwd
/home/user/Documents

4. cp – Copy files or directories

  • Copies files from one location to another.

  • Example: cp file.txt /path/to/destination

$ cp example.txt backup.txt

5. mv – Move or rename files

  • Moves or renames files or directories.
$ mv example.txt example_old.txt

6. rm – Remove files or directories

  • Deletes files or directories.

  • Example: rm file.txt (use rm -r for directories)

$ rm example_old.txt

7. touch – Create an empty file

  • Creates an empty file or updates the timestamp of an existing file.
$ touch newfile.txt

8. mkdir – Make a directory

  • Creates a new directory.
$ mkdir newfolder

9. rmdir – Remove directory

  • Deletes an empty directory.
$ rmdir newfolder

10. cat – Concatenate and display file content

  • Displays the content of a file.
$ cat example.txt
This is an example file.

11. grep – Search text using patterns

  • Searches for a pattern in files.

  • Example: grep "search_text" file.txt

$ grep "example" example.txt
This is an example file.

12. find – Search for files or directories

  • Searches for files based on name, type, or other attributes.
$ find /home/user -name "*.txt"
/home/user/Documents/example.txt

13. chmod – Change file permissions

  • Modifies the permissions of a file or directory.
$ chmod 755 script.sh

14. chown – Change file owner and group

  • Changes the owner or group of a file.
$ chown user:group file.txt

15. top – Display active processes

  • Shows real-time system processes, CPU, and memory usage.
$ top

(You’ll see an interactive display of processes.)

16. ps – Report process status

  • Displays currently running processes.
$ ps aux
  • Output:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
user      1234  0.1  1.2 123456 12345 ?        Ssl  10:00   0:01 /usr/bin/python3

17. kill – Terminate a process

  • Kills processes by process ID.
$ kill 1234

18. df – Disk space usage

  • Shows available disk space on file systems.
$ df -h
  • Output:
system      Size  Used Avail Use% Mounted on
/dev/sda1       100G   60G   40G  60% /

19. du – Disk usage

  • Shows the size of directories and files.
$ du -sh *
  • Output:
5.0M    Documents
10M     Downloads

20. tar – Archive files

  • Archives and compresses files.
$ tar -czvf archive.tar.gz /path/to/folder

21. wget – Download files from the web

  • Downloads files from a URL.
$ wget https://example.com/file.zip

22. curl – Transfer data from or to a server

  • Used to make network requests and download files.
$ curl https://example.com

23. man – Manual pages for commands

  • Displays the manual for a command.
$ man ls

24. echo – Display a line of text

  • Outputs text or variables.
$ echo "Hello World"
Hello World

25. sudo – Execute commands with superuser privileges

  • Runs commands as the root user or another user.
$ sudo apt update

Conclusion

These are just the essentials of Linux command line that you should know. With these, you will be able to do basic file management, navigate the file system, and run programs.