Table of Contents
PWD command
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
$ pwd -P
/home/dd/Pictures/test_dir
CD command
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
$ cd
$ pwd
/home/dd
$ cd ~
$ pwd
/home/dd
$ cd /home/dd/Documents
$ pwd
/home/dd/Documents
$ cd -
$ pwd
/home/dd
MV Command
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
$ mv a.txt b.txt c.txt some_directory
OR
$ mv *.txt some_directory
$ mv -i a.txt b.txt
mv: overwrite 'b.txt'?
RM Command
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
$ rm file1.txt file2.txt image.png
$ rm -r some_directory
$ rm -i file.txt
rm: remove regular file ‘file.txt’? y
MKDIR command
$ mkdir test_directory
$ mkdir -p dir1/dir2/dir3
$ tree dir1
dir1
└── dir2
└── dir3
$ 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
$ ls -l
$ ls -la
$ ls -ld /etc
drwxr-xr-x 162 root root 12288 Jun 18 09:42 /etc
TOUCH Command
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
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
$ touch -a a.txt
$ touch -m a.txt
$ touch -am a.txt
$ touch -c -t 1806181015 a.txt
$ 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
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
To get the LSB distribution information, use the following command.
$ lsb_release
$ lsb_release -d
$ lsb_release -r
$ lsb_release -c
$ lsb_release -a
UNAME Command
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 |
HISTORY Command
$ 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
...
...
$ !3
$ history | less
$ history | tail
$ history 30
PS Command
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
$ ps -e -F
$ ps -d
$ ps -d | grep httpd
$ ps -u dd
$ 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