Linux Commands with Syntax – Most Commonly Used Part 4

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Linux Commands with Syntax

Today, we will discuss 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.

Linux Commands with Syntax - Most Commonly Used Part 4

Linux Commands with Syntax – Most Commonly Used Part 4

2. A List of Linux Commands with Syntax

Let’s start a list of Linux Commands with Syntax & usage:

2.1. rm

a. Usage:

$ rm file1

This Linux 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 Linux 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 Linux 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 Linux 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 Linux 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 Linux 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 Linux command is used to change owner. Suppose myfile is owned by root(admin) but root want to change the owner to ec2-user.

So, this was all about Linux Command with Syntax. Hope you like our explanation. Furthermore, if you have a query, feel free to ask in the comment section.

Learn more commands in Linux commands-Part 3.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.