Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 04

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

DESCRIPTION

ls lists files and directories. If the pathname is a file, ls displays information on the file according to the requested options. If the pathname is a
directory, ls displays information on the files and subdirectories therein. You may obtain information on a directory itself using the -d option.

If you do not specify any options, ls displays only the file name(s). When ls sends output to a pipe or a file, it writes one name per line; when it sends output to the
terminal, it uses the -C (multi-column) format.

Options
-A
lists all entries including those starting with periods (.), but excluding any . or .. entries.

-a
lists all entries including those starting with a period (.).

-b
displays non-printable characters as octal bytes with the form \ooo.

-C
puts output into columns, sorted vertically; this is the default output format to the terminal.

-c
uses the creation time of the file for sorting (-t) or displaying (-l).

-d
does not display the contents of named directories, but show information on the directories
themselves.

-F
puts a / after each directory name, a * after every executable file, a | after every FIFO file, a @ after
every symbolic link and a = after every socket.

Note:
Windows systems do not support FIFO files or sockets (as files) and consider all files that are not
directories or symbolic links to be executable files. This option of the ls utility labels the files and
directories display accordingly.

-f
enables the -a option and disables the -C, -g, -l, -n, -o, -r, -s, and -t options. For each argument
that is a directory, all directory entries are listed in the same order they are retrieved from the system
(POSIX-compliant and UNIX systems only).

-g
displays only the group ID numbers (on 8.1/2012R2/10/2016/2019/11/2022, POSIX-compliant and UNIX
systems).
Types of file Permissions in Chown Command in Linux:
There are three types of file permission in Chown Command in Linux discussed below.
 User: This type of file permission affects the owner of the file.
 Group: This type of file permission affects the group that owns the file. Instead of the group permissions,
the user permissions will apply if the owner user is in this group.
 Other: These types of file permissions affect all other users on the system.
Note: To view the permissions we use:
ls -l

chown command is used to change the file Owner or group. Whenever you want to change ownership, you
can use the chown command.
Syntax of chown Command in Linux
The chown command in Linux has the following syntax:
chown [options] new_owner[:new_group] file(s)

Here’s a breakdown of the components:


 `chown`: The base command.
 `options`: Optional flags that modify the behavior of the
`chown` command.
 `new_owner[:new_group]`: The new owner and optionally the new
group. If `new_group` is omitted, only the owner is changed.
 `file(s)`: The file or files for which ownership is to be changed.
Options available in `chown` command in Linux
1) Using `-c` Option in `chown` to Change File Ownership
The `-c` option in the `chown` command is utilized to report when a file change is made. This option is
beneficial when you want to receive notifications about ownership alterations. The following example
demonstrates its usage:
Example:
chown -c master file1.txt

