SSD Grow

Essential Linux Commands | A Beginner’s Guide to Key Functions

Did you know that about 90% of the Internet is powered by Linux servers? If you want to manage or deploy applications on a Linux server, such as a VPS, you’ll need to know some basic Linux commands. However, for Linux beginners, the Linux terminal (also known as the shell) can be intimidating. There are a vast number of Linux terminal commands to learn and comprehend, and we cannot cover all of them today. Nevertheless, let’s explore some of the basic Linux commands you’ll need to get started navigating the shell.
The pwd is short for Present Working Directory. It’s a command-line utility tool that returns the path to the directory you’re in at that moment.

The output contains the full system path of the current working directory. By default, pwd ignores the symbolic links but with a proper option, you can look at the full physical path of the current working directory.

$ cd /home/dd/Pictures
$ pwd
/home/dd/Pictures

Use the P switch to find the full physical path if you have traversed inside a directory which is symbolically linked.

$ pwd -P
/home/dd/Pictures/test_dir

CD command

The cd command stands for “change directory,” and it allows you to navigate from one directory to another.

To navigate to a particular folder with cd command, pass the folder path as the parameter, like so

$ cd /home/dd/Documents
$ pwd
/home/dd/Documents

With no options, the cd command changes the working directory to the user’s home directory.

$ cd
$ pwd
/home/dd

Another way of doing the same i.e to navigate to the home directory quickly is to use the ~ switch.

$ cd ~
$ pwd
/home/dd

You may want to navigate to the previous working directory without typing the entire folder path again. cd – does exactly that.

$ cd /home/dd/Documents
$ pwd
/home/dd/Documents
$ cd -
$ pwd
/home/dd

MV Command

The mv command is a utility command that moves files and folders from one location to another. The mv command can move a single file, multiple files, and directories.

To move a single file using mv, pass the name of the file that needs to be moved as a first parameter and the new file name as a second parameter. In this case, mv commands rename the filename.

$ mv a.txt b.txt
// renames the file a.txt to b.txt
$ mv some_directory new_directory
// renames the folder some_directory to new_directory

To move a group of files to a folder, pass the name of the files followed by the destination folder name with cd command.

$ mv a.txt b.txt c.txt some_directory
OR
$ mv *.txt some_directory

By default the mv command overwrites the destination file. To prompt before overwriting the destination file, use the -i option.

$ mv -i a.txt b.txt
mv: overwrite 'b.txt'?

RM Command

The rm command is short for “remove.” It’s used to delete files and directories.

Be cautious when you use the rm command because once a file or directory is deleted, you cannot recover it later.

To delete a single file, just pass the name of the file along with the rm command.

$ rm file.txt

It is also possible to delete multiple files at one go.

$ rm file1.txt file2.txt image.png

To delete a directory, use the -r switch, which means to delete all files and folders recursively.

$ rm -r some_directory

To perform deletion safely and interactively, use the -i switch, which prompts before each delete action is performed.

$ rm -i file.txt

rm: remove regular file ‘file.txt’? y

MKDIR command

mkdir command is “make a directory.” To create a directory, pass the name of the directory along with mkdir command.

$ mkdir test_directory

Sometimes, you need to create a nested directory structure. Rather than creating directories one by one, use the -p option to create an entire directory structure.

$ mkdir -p dir1/dir2/dir3
$ tree dir1
dir1
└── dir2
└── dir3

If you want mkdir to give details of what operation it is performing in the process of creating directories, use the -v switch.

$ mkdir -v -p dir_1/dir_2/dir_3
mkdir: created directory 'dir_1'
mkdir: created directory 'dir_1/dir_2'
mkdir: created directory 'dir_1/dir_2/dir_3'

LS Command

ls is the list command in Linux, and it shows full list of files or contents of a directory. Just type ls and press the Enter key. The entire contents of the directory will be shown.

$ ls

Use the -l switch to show the list of files of the current directory in a long list format.

$ ls -l

In Linux, hidden files start with a . (dot) symbol and are invisible to the regular directory. The -a switch will list the entire contents of the current directory including the hidden files.

$ ls -la

Sometimes you may want to get the details of a directory rather than its content. To get the details of a directory, use -d option. For example, if you use ls -l /home, it will display all the files under /home directory. But if you want to display the information about the /home directory then use -ld option as shown below.

$ ls -ld /etc
drwxr-xr-x 162 root root 12288 Jun 18 09:42 /etc

TOUCH Command

Touch is a Linux command that’s used to quickly create a text file.

