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
r
- readw
- writex
- execute=running a program
Each permission(
rwx
) can be controlled at three levels:u
- user=yourselfg
- group=can be people in the same projecto
- other=everyone on the system
File or Directory permission can be displayed by running
ls -l
command-rwxrwxrwx
Command to change permission
chmod
Hand-On-
Let’s delete the group write permission from jerry file-
Now, delete all the read permission from jerry file-
Next, try to read the file and it will give you the permission denied error-
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-
Number | Permission Type | Symbol |
0 | No Permission | - - - |
1 | Execute | - - x |
2 | Write | - w - |
3 | Execute + Write | - w x |
4 | Read | r - - |
5 | Read + Execute | r - x |
6 | Read + Write | r w - |
7 | Read + Write + Execute | r 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
andchgrp
chown
changes the ownership of a filechgrp
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
andgetfacl
Lists of commands for setting up ACL:
To add permission for user
setfacl -m u:user:rwx /path/to/file
To add permissions for a group
setfacl -m g:group:rw /path/to/file
To allow all files or directories to inherit ACL entries from the directory it is within
setfacl -rm “entry“ /path/to/dir
To remove a specific entry
setfacl -x u:user /path/to/file
(For a specific user)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 permissionSetting
w
permission with ACL does not allow to remove a file
Help Command’s
There are 3 types of help commands
whatis
commandcommand
--help
man
command
Adding Text to Files(Redirects)
3 simple ways to add text to a file
vi
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.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
Standard input (
stdin
) and it has file descriptor number as 0Input is used when feeding file contents to a file.
E.g.
cat < listings
Standard output (
stdout
) and it has file descriptor number as 1By 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
- E.g.-
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
- E.g.-
Standard error (
stderr
) and it has file descriptor number as 2If 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.Let’s see a example of
tee
command -Now, let’s append, to do that we will use
-a
keyword aftertee
-
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 bymore
cmd-
File Maintenance Commands
cp
- Copy filerm
- Remove filemv
- Move or rename the filesmkdir
- To create the directoryrmdir
orrm -r
- To remove the directorychgrp
- Change group ownershipchown
- Change file owner and group
File Display Commands
cat
- Concatenate files and print on the standard output.more
- Display the content of a file in a terminal, just like a pages section. (forward navigation and limited backward navigation.)less
- Display the content of a file in a terminal. (both forward and backward navigation and also has search options.)head
- Output the first part of the file.head -2 <filename>
tail
- Output the last part of the file.tail <filename>
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