This command notifies you when the ownership of `file1.txt` is changed, providing valuable feedback for
tracking modifications.
2) `Using `-v` Option in `chown` to Change File Ownership
The `-v` option enhances the verbosity of the `chown` command by showing detailed information for every
processed file. This is particularly useful when you want a comprehensive log of ownership changes. The
following example illustrates its application:
Example:
chown -v master file1.txt

By using this command, you get a verbose output, displaying information about each file processed during the
ownership change.
3) `-f` Option in `chown` to File Ownership in Linux.
The `-f` option in the chown command serves to suppress most error messages and forcefully or silently
change ownership, even when not permitted. This option is handy when you want to override restrictions
without being interrupted by error notifications. Here’s an example:
chown -f master file1.txt

In this case, the command attempts to change ownership, and any error messages are suppressed, allowing
for a more seamless execution.
Examples to Change File Ownership in Linux
1) How to File Ownership in Linux
To Change the owner of a file in Linux, you can use the following basic syntax:
chown owner_name file_name

For example:
chown master file1.txt

In this instance, the command designates the user “master” as the new owner of the file `file1.txt`. This is
particularly useful when transferring ownership of files between users.

2) How to Change the Group of the File in Linux


To change the group ownership of a file, utilize the following syntax:
chown :group1 file1.txt

In this scenario, the group “group1” is assigned as the new group for the file `file1.txt`. This operation is
handy for managing access permissions within specific groups.
3) How to Change Owner and Group of the File in Linux
For simultaneous change in both of the owner and group of a file, we use the following syntax:
chown master:group1 file1.txt

In this use case, the user “master” assumes ownership, and the group “group1” is assigned as the new group
for the file file1.txt. This can be beneficial when restructuring file access hierarchies.
4) How To Change Group Ownership
When the goal is to change only the group ownership of a file, we use this syntax:
chown :group1 file1.txt

This command exclusively alters the group ownership of file1.txt to “group1” from its previous state. It
proves useful in scenarios where group permissions need to be modified independently.

You can see that the group permissions changed to group1 from root, if you use -v option it will report that.
We just need to add a “:” to change group.
5) How to Change Owner as well as Group
Again, taking master as user and group1 as a group in the system
chown master:group1 abhjijit

Here, abhjijit is a file.

6) How to Change Owner from a Particular Ownership Only


To change ownership from a specific user (e.g., “master”) to another (e.g., “root”), where the current owner
must be “master,” use the following syntax:
chown --from=master root abhjijit

This command ensures that ownership is changed from “master” to “root” only when the current owner is
“master.” It adds an additional layer of control to ownership modifications.

7) How to Change Group from a Particular Group


To change the group of a file (e.g., “group1” to “root”), use the following syntax:
chown --from=:group1 root abhjijit

This command specifically changes the group of abhjijit from “group1” to “root.” It is useful when refining
group associations.

Here, the group of abhjijit is changed to root.


8) How to Copy Ownership of One File to Another
To duplicate the ownership of one file (e.g., “abhjijit”) onto another file (e.g., “greek2”), use the following
syntax:
chown --reference=abhjijit abhijit2
This command copies the ownership details from “abhjijit” to “greek2,” ensuring consistency in ownership
between the two files.

9) How to Change Owner of Multiple Files


For simultaneous changes in the owner and group of multiple files, employ the following syntax:
chown master:group abhijit2 abhijit3

In this instance, both “abhijit2” and “abhijit3” will have their owner set to “master” and their group set to
“group.” This is useful for batch ownership modifications, streamlining the process for multiple files at once.

Explain Absolute and Relative Permission Using chmod


Linux permissions are a set of rules that determine the level of access or control on user , file or directory. It helps to
protect the system and its data by ensuring that only authorized users access.

To change file permission in Linux chmod command is used.

chmod command is categorized into three categories:

 user
 group
 other

Command can be specified in either of two ways:


1. relative permission
2. absolute permission
1)relative permission: In relative permission it changes the permission specified in command and leave other
permission unchanged.
syntax: chmod categorization permission filename
example chmod u+r file.txt

Following operation specified that need to performed:

+ assign permission
– remove permission
= assign absolute permission for all
permission specified r, w ,x(read, write, execute)

2)Absolute permission: permission specified by setting all nine permission bits explicitly.in the chmod we used three
octal number.
Example Of Absolute And Relative Permission In
Linux
Here is the practical example of absolute and relative permission that make you fully understand about different
permission in Linux/Unix

1.Assign execute permission to the user but other permission remain unchanged for the file marks.dat

2.Assign execute permission to all the category for the file stud list.txt and mark.dat.

3.Assign read permission to the categories group and other ,revoke execute permission from other for files marks.dat
What is a Hard Link?
The method of direct linking of two files in a same file system is referred to as the hard link. Hard links use
the index node (inode) number for the identification of a file. Since hard links point to inode, they cannot be
implemented on directories.

The "ln" command is executed to create a hard link. This command creates another file on the command link
that is used to refer the original one. Although, both the original file and the hard linked file have the same
index node (inode) number and content. Consequently, both files have the same set of permissions and the
same owner.

If we removed the original file, then it will affect the hard linked file. Thus, the hard linked file will exist even
after the removal of the original file. Hard links consume less memory space and they can be linked only to
its own partition. In the case of hard links, the relative paths are not allowed.

What is a Soft Link?


A file can be accessed through many references. All of this point to a specific file is
known as a soft link. In other words, the link to a file in which the file is pointed by its
file name is known as soft link. Thus, a soft link is an alias or alternate path to the
original file. Soft link does not use index node (inode) number. Soft links are also
referred to as a symbolic link, because they contain the name of the target file of the
link.

While accessing the file through the soft link, it redirects the link to the target file
through the path provided in the subject of the soft link. The major advantage of the
soft link is that the deletion and creation of the soft links do not influence the original
file. The soft links can have an absolute or relative path as their part.

Difference between Hard Link and Soft Link


The important differences between Hard Link and Soft Link are highlighted in the following table −

S. Hard Link Soft Link


No.
1. A file that has a hard link can A file can be accessed through
be accessed using multiple many references. All of this
names. These names are point to a specific file known
known as hard links. as a soft link.
2. Once the file is deleted, the Once the original file has been
link would still be valid. The deleted, the link becomes
file can be accessed even after invalid.
it has been deleted.
3. The inode number remains the The inode number is different.
same.
4. The command used to create The command to create a soft
a hard link is 'ln'. link is 'ln s'.
5. It consumes less memory. It consumes more memory.

6. It can be linked to its own It can be linked to other file


partition only. systems and can be
networked too.

7. It doesn't have a relative path. It has a relative path.

On Linux and Unix operating systems, all new files are created with a default set of permissions. The umask utility
allows you to view or to set the file mode creation mask, which determines the permissions bits for newly created files
or directories.

Before going further, let’s shortly explain the Linux permissions model.

In Linux, each file is associated with an owner and a group and assigned with permission access rights for three
different classes of users:

 The file owner.


 The group members.
 Everyone else.
There are three permissions types that apply to each class:
 The read permission.
 The write permission.
 The execute permission.
This concept allows you to specify which users are allowed to read the file, write to the file, or execute the file.

To view the file permissions, use the ls command:

ls -l dirnameCopy
drwxr-xr-x 12 linuxize users 4.0K Apr 8 20:51 dirname
|[-][-][-] [------] [---]
| | | | | |
| | | | | +-----------> Group
| | | | +-------------------> Owner
| | | +----------------------------> Others Permissions
| | +-------------------------------> Group Permissions
| +----------------------------------> Owner Permissions
+------------------------------------> File Type

The first character represents the file type which can be a regular file (-), a directory (d), a symbolic link (l), or any
other special type of file.

The next nine characters represent the permissions, three sets of three characters each. The first sets show the
owner permissions, the second one group permissions, and the last set shows everybody else permissions.

Character r with an octal value of 4 stands for read, w with an octal value of 2 for write, x with an octal value
of 1 for execute permission, and (-) with an octal value of 0 for no permissions.

There are also three other special file permissions types: setuid, setgid, and Sticky Bit.

In the example above (rwxr-xr-x) means that the owner has read, write and execute permissions (rwx), the
group and others have read and execute permissions.

If we represent the file permissions using a numeric notation, we will come up to the number 755:

 Owner: rwx = 4+2+1 = 7


 Group: r-x = 4+0+1 = 5
 Other: r-x = 4+0+1 = 5

When represented in numeric notation, permissions can have three or four octal digits (0-7). The first digit represents
the special permissions, and if it is omitted, it means that no special permissions are set on the file. In our
example 755 is the same as 0755. The first digit can be a combination of 4 for setuid, 2 for setgid,
and 1 for Sticky Bit.

File permissions can be changed using the chmod command and ownership using the chown command.

Understanding umask
By default, on Linux systems, the default creation permissions are 666 for files, which gives read and write
permission to user, group, and others, and to 777 for directories, which means read, write and execute permission
to user, group, and others. Linux does not allow a file to be created with execute permissions.

The default creation permissions can be modified using the umask utility.

umask affects only the current shell environment. On most Linux distributions, the default system-wide umask value
is set in the pam_umask.so or /etc/profile file.

If you want to specify a different value on a per-user basis, edit the user’s shell configuration files such
as ~/.bashrc or ~/.zshrc. You can also change the current session umask value by
running umask followed by the desired value.

To view the current mask value, simply type umask without any arguments:

umaskCopy

The output will include the

022
Copy

The umask value contains the permission bits that will NOT be set on the newly created files and directories.

As we have already mentioned, the default creation permissions for files are 666 and for directories 777. To
calculate the permission bits of the new files, subtract the umask value from the default value.

For example, to calculate how umask 022 will affect newly created files and directories, use:

 Files: 666 - 022 = 644. The owner can read and modify the files. Group and others can only read
the files.
 Directories: 777 - 022 = 755.The owner can cd into the directory, and list, read, modify, create
or delete the files in the directory. Group and others can cd into the directory and list and read the files.

You can also display the mask value in symbolic notation using the -S option:

umask -SCopy
u=rwx,g=rx,o=rx

Unlike the numeric notation, the symbolic notation value contains the permission bits that will be set on the newly
created files and directories.

Setting the Mask Value


Before making changes to the umask value, make sure the new value doesn’t pose a potential security risk. Values
less restrictive than 022 should be used with great caution. For example, umask 000 means anyone has read,
write, and execute permissions on all newly created files.
Let’s say we want to set more restrictive permissions for the newly created files and directories so others will not be
able to cd to the directories and read files. The permissions we want are 750 for directories and 640 for files.

To calculate the umask value, simply subtract the desired permissions from the default one:

Umask value: 777-750 = 027

The desired umask value represented in numeric notation is 027.

To permanently set the new value system-wide, open the /etc/profile file with your text editor:

sudo nano /etc/profileCopy

and change or add the following line at the beginning of the file:

/etc/profile
umask 027
Copy

For changes to take effect, run the following source command or log out and log in:

source /etc/profileCopy

To verify the new settings, we will create one new file and directory using mkdir and touch :

mkdir newdirtouch newfileCopyCopy

If you check the permissions using the ls command, you will notice that the new file has 640 and the new
directory 750 permissions, as we wanted:

drwxr-x--- 2 linuxize users 4096 Jul 4 18:14 newdir


-rw-r----- 1 linuxize users 0 Jul 4 18:14 newfile
Copy

Another way to set the file creation mask is by using symbolic notation. For example umask
u=rwx,g=rx,o= is same as umask 027.
Check file access and modification time in Linux
The Linux command line comes with several tools we can use to check file access
and modification time. It will not be necessary to install any extra tools. Check out
some of the examples below for various ways to check file access and
modification time on Linux.

1. The Linux stat command will show us the access time, modification time, and
change time of a file. Just specify the path to a file in your command.

2. $ stat example.txt

The output will look something like this. The bolded lines contain the relevant
information.

File: example.txt

Size: 13367 Blocks: 32 IO Block: 4096


regular file

Device: 805h/2053d Inode: 787524 Links: 1

Access: (0600/-rw-------) Uid: ( 0/ root) Gid: (


0/ root)

Access: 2021-12-15 22:28:53.480000095 -0500

Modify: 2021-12-15 22:28:53.480000095 -0500

Change: 2022-02-20 19:48:33.288001148 -0500

Birth: -

Note the Birth field (last line) is not supported on EXT file systems.
3. Another tool we can use for the job is the ls command. This is already an
extremely common command that all Linux users learn during their first day
anyway. You will need to add the -l option to the command in order to see the
modification time.

4. $ ls -l example.txt

5. -rw------- 1 root root 13367 Dec 15 22:28 example.txt

6. To see the access time for a file with ls, append the -u option in your command.

7. $ ls -u example.txt

8. -rw------- 1 root root 13367 Dec 15 22:28 example.txt

In this case, our access time is the same as the file’s modified time, which is
normal for files that have not been accessed since they were last saved.

9. Yet another tool we can use is the date command. It is not necessary to specify
any extra options, as the modified time will be output in very human readable
format, but we will show you why you might want to use extra options in the next
example.

10. $ date -r example.txt

11. Wed 15 Dec 2021 10:28:53 PM EST

12.The nice thing about using the date command is that we can choose the format
that we want our modified date to be output in. This makes it very friendly to use
inside a Bash script or other type of automation. For example, here we get the
modified date in the format of YYYY-MM-DD-HH-MM-SS.
$ date -r example.txt +"%Y-%m-%d-%H-%M-%S"

2021-12-15-22-28-53

13.The find command comes in handy if we need to see all files that contain a
certain modification time. For example, this command will display all files that
have been modified in the last seven days.

14. $ find . -mtime -7

Or files modified within the last hour:

$ find . -mmin 60

15.The find command can also be used to search for files with a particular access
time. For example, this command will search for files accessed within the last 20
minutes.

16. $ find ~ -amin 20

Or we can search for files with an access time later than 20 minutes ago by
changing to a plus sign.

$ find ~ +amin 20

touch – change file timestamps


touch command is one of these little but extremely useful tools in Unix and Linux which you may have used for quite sometime before
realizing their full potential. In short, it updates file timestamps – access and modification ones (atime and mtime respectively).

Why modify file timestamps?


There are quite a few legitimate reasons why you may want to update timestamps on a certain file. Ranging from source control
approaches to storage usage analysis, there are processes out there which rely on the timestamps associated with each file and directory
of yours.

After all, it’s always useful to know when the file was last modified or when somebody tried to access its contents.

Changing timestamps of a time to the current system time


The default behavior of touch command is to change all three timestamps associated with a file to the current system time.

You simply specify the filename as a command line parameter, no oother options are needed. If there isn’t a file with the specified name,
touch command will create it for you if permissions allow it:

ubuntu$ ls try

ls: try: No such file or directory

ubuntu$ touch try

ubuntu$ ls try

try

ubuntu$ stat try

File: `try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)