To simply create a blank file with the touch command, use the following syntax.

$$ touch a.txt
// Creates a file by the name a.txt
$ touch a.txt b.txt c.txt
// Creates multiple files
$ touch {A..Z}.txt
// Creates files with names from A.txt to Z.txt

You can also use touch to modify the access and modification timestamps of files.

If you want to update the access time of an existing file to the current time without creating it, use the -c switch. If the file exists, touch will update the access time, otherwise it will do nothing.

$ touch -c a.txt

To change the access time of a file use the -a switch with touch command.

$ touch -a a.txt

To change the modification time of a file use the -m switch with touch command.

$ touch -m a.txt

To change both the access and modification times use the -a and -m switches together.

$ touch -am a.txt

To change the access and modification time to a specific datetime, use the -t switch and specify the datetime in the format [[CC]YY]MMDDhhmm[.ss].

$ touch -c -t 1806181015 a.txt

Once you have updated the access or modification time using touch command, verify the access/modification time with stat command.

$ stat touch.txt
File: 'touch.txt'
Size: 1838 Blocks: 24 IO Block: 4096 regular file
Device: 2fh/47d Inode: 4471138 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/dwijadas) Gid: ( 1000/dwijadas)
Access: 2018-06-19 12:38:26.274344027 +0530
Modify: 2018-06-19 12:38:21.120301504 +0530
Change: 2018-06-19 12:38:21.152289411 +0530
Birth: -

LESS Command

The less command in the Linux terminal is used to view files. It’s similar to the more command, but allows navigation in both forward and backward direction. less does not need to read the entire input file before processing, so with large input file it starts faster than other text editors like vi.

To view a file using less, just pass the filename along with the less command.

$ less a.txt

