About 90% of the Internet is powered by Linux servers. And if you want to manage or deploy applications on a Linux server, you’ll need to know some basic Linux commands. For Linux beginners, the Linux terminal (otherwise known as the shell) can be daunting. There’s a veritable ocean of Linux terminal commands to learn and understand. We can’t cover them all today, but let’s take a look at some of the basic Linux commands you’ll need to begin navigating the shell.

1. PWD command

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

2. 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

3. MV Command

The mv command is a utility command that moves files and folders from one location to another. The mvcommand 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 renames 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' ?

4. 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

5. 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'

6. 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 entire contents of 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 /homedirectory. 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

7. TOUCH Command

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

To simply create a blank file with 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 -cswitch. 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 the 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: -

8. 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. 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. Default is the start of the file.
  • nG: Jump to line number n. Default is the end of the file.
  • /pattern: Search for pattern. Regular expressions can be used.
  • G: go to the end of file
  • g: go to the start of file
  • q or ZZ: exit the less pager
  • 10j: Jump 10 lines forward.
  • 10k: Jump 10 lines backward.
  • Ctrl+G: show the current file name along with line, byte and percentage statistics.

9. LSB_RELEASE Command

You might be interested to know which Linux distribution or the OS version number you are using. One of the option 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

10. 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.

Switch Switch description
-s Display the kernel name
-n Display the hostname
-r Display the kernel release
-v Display the kernel version
-m Display the machine hardware name
-p Display the processor type or ‘unknown’
-i Display the hardware platform or ‘unknown’
-o Display the name of operating system

11. 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 history command displays line numbers. It is also possible to repeat a command by specifying its line number.

$ !3

For better view of the output of 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 tailcommand:

$ history | tail

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

$ 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.

12. 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

13. 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 includes uptime, load average, cpu usage, memory usage data.

The process list shows all the processes with various details in a 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.

هل كانت المقالة مفيدة ؟ 224 أعضاء وجدوا هذه المقالة مفيدة (225 التصويتات)