Tag Archives: Linux shell commands

We have gone through certain Linux Shell Commands in our Part I article. Let’s see a few more basic Linux commands here.

The rm command:

This command is used to remove a file or directory. The -rf option is to remove a file or directory recursively. You must use this command carefully i.e if you run this command in the home directory, or any other important directory, it will delete everything there without confirmation. -f stands for force, it will perform the delete action forcefully. So, please be careful while using this command.

root@monitor:/home# ls
ansible ec2-automate-backup.sh ssbackup.py test
dir1 ssbackup.log ssbackup.py-bak ubuntu
root@monitor:/home# rm -rf test
root@monitor:/home# ls
ansible ec2-automate-backup.sh ssbackup.py ubuntu
dir1 ssbackup.log ssbackup.py-bak
The cp command:

The cp command used to copy a file in the Linux shell. To copy a file/directory recursively use the cp command with the -r flag. For example, we are coping test1.txt to test2.txt

root@monitor:/home# cp test1.txt test2.txt
root@monitor:/home# ls -l
-rw-r--r-- 1 root root 0 Jan 23 16:46 test1.txt
-rw-r--r-- 1 root root 0 Jan 23 16:46 test2.txt

In the below example we are copying test dir to /tmp folder :

root@monitor:/home# cp test /tmp/
The mv command:

The mv command is used to rename or move a file or a directory. In following example we are renaming test1.txt to test.txt.

root@monitor:/home# mv test1.txt test.txt
root@monitor:/home# ls -l
-rw-r--r-- 1 root root 0 Jan 23 16:46 test2.txt
-rw-r--r-- 1 root root 0 Jan 23 16:46 test.txt

In the below example we will move file test.txt to /tmp directory.

root@monitor:/home# mv test.txt /tmp/
root@monitor:/home# cd /tmp/
root@monitor:/tmp# ll | grep test.txt
-rw-r--r-- 1 root root 0 Jan 23 16:46 test.txt
The wc command:

wc is a useful command to count newline, word and bytes of a file

root@monitor:/home# cat test.txt
HI that is a file.
This is the second line.
And we also have a third line.
root@monitor:/home# wc -l test.txt
3 test.txt
root@monitor:/home# wc -w test.txt
17 test.txt

The -l flag is to find the number of lines in a file, -w is to count the number of words in the file.

The echo command:

The echo command echoes a given string to the display.

 $ echo "Hello"
Hello
Redirecting the command output

we can redirect the command output to a file, or as input to another command. | is the most common way to do so. Using this we can count the number of directories in the root (/) directory very easily.

$ ls / bin boot dev etc home lib lib64 lost+found media mnt opt proc root run ˓→sbin srv sys tmp usr var 
$ ls / | wc -w
20
Using > to redirect output to a file!

You can use > to redirect the output of one command to a file, if the file exists this will remove the old content and only keep the input. We can use >> to append to a file, means it will keep all the old content, and it will add the new input to the end of the file.

$ ls / > details.txt 
$ cat details.txt
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var $ ls /usr/ > details.txt $ cat details.txt bin games include lib lib64 libexec local sbin share src tmp
$ ls -l /tmp/ >> details.txt
$ cat details.txt
bin games include lib lib64 libexec local sbin share src tmp
total 776
-rwxrwxr-x. 1 fedora fedora 34 Jun 24 07:56 helol.py
-rw-------. 1 fedora fedora 784756 Jun 23 10:49 tmp3lDEho
The man pages.

The man command shows the system’s manual pages. You can use this command to view the help document (manual page) for any command.

Syntax:

man section command. 

Example : man 7 signal.