DEV Community

loading...
Cover image for Let's Learn more about Linux/Unix Commands

Let's Learn more about Linux/Unix Commands

payalsasmal profile image Payalsasmal ・5 min read

Hi there!, Lets learn few more about Linux/Unix commands.

1.chmod:
chmod stands for change mode. It's used to change access of a file. Linux has total 27 kinds of file permissions. listing few of them.

Please don't apply this command outside of your home directory. If you will change access on System Directory with sudo command, you may have to reinstall your OS again.

Syntax:

chmod [references][operator][modes] file...
Enter fullscreen mode Exit fullscreen mode

we can also say that, this syntax as who(user) as reference,what(what permission) as operator, mode as which(which permission).

[reference]: It represents that whom to give permission.

Reference Class Description
u user File’s owner
g group Users who are Members of the file’s group
o others User who are neither the file’s owner nor members of the file’s group
a all All three of the above, same as ugo

[operator]: It represents what permission we are making. Are we adding or removing or setting new permission?

Operator Description
+ add the permission.
- remove the permission.
= set the permission and wipe out previous permissions.

[modes]: It represents which permission to be granted.

Modes Names Description
r read read permission to a file or a directory
w write write permission to a file or a directory
x execute execute permission to a file

Example:

Alt Text

Please see left sided values in above example ( like drwxrwxr-x or -rw-r--r-- etc), we called these symbolic value. These sections divided into 4 parts- File types, owner, group and other.

Let's make this example simple to see what permission given to which file. I am taking few examples from the above screenshot.

File types owner permission group permission other permission hard link file/directory name
d rwx r-x r-x 2 Desktop
- rw- r-- r-- 1 get-docker.sh
d rwx rwx r-x 2 payal
- r-x r-- r-- 1 test.sh

File type: It has two types
d: for directory and
- : for regular file.

owner permission: we can see different owner permissions.
rwx: For Desktop & payal directories, the directory's owner can create files within it, can list its contents and descend into it.

rw-: For get-docker.sh file, the file owner can read from the file and write to the file but can't execute the file as "-" ( which means no permission) provided in execute position.
r-x: For test.sh file, the file owner can read from the file and execute the file.

group permission: we can see different group permission.
r-x: For Desktop directory, the member of directory's group can read and descend into it.
r--: For get-docker.sh & test.sh files, the members of the file's group can read from the file but can't write and execute as "-" provided for both places.
rwx: For payal directory, the members of the directory's group can read, write and descend into it.

other permission:
r-x: For Desktop and payal directories, the other users can read and descend into it.
r--: For get-docker.sh & test.sh files, other users can only read from the file .

File Permission can also be given through octa values:
Let's take an example from above example for Desktop directory.

 rwx  r-x  r-x
  u    g    o
Enter fullscreen mode Exit fullscreen mode
Permission Binary Octal User Group Other
r 100 4 4 4 4
w 010 2 2 0 0
x 001 1 1 1 1
total number 7 6 6

Now we can change the permission using below ways:
1.

chmod u+rwx g+rx o+rx <file>
Enter fullscreen mode Exit fullscreen mode

2.

chmod 766 <file>
Enter fullscreen mode Exit fullscreen mode

So both of the commands will give same permission to the file.

2. cal:
cal stands for calendar i.e. use to see calendar in terminal.

Syntax:

cal [-jy] [[month] year]
Enter fullscreen mode Exit fullscreen mode

-j: Show julian dates (days one-based, numbered from January 1)
-y: Show calendar from the current year

Example:

Alt Text

Example:

Alt Text

3. grep:
grep stands for Global Regular Expression Print. It is used to search for a particular pattern of a character and print all the contain that pattern.

Syntax:

 grep [OPTIONs]... PATTERN [FILE]..
Enter fullscreen mode Exit fullscreen mode

We can use grep command in so many ways. Given few examples.

grep [options] Pattern [file]: Search the file with Options. There are so many options, I provided few as examples.

  • grep -c "string-to-search" [FILE] : It find the number of lines that matches the pattern.

Example:

Alt Text

  • grep -i "string-to-search" [FILE] : It search string case insensitively.

    Example:
    Alt Text

NOTE:
Do grep --help to know more about Options.

grep "string-to-search" [file] : Search the word in a single file.

Example:

Alt Text

grep "string-to-search" [file1] [file2] [file3] [file4]: Search the word in a multiple files.

Example:

Alt Text

[some-commands] | grep "string-to-search" : Put some command and search.

Example:

Alt Text

cat [file] | grep "string-to-search" : While concatenate the file and search particular.

Example:

Alt Text

4. zip:
zip command used for compressed files with .zip extension. We can zip files and directories in linux/unix. zip files use in Linux for many reasons. Even we can share the zip file with Window users instead of .tar extension as it won't be compatible.

Syntax:

zip [OPTIONS] filename.zip [list of files]
Enter fullscreen mode Exit fullscreen mode
  • zip filename.zip [file1] [file2] : Creating zip with files:

Example:

Alt Text

  • zip filename.zip [list of files] [list of directories] : Creating zip with files and directories.

Example:

Alt Text

  • zip -e filename.zip [list of files] : Creating password protecting zip.

Example:

Alt Text

5. unzip:
unzip command used for unzipping the zip files.

Syntax:

unzip [OPTIONS] [filename.zip]
Enter fullscreen mode Exit fullscreen mode
  • unzip [filename.zip] : Unzipping the above created zip file combined with files and directories.

Example:

Alt Text

  • unzip -P [password] [filename.zip] : Unzipping password protected zip file.

Example:

Alt Text

NOTE: Providing password in command line is not secure and it should be avoided. We can overcome this situation just providing the below command without providing the password. So, if the zip file is encrypted then unzip command will prompt with window to enter the password.

unzip [filename.zip] 
Enter fullscreen mode Exit fullscreen mode

Example:

Alt Text

Discussion (0)

pic
Editor guide