Most commonly used Linux commands with Syntax – Part 4


1. Objective

This Linux commands tutorial will guide you with some of the top used Linux commands with syntax. Few Linux commands with examples are available for easy learning and to make you ready for Linux programming. It covers commands like rm, shred, man, head, tail, grep, egrep, whoami, chmod, chown.

So let’s start!!!

linux commands with syntax

2. Linux Commands with Syntax

2.1. rm

a. Usage:

$ rm file1

This command will remove the file which u pass as an argument

b. Usage:

$ rm -r <dir or file>

This command will remove the nonempty file or directory

c. Usage:

$ rm -rf dir1

If we want to remove the directory which does not exist, it will give a warning message but with the help of f flag, we can remove the dir1. It will ignore the warning.

2.2. shred

a. Usage:

$ shred -zuv file1

This command will surely remove your file after overwriting it

2.3. man

a. Usage:

$ man ls

Through this command, you can see the usage manual of any command like ls, cat etc.

2.4. head

a. Usage:

$ head -2 file2.txt

This command prints first 2 lines of the file, by default it will print 10 lines.

b. Usage:

$ head -2 f1.txt f2.txt

This command will display the first two lines of both the files with the file name

c. Usage:

$ head *

This command will display the content of all files in the current working directory with the file name

d. Usage:

$ ls | head

This command will list the first 10(by default or we can give value like -2) directory or files of current working directory

2.5. tail

a. Usage:

$ tail -2 file2.txt

This command displays last 2 lines of the file.

b. Usage:

$ history | tail

This command displays the last 10 lines of your bash history

2.6. grep

a. Usage:

$ grep apple file1.txt

This command returns the lines which have the word apple from the file1.txt

b. Usage:

$ grep -n apple file1.txt

This command will display the line number having apple word in file1

c. Usage:

$ grep -i apple file1.txt

This command does case insensitive matching (both lower and upper case of apple)

d. Usage:

$ grep --color apple file1.txt

This command colours the matching text. Here it will color Apple in File1

e. Usage:

$ grep -A1 apple file1.txt

This command returns the file with matching word as well as one more line after it

f. Usage:

$ grep -B1 apple file1.txt

This command returns the file with matching word as well as one more line before it

g. Usage:

$ grep -C1 apple file1.txt

This command returns matching text with after and before one line as well

h. Usage:

$ grep -v apple file1.txt

This command returns the line which doesn’t contain the matching word.

i. Usage:

$ grep -R apple directory1

This command searches for the apple word in all the files in the directory

j. Usage:

$ history | grep <any command name>

This command searches the history on terminal which have the specified command

k. Usage:

$ history | grep cd | head -12

This command searches history of first 12 commands which have cd word match

l. Usage:

$ cat file.text _grep -m 2 apple

This command shows only top two lines which have word apple in the specified file

2.7. egrep

a. Usage:

$ egrep "apple|orange" file.txt

This command returns line with apple or orange

b. Usage:

$ which <any command or folder>

This command shows the path of the specified command or folder in your computer

2.8. whoami

a. Usage:

$ whoami

This command shows the user name

2.9. chmod

a. Usage:

$ chmod entity+permissiontype

This command is used to grant or add permission (read,write,execute) to the entity(user,group,other)

e.g. $ chmod u+r filename // add read permission to user(you)
$ chmod ug+rwx filename // add read write execute permission to user and group
$ chmod go-wx filename // remove write and execute permission from group and other but not to user(you) to access the file
$ chmod a-rwx filename // remove all the permissions from user, group and rest of the world

2.10. chown

a. Usage:

$ sudo chown ec2-user myfile

This command is used to change owner. Supose myfile is owned by root(admin) but root want to change the owner to ec2-user

Learn more commands in Linux commands-Part 3.

Leave a comment

Your email address will not be published. Required fields are marked *