Linux Training : Section 4 (Part-1)

Definition: Command Syntax

Commands typically have the syntax:

  • command option(s) argument(s)

Options:

  • Modify the ay that a command works

  • Usually consist of a hyphen or dash followed by a single letter

  • Some commands accept multiple options which can usually be grouped together after a single hyphen

Arguments:

  • Most commands are used together with one or more arguments

  • Some commands assume a default argument if none is supplied

  • Arguments are optional for some commands and required by others

rm -r - to remove the directory

man <cmd> - built-in manual in Linux

Files Permissions

  • UNIX is a multi-user system. Every file and directory in your account can be protected from or made accessible to others users by changing its access permissions. Every users has responsibility for controlling access to their files.

  • Permissions for a file or directory may be restricted to by types

  • There are 3 types of permissions

    1. r - read

    2. w - write

    3. x - execute=running a program

  • Each permission(rwx) can be controlled at three levels:

    1. u - user=yourself

    2. g - group=can be people in the same project

    3. o - other=everyone on the system

  • File or Directory permission can be displayed by running ls -l command

    1. -rwxrwxrwx
  • Command to change permission

    1. chmod

Hand-On-

  1. Let’s delete the group write permission from jerry file-

  2. Now, delete all the read permission from jerry file-

  3. Next, try to read the file and it will give you the permission denied error-

  4. Assign, the permissions-

NOTE: x present in the directory permission shows that we can enter into the directory, if we remove the x from directory then we will get the “Permission Denied“ error.

Permissions using Numeric Mode-

NumberPermission TypeSymbol
0No Permission- - -
1Execute- - x
2Write- w -
3Execute + Write- w x
4Readr - -
5Read + Executer - x
6Read + Writer w -
7Read + Write + Executer w x

example- chmod 764 <filename>

File Ownership

  • There are 2 owners of a file or directory

    • User and Group
  • Command to change file ownership

    • chown and chgrp

      • chown changes the ownership of a file

      • chgrp changes the group ownership of a file

  • Recursive ownership change option (Cascade)

    • -R

NOTE- To change the ownership, ROOT will be the only option

Hands-On: Let’s change the User and Group of a “lisa“ file to root-

Access Control List (ACL)

What is ACL?

ACL provides an additional, more flexible permission mechanism for file systems. It is designed to assist with UNIX file permissions. ACL allows you to give permissions for any user or group to any disc resource.

Use of ACL-

  • Think of a scenario in which a particular user is not a member of a group created by you but still you want to give some read or write access, how can you do it without making user a member of group, here comes in picture Access Control Lists, ACL helps us to do this trick.

  • Basically, ACLs are used to make a flexible permission mechanism in Linux.

  • From Linux man pages, ACLs are used to define more fine-grained discretionary access rights for files and directories.

  • Commands to assign and remove ACL permissions are:

    • setfacl and getfacl

Lists of commands for setting up ACL:

  1. To add permission for user

    setfacl -m u:user:rwx /path/to/file

  2. To add permissions for a group

    setfacl -m g:group:rw /path/to/file

  3. To allow all files or directories to inherit ACL entries from the directory it is within

    setfacl -rm “entry“ /path/to/dir

  4. To remove a specific entry

    setfacl -x u:user /path/to/file (For a specific user)

  5. To remove all entries

    setfacl -b /path/to/file (For all users)

NOTE:

  • As you assign the ACL permission to a file/directory it adds + sign at the end of the permission

  • Setting w permission with ACL does not allow to remove a file

Help Command’s

There are 3 types of help commands

  • whatis command

  • command --help

  • man command

Adding Text to Files(Redirects)

  • 3 simple ways to add text to a file

    1. vi

    2. Redirect command output > or >>

      Using this snap, we can clearly see by putting > or >> before file name, the output is getting printed into the file.

    3. echo > or >>

      using echo cmd, we’ve added the content, then overwritten and then added the content in the same file.

Input and Output Redirects

  • There are 3 redirects in Linux

    1. Standard input (stdin) and it has file descriptor number as 0

      Input is used when feeding file contents to a file.

      E.g. cat < listings

    2. Standard output (stdout) and it has file descriptor number as 1

      • By default when running a command it’s output goes to the terminal

      • The output of a command can be routed to a file using > symbol

        • E.g.- ls -l > jerry pwd > findpath
      • If using the same file or additional output or to append to the same file then use >>

        • E.g.- ls -la >> listings echo “Hello World“ >> findpath
    3. Standard error (stderr) and it has file descriptor number as 2

      • If the command produced any error on the screen then it is considered as stderr-2

        • We can use redirects to route errors from the screen to a file

          E.g. - ls -l /root 2>errorfile

Standard Output to a File (tee)

  • tee“ command is used to store and view (both at the same time) the output of any command.

    1. Let’s see a example of tee command -

    2. Now, let’s append, to do that we will use -a keyword after tee-

Pipes

  • A pipe is used by the shell to connect the output of one command directly to the input of another command.

    • command1 [arguments] | command2 [arguments]
  • Example-

    Let’s say, if we’re using ls -ltr and we want the output as a pages then we will use the pipe cmd followed by more cmd-

File Maintenance Commands

  1. cp - Copy file

  2. rm - Remove file

  3. mv - Move or rename the files

  4. mkdir - To create the directory

  5. rmdir or rm -r - To remove the directory

  6. chgrp - Change group ownership

  7. chown - Change file owner and group

File Display Commands

  1. cat - Concatenate files and print on the standard output.

  2. more - Display the content of a file in a terminal, just like a pages section. (forward navigation and limited backward navigation.)

  3. less - Display the content of a file in a terminal. (both forward and backward navigation and also has search options.)

  4. head - Output the first part of the file. head -2 <filename>

  5. tail - Output the last part of the file. tail <filename>

0
Subscribe to my newsletter

Read articles from Aditya Dev Shrivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Dev Shrivastava
Aditya Dev Shrivastava