Linux Commands Explained The Rightway!

Linux Commands Explained The Rightway!

Β·

8 min read

This blog is about Linux commands I came across recently while learning Linux. I had to search a lot about how they work and their usage, so I wrote this blog to help someone like me. This is not a beginner's starter guide or compilation of basic Linux commands. I'm Ebrahim Afridi a CS grad and this is my first ever blog be kind to me and enjoy : )

List of commands covered in this blog

  1. chmod
  2. grep
  3. find

    chmod

    ch -> change mod -> mode, it changes the mode of access permissions to read, write and execute a file or a directory.

Before discussing a few ways to use chmod let me show you how to check what permissions a file already has to do so just type ls -l <file_name>

ls -l.png

Here the text on the second line -rw-r--r-- shows us the permissions of file.txt.

To understand what now looks like random alphabets and dashes to you, let me explain its structure.

chmod-structure.png

Here the 1. tells us whether it is a file or a directory, if it is a directory at the place of the first dash there will be a d.

If you count there are a total of 10 spaces to be filled, the first one is for the file type, next there are 3 sets of 3 spaces each first set is for the owner's/user's permissions, next for group's permission, and the last for others/public permission.

Permissions are always arranged in the order of rwx i.e. first slot is for reading next for writing and the last one is for executing permissions.

In our case, it is a file so - as dash represents a file then the next three slots are of owner who has read(r) and write(w) permissions, not the permission to execute so at the place of execute a - here dash represents permission not granted, next three slots are for the group here we only have read permission and others also only have read permission.

Few methods to use chmod

  • Alphabetical Notation
  • Octal Notation

Alphabetical Notation πŸ”‘

Terminology 🧩

u -> user/owner

g -> group

o -> other/public

a -> all

+ -> add a permission

- -> remove a permission

= -> use only this permission

Syntax:
chmod [operation] [file_name]

Let us give all permissions to all users πŸ‘‡πŸ»

a=rwx.png

a shortcut to this is using the a tag

a=rwx(2).png

haha, it's fun, right?

Let us give the user permission to read-write-execute and group and others to read-write

case2.png

a shortcut to this is using the go tag go means group and other πŸ‘‡πŸ»

case2go.png

see it is the same result as above, Linux is fun!

Let us give the user permission to read-write-execute, group read-write, and other read-only

case3.png

whenever you get confused take a break come back again and count the slots for a particular user and match it with the permissions arrangement

πŸ’‘Tip: There are 10 slots and the arrangement is like rwx

I won't be telling you how to use + and - operators you can use them when you practice, remember to use + to add permissions and - to remove permissions.

An example of the same is πŸ‘‡πŸ» here I will be removing r permission from the group and adding w to others +-operator.png

Octal Notation πŸ”’

Terminology 🧩

Binary Notation

0 -> No

1 -> Yes

       Octal    Binary        File Mode
         0       000            ---
         1       001            --x
         2       010            -w-
         3       011            -wx
         4       100            r--
         5       101            r-x
         6       110            rw-
         7       111            rwx

πŸ’‘Tip: Remember the user's arrangement user/owner - group - others/public

So if you want to give all users all permissions i.e. rwx for all users, from the above table we can see that 7 is rwx.

Therefore we have to assign 7 to the owner, group, and others i.e. 777

777.png

Let us give the user permission to the read-only, group to read-write and others to execute only

r-rw-x.png

you can always refer to the table for help.

Another approach to look at this is like this

0 -> No Permission 1 -> Execute 2 -> Write 4 -> Read

If you want to give user/owner read-write-execute permissions i.e. you want a 7 so according to the information above you need 1, 2, 4 add them up it gives a 7 now that's how you can also approach this, it's up to you how you prefer it.

grep

grep searches for words in a file, what we call words are patterns to Linux so it basically looks for the patterns we ask it to find, pretty useful right? damn useful!! 🀯

