Linux Commands with Syntax – Most Commonly Used Part 4
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.
2. A List of Linux Commands with Syntax
Let’s start a list of Linux Commands with Syntax & usage:
$ rm file1
This Linux command will remove the file which u pass as an argument
$ rm -r <dir or file>
This command will remove the nonempty file or directory
$ 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.
$ shred -zuv file1
This command will surely remove your file after overwriting it
$ man ls
Through this command, you can see the usage manual of any command like ls, cat etc.
$ head -2 file2.txt
This Linux command prints first 2 lines of the file, by default it will print 10 lines.
$ head -2 f1.txt f2.txt
This command will display the first two lines of both the files with the file name
$ head *
This command will display the content of all files in the current working directory with the file name
$ 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
$ tail -2 file2.txt
This command displays last 2 lines of the file.
$ history | tail
This Linux command displays the last 10 lines of your bash history
$ grep apple file1.txt
This command returns the lines which have the word apple from the file1.txt
$ grep -n apple file1.txt
This command will display the line number having apple word in file1
$ grep -i apple file1.txt
This command does case insensitive matching (both lower and upper case of apple)
$ grep --color apple file1.txt
This command colours the matching text. Here it will color Apple in File1
$ grep -A1 apple file1.txt
This Linux command returns the file with matching word as well as one more line after it
$ grep -B1 apple file1.txt
This command returns the file with matching word as well as one more line before it
$ grep -C1 apple file1.txt
This command returns matching text with after and before one line as well
$ grep -v apple file1.txt
This command returns the line which doesn’t contain the matching word.
$ grep -R apple directory1
This command searches for the apple word in all the files in the directory
$ history | grep <any command name>
This Linux command searches the history on terminal which have the specified command
$ history | grep cd | head -12
This command searches history of first 12 commands which have cd word match
$ cat file.text _grep -m 2 apple
This command shows only top two lines which have word apple in the specified file
$ egrep "apple|orange" file.txt
This command returns line with apple or orange
$ which <any command or folder>
This command shows the path of the specified command or folder in your computer
This Linux command shows the user name
$ 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
$ 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.