Linux Essentials: Part-2

Linux Essentials: Part-2

Everything You Need To Know About Linux: Beginner to Advanced.

·

5 min read

Introduction:

Hey folks! Am Vishwa, DevOps and OpenSource Enthusiast. This is the second part of Linux Essentials, in this, you will learn about Linux File Permissions, Use of find, grep, and OS commands. You can find Part-1 on my Blogs Page.

1. File Permissions:

$ls -l -> It lists all the files/folders with additional information like Date, Time, It's permission.

  • As a result, the first thing we've is Permission, like -rwxrwxrwx or drwxrwxrwx, here if 'd' is in the first place means it refers to Directory else it is File(-), r is read permission, w is written permission and x is for execution rights.

  • Permission is split into 3 parts,

    After '-' or 'd', the first three(rwx) is for the user(u), the second refers to group permission(group) and the last three are for others.

-> By using chmod we can change the permissions for files/folders.

-> We can do it in two ways, one is the Symbolic way and another one is the Absolute way.

I. Symbolic mode:

  • This uses symbols like u(user), g(group) and o(others) and permissions are represented by r, w, x and for modifying permissions we use operators like '+', '-' and '='.

  • After creating a file, this command $ls -ltr | grep fileName will display the file permission of that file alone.

Result:

- $chmod u+x fileName, it will add execution rights(x) to user/owner.

Result: After adding we will get, -rwxr--r-- another detail but before adding -rw-r--r--. Here x is added in User(u) part.

Similarly for removing we use '-', for assigning and overriding existing permissions we use '='.

- $chmod g=rwx,o=rwx fileName, it will add rwx rights to both groups and others too.

Result: -rwxrwxrwx

II. Absolute mode:

  • This represents permissions by using 3-digit octal numbers ranging from 0-7 and to modify them we use operators.

  • For read=4, write=2, execute=1.

- $chmod 777 fileName, it will add all three rights(rwx) to all the three parts that is user, group and others.

Result: After adding -rwxrwxrwx, before adding permissions it was -rw-r--r--

Similarly, set read for the user, read and execute for the group and execute for others.

- $chmod 451 fileName.

Result: -r--r-x--x

Another example set rwx to user, r & x to g and r to o.

- $chmod 754 fileName.

Result: -rwxr-xr--

What is the use of the find command?

-> Find command helps us to show the hierarchy of a file or a folder.

Using for various purposes and they are

1. $find . -> Displays all the files and directories in the current directory, similar to the parent directory($find ..).

2. $find dirName -> Displays everything in it.

3. $find . -type d -> Displays only the directory.

4. $find . -type f -> Displays only the files.

5. $find . -type f -name "fileName" -> For particular file, similarly for directory(change 'f' as 'd' and fileName to dirName).

6. $find . -type f name "file*" -> Displays the files that have the name "file".

7. $find . -type f -name "*.txt" -> Displays .txt extension files.

-> We can check with the help of the date/time also.

Time:

8. $find . -type f -mmin -20 -> Displays the files that are modified less than 20mins.

9. $find . -type f -mmin +15 -> Files that are modified before 15mins.

Days:

10. $find . -type f -mtime -10 -> Modified in last 10 days.

11. $find . -size +1k -> Displays more than 1kb sized files/folders, similarly 'g' for Gb, 'm' for Mb.

12. $find . -size -1k -> Less than 1kb sized files/folders.

13. $find . -empty -> Displays empty files/folders in the current directory.

14. $find . -perm 777 -> Displays files/dir that has rwx permissions.

15. $find . -type f -name "*.txt" -exec rm -rf {} -> Will remove all the .txt extension files.

Here, we will get name from the find command then execute(exec) will remove those by adding in a placeholder({}).

What is the use of the grep command?

-> It is used to find a particular content in a file and it is Case sensitive.

$grep "Vishwa" fileName -> Displays the word by highlighting if it is a sentence.

Result: Am Vishwa*.*

1. Not Case sensitive:

$grep -i "vishwa" fileName -> It displays without considering case sensitive.

2. Not Case sensitive and should be a word:

$grep -iw "vishwa" fileName

4. For to know the line number,

$grep -n "DevOps" fileName

5. Combine all the three,

$grep -win "Vishwa" fileName

Another command,

6. $grep -B 3 "Vishwa" fileName -> It displays the previous 3 lines and also the word's line.

7. $grep -win "vishwa" ./*.txt -> To check in the current directory, when we don't know where it is.

8. $grep -rwin "Vishwa" . -> It displays where the word is in the current directory.

9. $grep -wirl "Vishwa" . -> It displays the matches in every file.

10. $grep -wirc "vishwa" . -> Count of the word in the current directory.

11. $history | grep "grep" -> It displays only the history of grep used commands.

12. $grep -P "\w" file.txt -> It displays all the words.

13. $grep -P "\d{3}-\d{3}-\d{4}" fileName -> Displays the phone number available in the file.

What is the use of Alias?

-> By using this command we can set alias name for any command.

Example: ls as 'l', here after setting this both will do the same work.

For setting permanently,

In bash shell, (from root(~) directory)

- $cat .bashrc -> To see the paths.

- $vi .bashrc -> Then type here, like alias l="ls" and save it.

Similarly for other shells like zsh, visit respective file and make changes.

Operating System Command:

1. $uname -> Displays the name of the OS.

2. $uname -o -> Type of the OS.

3. $uname -m -> Architecture of the OS.

4. $uname -r -> Kernel version.

5. $cat /etc/os-release -> Displays all the information about the OS.

That's a wrap folks! Thanks for reading the blog. Hope you find this as an useful one for your learning. Happy learning guys! I'll be covering about User Management, Network commands and Working with operators in the Part-3 of Linux Essentials. Thanks again.

Did you find this article valuable?

Support VISHWA S by becoming a sponsor. Any amount is appreciated!