Now let me tell you some useful grep cmds, first let take a file of names names.txt full of different names like this one πŸ‘‡πŸ»

names.png

Syntax:
grep "[pattern]" [file_name]

Let's search for my name in the file

ebu.png

see it returned my name now let's search for my real name ebrahim afridi.png

you can see I searched for Ebrahim that's why only the Ebrahim part is red in color and grep still prints the other part because it is a whole word you can use -w tag to explicitly tell Linux to only print the word you mentioned nothing else example for the same is below

afri.png

nothing is returned because Ebrahim Afri is not a word Ebrahim Afridi is using the
-w tag makes it a must to return the exact same word nothing similar and nothing else.

Play with the -w tag for more understanding of it and the right way to use it.

grep is case sensitive let me explain case sensitivity with an example from our names.txt file there are two EBU's in the file one with a capital (uppercase) E and the other one with a small(lowercase) e while searching for ebu the capital E and small e matters a lot see this πŸ‘‡πŸ»

2ebu.png

to eliminate this case sensitivity to our advantage we can use the i tag like this

-i.png

tada!

To find the words/patterns with their line number in the file use the n tag.

ni.png

How to search a word/pattern recursively i.e. in a folder then in its sub-folders with one cmd

To do so we use the -r tag in my system I have a folder with sub-folders namely cars.txt file.txt names.txt newFile.txt in all these files the word ebu is only on names.txt and newFile.txt let's see how -r works

-r.png

πŸ’‘ Tip: here . means current directory

see the result shows the folders and subfolders where "ebu " is present

find

searches for files in directories and sub-directories, it can be used in a variety of situations combined with other commands.

For example, if you want to search all the files in directories and sub-directories type the following command

find ..png

In my system, I have three directories namely NEW folder newFolder .

I searched for all the files in all three directories, in the result, you can also see files with a dot before them those are the hidden files, yes find shows hidden files too.

Lets search only for the directories

To find any item of a particular type we use the -type tag, and then add a d or a f to search for directories or files here we will use -type d to only search for the directories.

-d.png

you can do the same for files just use the f tag

Let's make our search more advanced what if you are looking for only the .txt extension files well let me guide you on how to do so

.file.png

wait! wait! don't worry I got you to let me break down this command to you 🦸🏻 the first half part find . -type f is the same as before finding in the current directory for only the file type, the other half part is also very easy to understand here and everywhere else the name tag is used to explicitly tell Linux to find that exact named file/dirt and we type that name in the double quotes "<file_name.extension>" but you must be wondering why I used a * well we use a * to tell Linux that all files of any name ending with the .txt extension will be replaced with that *, you can also understand it as a placeholder it holds the <file_name> place for the names of all the files, it works like this for example if you got two files namely ebu.txt and hehe.txt when we use a * tag while searching for .txt files Linux just replaces the * with ebu and then hehe the rest of commands works the way it should. I hope I explained you well.

πŸ’‘Tip you can eliminate case sensitivity using the i tag
Example find . -type f -iname " *.txt "

What if you want to search for a file modified within a time range

you can do so using the -mmin tag

-mmin2.png

here the first command is searching in the current directory for the file type, modified less than ten minutes ago here the -mmin tag searches for a file modified n minutes ago and as we mentioned -10 it will return a file modified -10 minutes ago.

In the second command, we are searching for a file modified less than ten minutes ago but more than two minutes ago you can change the time range according to your use.

You can also check for files modified days ago using -mtime n tag, if you want to search files only to a certain depth i.e. depth of folders inside folders use -maxdepth n, you can even search for files of a particular size using the -size n tag use + and - sign to represent size bigger than or smaller than.

That's all from my side I hope this blog helped you understand these commands better although you can always use man [cmd] to read about it.

This was my first ever blog ignore all the grammatical mistakes :) Thank you for reading, follow me and give a πŸ‘πŸ» to this blog.

Β