Access: 2008-11-17 08:03:02.000000000 -0600

Modify: 2008-11-17 08:03:02.000000000 -0600

Change: 2008-11-17 08:03:02.000000000 -0600

ubuntu$ date

Mon Nov 17 08:03:05 CST 2008

As you can see from the example, the file which isn’t originally found, gets created by the touch command and gets its timestamps set to
the current system time and date.

Changing file timestamps to a specific date and time


If you have a specific time and date you would like to be used for all the timestamps of a file or directory, touch command will gladly
accempt a timestamp template with -t command line option.

Template for the timestamp is quite thorough: [[CC]YY]MMDDhhmm[.ss], but it’s entirely up to you whether to specify the year (either
two-digit or a full form) or not.

This example resets the date to October 16th:

ubuntu$ touch -t 10161000 ./try

ubuntu$ stat ./try

File: `./try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1


Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)

Access: 2008-10-16 10:00:00.000000000 -0500

Modify: 2008-10-16 10:00:00.000000000 -0500

Change: 2008-11-18 03:54:10.000000000 -0600

As you can see from the output, both access time and modification time got updated. The reason change time (ctime) is set to a different
date is because this field reflects the last update to the inode behind a file, and always reflects the current time. In other words, it’s set to
Nov 18th 2008 because of the date of writing this example.

If you fancy adding a year to the timestamp specification, you can specify something from both past and future.

Here’s how easy it is to set atime and mtime to the Oct 16th, 2010 date:

ubuntu$ touch -t 201010161000 ./try

ubuntu$ stat ./try

File: `./try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)

