Unit 04
Unit 04
Unit 04
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)
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.
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
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.
This command specifically changes the group of abhjijit from “group1” to “root.” It is useful when refining
group associations.
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.
user
group
other
+ 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.
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.
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:
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:
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
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.
To calculate the umask value, simply subtract the desired permissions from the default one:
To permanently set the new value system-wide, open the /etc/profile file with your text editor:
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 :
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:
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
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
6. To see the access time for a file with ls, append the -u option in your command.
7. $ ls -u 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.
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.
$ 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.
Or we can search for files with an access time later than 20 minutes ago by
changing to a plus sign.
$ find ~ +amin 20
After all, it’s always useful to know when the file was last modified or when somebody tried to access its contents.
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
ubuntu$ ls try
try
File: `try'
ubuntu$ date
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.
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.
File: `./try'
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:
File: `./try'
File: `./try'
File: `./try'
File: `/etc/lsb-release'
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:
File: `./try'