You can now navigate to the file using the navigation keys. A few of them are given below:

  • [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigate the file.
  • [Space bar]: Next page.
  • b: Previous page.
  • ng: Jump to line number n. The default is the start of the file.
  • nG: Jump to line number n. The default is the end of the file.
  • /pattern: Search for pattern. Regular expressions can be used.
  • G: go to the end of the file
  • g: go to the start of file
  • q or ZZ: exit the less pager
  • 10j: Jump 10 lines forward.
  • 10k: Jump 10 lines backwards.
  • Ctrl+G: show the current file name along with line, byte and percentage statistics.

LSB_RELEASE Command

You might be interested to know which Linux distribution or the OS version number you are using. One of the options to find this information is to use the lsb_release command. The lsb_release command displays LSB (Linux Standard Base) information about your specific Linux distribution.

To get the LSB distribution information, use the following command.

$ lsb_release

To display the single line text distribution, use -d switch.

$ lsb_release -d

To display the release number of the distribution, use -r switch.

$ lsb_release -r

To display the codename according to the distribution release, use -c switch.

$ lsb_release -c

The lsb_release command with -a option displays all the information for the Linux OS you’re using:

$ lsb_release -a

UNAME Command

The uname command is used to display the software- and hardware-related information such as the kernel release or version, processor type, hostname, etc., in your Linux system, and is built-in with the shell.

To display all the information of a system, use -a switch with uname command.

$ uname -a

The output of the above command will display following information.

  • Kernel name
  • Hostname
  • Kernel release
  • Kernel version
  • Machine hardware name
  • Processor type
  • Hardware platform
  • Operating system

Rather than displaying all the information, it is also possible to get information about your point of interest with the following switch along with uname command.

SwitchSwitch description
-sDisplay the kernel name
-nDisplay the hostname
-rDisplay the kernel release
-vDisplay the kernel version
-mDisplay the machine hardware name
-pDisplay the processor type or ‘unknown’
-iDisplay the hardware platform or ‘unknown’
-oDisplay the name of operating system

HISTORY Command

History is a very useful command that displays all the commands that have been used recently. In its simplest form, just run the history command with no optiosn and it will print out the bash history of the current user in the terminal.

$ history
1 cp a.txt b.txt
2 ssh [email protected]
3 ping 123.456.78.9
4 ssh [email protected]
5 rm /home/dd/Documents/a.txt
...
...

The output of the history command displays line numbers. It is also possible to repeat a command by specifying its line number.

$ !3

For a better view of the output of the history command, filter it with less command to view the output one page at a time.

$ history | less

Alternatively, if you want to view just the last 10 commands, filter the output of history with the tail command:

$ history | tail

To view the last 30 commands you have entered, pass the number i.e 30 as a parameter of history command.

$ history 30

The history of commands is stored in the file ~/.bash_history by default. If you run cat ~/.bash_history, it will display all the commands that you have entered but will not include the line numbers or formatting.

PS Command

The process status command (ps) displays information about active processes in your system. It is generally used to find process identifier number and supports searching of processes by user, group, process id or executable name.

In the simplest form, ps command displays the running processes for a user within the terminal window. To invoke it, just type ps in the terminal.

$ ps
PID TTY TIME CMD
14591 pts/1 00:00:00 bash
14891 pts/1 00:00:00 ps

The output of the ps command will show the rows of data containing the following information.

  • PID
  • TTY
  • Time
  • Command

To view all the running processes in your system, use either of the following commands.

$ ps -A
OR
$ ps -e

To view more information about processes, pass the -F option with the ps command.

$ ps -e -F

To find daemon processes running in your system, use the -d switch.

$ ps -d

You can also find information about specific process by filtering the output of ps command with grep.

$ ps -d | grep httpd

To view all processes owned by a user, use -u switch by specifying either user ID or user name.

$ ps -u dd

A common and convenient way of using ps to fetch more complete information about the processes running in the system is to use the following command.

$ ps aux

where:
a = Displays processes for all users.
u = Displays user/owner for each process.
x = Displays processes those are not attached to the terminal.

In a nutshell, the aux options allows one to view all the running processes in a system in BSD Unix style. The output of ps aux will contain following fields.

Column name

Description

USER

The owner of the process

PID

Process ID

%CPU

CPU time used in percentage

%MEM

Physical memory used in percentage

VSZ

Virtual memory used in bytes.

RSS

Resident Set Size, the non-swappable physical memory used by the process in KiB

TTY

Terminal from which the process is started.

STAT

Process state

START

Starting time and date of the process

TIME

Total CPU time used of the process

COMMAND

The command with all the arguments which started the process

TOP Command

One of the most widely used commands to monitor processes and system resource usage on Linux is the top command. It is installed by default on every Linux distribution. The processes are displayed in a list with multiple columns containing information like process name, pid, user, CPU usage percentage, memory usage percentage, and more.

To view the running processes, just run the top command without any options like below.

$ top

The output of the above command contains a lot of information about the system. The header areas include uptime, load average, cpu usage, memory usage data.

The process list shows all the processes with various details in separate columns. The following column names are included in the output of top command and are as follows.

Column hame

Description

PID

Process ID

%CPU

CPU usage by the process

%MEM

Memory usage by the process

COMMAND

The command (executable file) of the process

Sort by Memory/CPU/Process ID/Running time

You can now sort the process list by memory usage, CPU usage, process ID, and running time.

  • To sort the process list by memory usage, press the M key.
  • To sort the process list by CPU usage, press the P key.
  • To sort the process list by process id , press the N key.
  • To sort the process list by process id , press the T key.

Reverse the sorting order

By default the processes are displayed in descending order. Press R to reverse the sorting order of the processes based on currently sorted column. By default the sorting is done through %CPU usage.

Change the update delay

The top command updates the information on the screen every 3.0 seconds by default. To change the update delay time, press the d key. top will ask to provide the time interval between each refresh.

Display full command path

By default, the command column does not display the full path of the command. To view the full path of the command, press the c key.

Add/Remove column

By default the top command displays only few columns. If you want to add/remove columns or change the order of columns, then press the f key.

In the following screen, you will find few fields are marked with * and they are displayed in the order in which they appear in this list. Navigate to the list using up/down arrow keys and press the d key to toggle the display of that field. Once done, press the q key to go back to the process list.

There are much more options to manipulate the output of the top command, which are out of scope for this tutorial. For more options on the top command, consult its man pages.

MAN Command

As a Linux beginner, you’ll find yourself forgetting exactly what your commands do from time to time. That’s where the man command can be a lifesaver!

The man command will give you information about Linux commands, including its usage options and the various arguments it accommodate.

 To learn more about a command, just run man followed by the command (in this case, mkdir)

$ man mkdir

Conclusion

The journey through essential Linux commands offers beginners a vital foothold in understanding and utilizing this powerful operating system. Each command represents a gateway to efficient system management, file navigation, and task execution. Embracing these fundamentals equips users with the tools to navigate the Linux landscape with confidence, setting the stage for deeper exploration and mastery of its capabilities. As beginners continue to apply and expand their knowledge of these key functions, they lay the groundwork for a rewarding and productive experience in the world of Linux computing.

Related Articles

This is a staging environment