Access: 2010-10-16 10:00:00.000000000 -0500

Modify: 2010-10-16 10:00:00.000000000 -0500

Change: 2008-11-18 03:57:30.000000000 -0600

Modifying atime of a file in Unix


Similar to the commands above, you can use -a option to make touch only update the access time field of a file:

ubuntu$ touch -at 200010161000 ./try

ubuntu$ stat ./try

File: `./try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)

Access: 2000-10-16 10:00:00.000000000 -0500

Modify: 2010-10-16 10:00:00.000000000 -0500

Change: 2008-11-18 04:05:22.000000000 -0600

Modifying mtime of a file in Unix


If you’re interested in updating the modification date only, use -m option:

ubuntu$ touch -mt 200510161000 ./try

ubuntu$ stat ./try

File: `./try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)


Access: 2000-10-16 10:00:00.000000000 -0500

Modify: 2005-10-16 10:00:00.000000000 -0500

Change: 2008-11-18 04:07:12.000000000 -0600

Using a reference file to set atime and mtime


Finally, the really useful option for synchronizing access and modification time fields between multiple files is to use reference file. A
reference file is the file which already has the timestamps you’d like to copy:

ubuntu$ stat /etc/lsb-release

File: `/etc/lsb-release'

Size: 97 Blocks: 8 IO Block: 4096 regular file

Device: 801h/2049d Inode: 1278451 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2008-11-14 05:30:09.000000000 -0600

Modify: 2007-04-12 01:02:52.000000000 -0500

Change: 2007-09-26 02:41:20.000000000 -0500

By specifying this file using a -r option, you can use the touch command to set the same atime and mtime values to any file of yours:

ubuntu$ touch -r /etc/lsb-release ./try

ubuntu$ stat ./try

File: `./try'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 801h/2049d Inode: 655596 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 1000/ greys) Gid: ( 113/ admin)

Access: 2008-11-14 05:30:09.000000000 -0600

Modify: 2007-04-12 01:02:52.000000000 -0500

Change: 2008-11-18 04:09:02.000000000 -0600

You might also like