1. Part 2: Working with Files & Folders
Dr. Jahangir Alam
Computer Engineering Section
University Women’s Polytechnic
Aligarh Muslim University, Aligarh
November 3, 2022
2. The ls Command
ls command on Unix-like operating systems, lists information
about files and directories.
It lists files and directories, and their associated metadata, such
as file size, ownership, and modification time.
The general form of ls command is:
$ls [Option...] [file | folder]...
With no options, ls lists the files contained in the current direc-
tory, sorting them alphabetically.
ls: Important Options
- ls offers a large number of options. Following table lists some
important one:
2 of 180
3. The ls Command (contd...)
Option Description
-a, - -all list all files including hidden file starting with ’.’
-A, - -almost-all Do not list implied . and ...
-l list with long format - show permissions
- -author With -l, print the author of each file.
- -color colored list [=always/never/auto]
-d, - -directory list directories with ’ */’
-F, - -classify Flags filenames by appending / to directories, * to executable files, @ to
symbolic links etc.
-i list file’s inode index number
-la list long format including hidden files
-lh list long format with readable file size
-ls list with long format with file size
-r list in reverse order
-R list recursively directory tree
-s list file size
-S sort by file size
-t sort by time date
-C List entries by columns (default with ls)
-x List entries by lines instead by columns
-X sort by extension name
3 of 180
4. The ls Command (contd...)
ls: Examples
1. Lists the total files in the directory and subdirectories, the names of the files in the current directory, their
permissions, the number of subdirectories in directories listed, the size of the file, and the date of last
modification.
$ls -l
2. Lists files with permissions, shows hidden files, displays them in a column format, and suppresses group
information.
$ls -alxo
3. List the contents of your root directory.
$ls /
4. List the contents of your home directory no matter in which directory you are currently in.
$ls˜
5. Display a list of directories in the current directory.
$ls -d */
6. Display all directories with their contents in the current directory.
$ls */
7. List all files containing the file extension .htm, .php, or .cgi.
$ls *.{htm,php,cgi}
8. Find the file last edited.
$ls -t
9. List files sorted by the time they were last modified in reverse order (most recently modified files last).
$ls -ltr
4 of 180
5. The ls Command (contd...)
10. List only files that begin with a vowel (a, e, i, o, or u).
$ls [aeiou]*
11. Display one file per line.
$ls -1
12. Display File Size in Human Readable Format.
$ls -lh
(h stands for human readable form) and is used to display file size in easy to read format. i.e i.e M for
MB, K for KB, G for GB.
13. Display Files Recursively.
$ls -R
14. Display File Inode Number.
$ls -i
15. Visually classify of files With special characters
$ls -F
16. - -help and - -version have usual behaviour with ls.
5 of 180
6. Shell Globbing: Brief Intro
There may be a large number of files in a folder on a linux
system.
Iterating through a large list of files manually may be error-prone
and not very efficient.
Instead, we can take advantage of Bash wildcards which allow
us to define patterns to match against filenames or strings.
This process is known as globbing.
Globbing is mainly used to match filenames or searching for
content in a file. Globbing uses wildcard characters to create
the pattern.
Note that bash itself cannot recognize Regular Expressions (pat-
tern matching).
6 of 180
7. Shell Globbing: Brief Intro (contd...)
Inside scripts, it is commands and utilities such as sed and awk,
that interpret regular expressions.
The most common wildcard characters that are used for creating
globbing patterns are described below:
Question Mark(?):
’?’ is used to match any single character. We can use ’?’ for
multiple times for matching multiple characters.
Suppose, you want to search those text filenames whose names
are 4 characters long and extension is .txt. You can apply glob-
bing pattern by using ‘?’ four times to do this task as follows:
$ ls ????.txt
7 of 180
8. Shell Globbing: Brief Intro (contd...)
Now suppose, you want to search those document files whose
names are 8 characters long, first 4 characters are f, o, o and t
and extension is doc. Run the following command with globbing
pattern to search the files.
$ ls -l foot????.doc
Suppose, you know the filename is ‘best’ and extension is 3
characters long, but don’t know the extension. Run the follow-
ing command by using ‘?’ to search all files with the name ‘test’
having any extension of three characters long.
$ ls -l best.???
Asterisk(*):
8 of 180
9. Shell Globbing: Brief Intro (contd...)
‘*’ is used to match zero or more characters. If you have less
information to search a file then you can use ‘*’ in globbing
pattern.
Suppose, you want to search all files with ‘pl’ extension. Run
the following command using ‘*’ to do that task.
$ ls -l *.pl
Suppose, you know the starting character of the filename only
which is ‘a’. Run the following command using ‘*’ to search all
files of the current directory whose names are started with ‘a’.
$ ls -l a*.*
9 of 180
10. Shell Globbing: Brief Intro (contd...)
Note: In Bash when the globstar option is enabled, two adjacent
asterisk * used as a single pattern will match all files and zero
or more directories and subdirectories. If followed by a slash /,
it will match only directories and subdirectories.
Square Bracket ([ ]):
‘[ ]’ is used to match the character from the range.
Some of the mostly used range declarations are shown in the
table on the next page.
10 of 180
11. Shell Globbing: Brief Intro (contd...)
Range Description
[:upper:] or [A-Z] All uppercase alphabets are defined by this range
[:lower:] or [a-z] All lowercase alphabets are defined by this range
[:digit:] or [0-9] All numeric digits are defined by this range
[:alpha:] or [a-zA-Z] All uppercase and lower alphabets are defined by this range
[:alnum:] or [a-zA-Z0-9] All uppercase alphabets, lowercase alphabet and digits are defined by
this range
[:ascii:] Any of the 128 ASCII characters. It is a bash specific character class.
[:blank:] Only space and tab characters.
[:cntrl:] Non-printable characters, i.e. control characters.
[:graph:] Graphically printable characters, excluding space.
[:print:] Printable characters, including space.
[:punct:] Punctuation characters.
[:space:] Any white-spaced character, including tabs, newline, carriage-return, and
similar.
[:word:] Alphanumeric characters with underscore character _, meaning alnum
+ _. It is a bash specific character class.
[:xdigit:] Hexadecimal digits.
11 of 180
12. Shell Globbing: Brief Intro (contd...)
Run the following command to search all files and folders whose
name starts with a or b or c or d.
$ ls -l [a-d]*
Run the following command to search all files and folders whose
name starts with A or B or C.
$ ls -l [A-C]*
Run the following command to search all files and folders whose
name starts with any digit from 1 to 5.
$ ls -l [1-5]*
Run the following command to search all files and folders whose
name starts with any alphanumeric character:
12 of 180
13. Shell Globbing: Brief Intro (contd...)
$ ls -l [[:alnum:]]*
Run the following command to search all files and folders whose
name starts with any digit:
$ ls -l [[:digit:]]*
Run the following command to search all files and folders whose
name starts with any alpha character:
$ ls -l [[:alpha:]]*
Note: We shall once again get back to shell globbing in detail once we
proceed to pattern matching.
13 of 180
14. Symbolic Link
A symbolic link, also referred to as a soft link or symlink may
refer to any of the following:
1. It is a file that links to another file or directory using its path. Unlike
a hard link, a symbolic link can link to any file or directory on any
computer.
In Unix like OS symbolic links are created with the ln command, and
in Windows they are created using the mklink command.
2. Alternatively known as SYLK, a symbolic link is an ASCII formatted
file with the extension .slk, used by some Microsoft applications. It can
exchange data between some Microsoft applications such as Excel.
Certainly, here we are interested in the first form of symbolic
links.
Following figure shows how symbolic links are created in Win-
dows:
14 of 180
16. Symbolic Link (contd...)
Once the symbolic link is created, using the dir command we
can see the symbolic link in the directory listing.
To get into the symbolic link directory, we would treat it like
any other directory and use the cd command.
More details on mklink command can be obtained from:
https://www.computerhope.com/mklink.htm
In linux symlink is created using the ln command.
The general form of ln command is:
$ ln -s {source-directory or file} {linkname}
For example the following command creates a symlink named
as mylink1 to the file sss in folder Desktop:
16 of 180
18. The pwd Command
pwd command on Unix-like operating systems, outputs the name
of the current working directory.
pwd stands for print working directory.
It is a built-in shell utility which could be confirmed using the
command type pwd.
The general form of pwd command is:
$pwd [Option]
Two options available with pwd command are -L and -P.
-L prints symbolic links, if any, while -P prints physical links.
Execute the following commands:
18 of 180
19. The pwd Command (contd...)
$type pwd
$pwd
$pwd -L
$pwd -P
- -help and - -version have usual behaviour with pwd.
19 of 180
20. The tree Command
tree command on Unix-like operating systems, lists the contents
of directories in a tree-like format.
It can be used to understand/ structure your file system.
tree is a recursive directory listing program that produces a
depth-indented listing of files and outputs it to tty (terminal).
(Note: It is colorized only if the LS_COLORS environment
variable is set).
With no arguments, tree lists the files in the current directory.
When directory arguments are given, tree lists all the files and/or
directories found in the given directories each in turn.
tree then returns the total number of files and/or directories
listed.
20 of 180
21. The tree Command (contd...)
By default, when a symbolic link is encountered, the path that
the symbolic link refers to is printed after the name of the link
in the format: name→real-path.
The general form of tree command is:
$tree [Options][directory]
tree: Important Options
- Like ls, tree also offers a large number of options.
- Detailed listing of options is available at:
https://www.geeksforgeeks.org/tree-command-unixlinux/
- Following table lists some important one:
21 of 180
22. The tree Command (contd...)
Option Description
-a All files are printed. By default, tree does not print hidden files (those
beginning with a dot ’.’).
-d List directories only.
-f Prints the full path prefix for each file.
-i Tree will not print the indentation lines. Useful when used in conjunction
with the -f option.
-P pattern List only those files that match the wild-card pattern*
-I pattern Do not list those files that match the wild-card pattern.
- -prune Makes tree prune empty directories from the output, useful when used in
conjunction with -P or -I.
- -filelimit # Do not descend directories that contain more than # entries.
- -timefmt format Prints (implies -D) and formats the date according to the format string
which uses the strftime syntax (strftime( ) C function, part of the time.h
library).
- -noreport Omits printing of the file and directory report at the end of the tree listing.
-p Print the protections for each file (as per ls -l).
-s Print the size of each file along with the name.
-u Print the username, or UID # if no username is available, of the file.
-g Print the group name, or GID # if no group name is available, of the file.
-D Print the date of the last modification time for the file listed.
- -inodes Prints the inode number of the file or directory.
- -device Prints the device number to which the file or directory belongs.
-F Appends a ’/’ for directories, a ’=’ for socket files, a ’*’ for executable files
etc. as per ls -F
-r Sort the output in reverse alphabetic order.
-t Sort the output by last modification time instead of alphabetically.
22 of 180
23. The tree Command (contd...)
tree: Examples
1. Display the contents of the current directory and subdirectories in a tree format.
$tree
2. Show all files including hidden dot files
$tree -a
3. Display the tree hierarchy of a directory.
$tree -a ahmad
4. Displays a tree that lists all directories but only those files that begin with t.
$tree -P ’t*’
5. List a tree that shows the file permissions.
$tree -p
6. Displays a tree containing all directories plus files that begin with t. Also display file permissions
$tree -pP t*
7. Prints the device number to which the file or directory belongs.
$tree - -device
8. List the directory contents with the full path prefix for each sub-directory and file
$tree -f
9. Print the output by last modification time instead of alphabetically.
$tree -t
10. You can specify the maximum display depth of the directory tree using the -L option. For example, if you
want a depth of 2, run the following command.
$tree -L 2
23 of 180
24. The tree Command (contd...)
11. We can also tell the tree to prune empty directories from the output by adding the –prune option, as
shown below.
$tree - -prune
12. Besides, to print the username of each file, use the -u option, and the -g option prints the group name.
We can combine the -p, -u and -g options to do a long listing (somewhat similar to ls command) as shown
below:
$tree -pug
13. We can also print the size of each file in bytes along with the name using the -s option. To print the size
of each file but in a human-readable format, use the -h flag as shown below:
$tree -s
OR
$tree -h
14. To display the date of the last modification time for each sub-directory or file, use the -D options as
follows:
$tree -D
15. Another useful option is - -du, which reports the size of each sub-directory as the accumulation of sizes
of all its files and subdirectories (and their files, and so on).
$tree - -du
16. Last but not least, you can send or redirect the tree’s output to filename for later analysis using the -o
option.
$tree -o target_file
17. Same thing could be done using:
$tree target_file
24 of 180
25. Creating a file in Linux
Following are various ways of creating a file in linux:
1. Using redirect operator ()
2. Using cat command
3. Using echo command
4. Using printf command
5. Using a text editor
6. Using touch command
Using redirect operator:
The redirection symbol tells the system to output results into
whatever we specify next.
The target is usually a filename. we can use this symbol to
create a new file as follows:
$ myfile1.txt
25 of 180
26. Creating a file in Linux (contd...)
Above command creates an empty file named as ’myfile1.txt’.
You can confirm using ls -l command.
Using cat command:
cat stands for ’catenate’. It is a multipurpose command.
It is one of the most commonly-used commands in Unix-like
operating systems. It can be used to:
- Display files
- Create new files
- Copy files
- Append the contents of a file to the end of another file i.e. combining
them
Display the contents of a file:
26 of 180
27. Creating a file in Linux (contd...)
The simplest way to use cat is to give it the name of a text
file. It displays the contents of the text file on the screen. For
instance:
$cat myfile.txt
OR
$cat myfile.txt
...will read the contents of mytext.txt and send them to standard
output (your terminal screen).
If we specify more than one file name, cat displays those files one
after the other, catenating their contents to standard output.
So the command:
$cat myfile.txt myfile1.txt
27 of 180
28. Creating a file in Linux (contd...)
If a filename starts with dash (-) use - - option with cat to
dispaly file contents:
$cat - - -filename
Will print the contents of these two files as if they were a single
file.
Create new files:
To create a file using cat, use the following command:
$cat myfile3.txt
Press enter and typeset whatever you want. Save with Ctrl+d.
Note the redirection operator. Without this the command dis-
plays the contents of myfile3.txt on the screen.
28 of 180
29. Creating a file in Linux (contd...)
The redirection operator () tells the system to place the typed
contents in the myfile3.txt file.
If myfile3.txt does not exist, it will be created.
Ifmyfile3.txt already exists, it will be overwritten and its previous
contents will be lost, so be careful.
Copy files:
We can use cat to make copies of files.
cat sends its output to stdout (standard output), which is usu-
ally the terminal screen.
However, we can redirect this output to a file using the shell
redirection symbol . For instance the following command:
29 of 180
30. Creating a file in Linux (contd...)
$cat myfile3.txt myfile4.txt
OR
$cat myfile3.txt myfile4.txt
Sends (copies) contents of myfile3.txt to myfile4.txt.
If myfile4.txt does not exist, it will be created.
If myfile4.txt already exists, it will be overwritten and its previ-
ous contents will be lost, so be careful.
Similarly, we can catenate several files into one destination file.
For instance:
$cat mytext1.txt mytext2.txt newfile.txt
Append contents of one file to another:
30 of 180
31. Creating a file in Linux (contd...)
Instead of overwriting another file, we can also append a source
text file to another using the double redirection or append op-
erator (»). For instance the command:
$cat mytext1.txt myfile2.txt
will read the contents of myfile1.txt, and write them at the end
of file myfile2.txt.
If myfile2.txt. does not already exist, it will be created and the
contents of myfile1.txt will be written to the new file.
cat can also be used to append contents of multiple files to
another file.
cat: Important Options
- cat offers several options. Following table lists some important
one:
31 of 180
32. Creating a file in Linux (contd...)
Option Description
-b, - -number-nonblank Number non-empty output lines. This option over-
rides -n.
-E, - -show-ends Display $ at end of each line.
-n, - -number Number all output lines.
-s, - -squeeze-blank Suppress repeated empty output lines.
-T, - -show-tabs Display TAB characters as ∧I.
- -help Display a help message, and exit.
- -version Output version information, and exit.
- Examples: Run following commands:
$cat -b myfile1.txt
$cat -E myfile1.txt
$cat -n myfile1.txt
$cat -s myfile1.txt
$cat -T myfile1.txt
$cat - - help
$cat - - version
32 of 180
33. Creating a file in Linux (contd...)
Using echo command:
The echo command will duplicate whatever we give in the com-
mand and put it into a file we specify. Run the following:
$echo The quick brown fox jumps over the lazy dog myfile5.txt
Using printf command:
The printf command works like the echo command, and it adds
some formatting functionality. (However, formatting could also
be added using echo command)
To add two lines of text, enter:
$printf Hello linux.n You are simple awesome.n myfile6.txt
Using a text editor:
33 of 180
34. Creating a file in Linux (contd...)
A large number of text editors are available for linux. They could
be used to create files like ordinary text editors e.g. notepad,
wordpad etc.
Some text editors for linux are gedit, kwrite, pico, nano, vi, vim
etc.
Using touch command:
On Unix-like operating systems, the touch command modifies
file timestamps.
If the file doesn’t exist, an empty file with that name is created.
A timestamp is information associated with a file that identifies
an important time in the file’s history.
A file can have multiple timestamps, and some of them can be
forged by setting them manually.
34 of 180
35. Creating a file in Linux (contd...)
In Linux, there are three timestamps associated with a file:
Timestamp Type Description Abbreviation
Access time The last time the file was read. atime
Modification time The last time the contents of the file were
modified.
mtime
Change Time The last time the file’s metadata, called the
Status, was changed. Status information in-
cludes a file’s permissions and its timestamps.
Every time anything happens to a file, at least
one element of its status changes, and its
ctime is set to the current system time.
ctime
The atime and mtime are part of a file’s status metadata.
Therefore, when we change the atime (-a) or mtime (-m) of
a file, its ctime is automatically set to the current time.
There is no way to manually set the ctime.
35 of 180
36. Creating a file in Linux (contd...)
A file’s atime or mtime can be set to the future or the past if
the user owns the file.
Windows operating system associates another time, named as
Create time with the file. It should not be confused with Change
time.
Newer Linux reports create time with the name of ’birth time’.
The ’birth time’ of a file can’t be changed.
Difference between atime, mtime ctime:
Every Linux file has three timestamps: the access timestamp
(atime), the modified timestamp (mtime), and the changed
timestamp (ctime).
36 of 180
37. Creating a file in Linux (contd...)
The access timestamp is the last time a file was read. This
means someone used a program to display the contents of the
file or read some values from it. Nothing was edited or added
to the file. The data was referenced but unchanged.
A modified timestamp signifies the last time the contents of
a file were modified. A program or process either edited or
manipulated the file. “Modified” means something inside the
file was amended or deleted, or new data was added.
Changed timestamps aren’t referring to changes made to the
contents of a file. Rather, it’s the time at which the metadata
related to the file was changed. File permission changes, for
example, will update the changed timestamp.
Viewing Timestamps:
37 of 180
38. Creating a file in Linux (contd...)
Have a look at following table:
S.No. Description Command
1. When we use the -l (long listing) option with ls, we can
access the modified timestamp.
ls -l myfile.txt
2. If we want to see the access timestamp, use the -lu
(access time) option with ls
ls -lu myfile.txt
3. And finally, to see the change timestamp, we can use
the -lc (change time) option with ls
ls -lc myfile.txt
To see all timestamps simultaneously, use the stat command as
follows:
$stat myfile.txt
Changing Timestamps:
With no options, touch changes the atime, mtime, and ctime
of file to the current system time e.g.:
38 of 180
39. Creating a file in Linux (contd...)
$touch myfile.txt
If file doesn’t exist, it is created with current time as timestamps.
touch: Important Options
- touch offers several options. Following table lists some impor-
tant one:
39 of 180
40. Creating a file in Linux (contd...)
Option Description
-a Set the access time only.
-c, - -no-create Do not create files.
-d datestring, - -date=datestring Parse the date string datestring, and use it instead of current
time. Strings valid to the date command are accepted by the -d
option. In addition to having write access, the user must also
own a file to set its times to the past or future.
-h, –no-dereference If file is a symbolic link and this option is specified, touch modifies
the timestamp of the symlink, rather than its referenced file. If
this option is not specified, touch will dereference symlinks before
making modifications.
This option implies -c: nothing is created if file does not exist.
-m Set modification time only.
-r=reffile, –reference=reffile Set the times of file to the times of file reffile instead of the
current time.
In addition to having write access, the user must also own a file
to set its times to the past or future.
-t timestamp Use the numeric timestamp instead of the current time. The
format of timestamp is [[CC]YY]MMDDhhmm[.ss].
In addition to having write access, the user must also own a file
to set its times to the past or future.
40 of 180
41. Creating a file in Linux (contd...)
Option Description
- -time=timetype An alternate way to specify what type of time to set (as with -a
and -m). The value of timetype must be one of the following:
- atime, access, use: Set access time. (Equivalent to -a.)
- mtime, modify: Set modification time. Equivalent to -m.
This option can be specified twice nondestructively. For example,
- -time=atime - -time=mtime is the same as -am.
- -help Display a help message, and exit.
- -version Display version information, and exit.
touch: Examples
- In following command: If file.txt exists, set its access, modi-
fication, and change times (atime, mtime, and ctime) to the
current system time. If file.txt doesn’t exist, create an empty
file with that name.
$touch file.txt
41 of 180
42. Creating a file in Linux (contd...)
- The following commands set file times to the current system
time. If file does not exist, does nothing.
$touch -c file.txt
OR
$touch -h file.txt
- The following command changes the atime of file.txt. The
mtime is not changed. The ctime is set to the current system
time. If file.txt does not exist, it is created.
$touch -a file.txt
- Change the access and modification times of file2.txt to match
the times of file1.txt. The ctime will be set to the current
system time. If file2.txt does not exist, it is not created.
$touch -cr file1.txt file2.txt
42 of 180
43. Creating a file in Linux (contd...)
- The following command changes the atime and mtime of file2.txt
to match the atime and mtime of a.txt. If file2.txt doesn’t exist,
do nothing. If file2.txt is a symlink, set the times of the symlink.
Do not touch the referenced file.
$touch -ahmcr file1.txt file2.txt
- The above command could also be stated as follows:
$touch –time=atime –no-dereference –time=mtime –no-create –
reference=file1.txt file2.txt
- The following command sets the atime and mtime of file1.txt
to 1:02 AM, today.
$touch -d 01:02 file1.txt
- Above command can also be stated as:
$touch -d 1:2 file1.txt
43 of 180
44. Creating a file in Linux (contd...)
- The following command sets the mtime of file1.txt to September
1, 1927, 11:58 PM and 59 seconds. The ctime is set to the
current system time. The atime is not changed.
$touch -md Sep 1 1927 23:58:59 file1.txt
- The following command sets the mtime of file1.txt to September
1, 1927, 11:58 PM and 59 seconds. The ctime is set to the
current system time. The atime is not changed.
$touch –time=01020304 file1.txt
- The following command sets the atime and mtime of file1.txt to
June 7, 2050, 4:05 AM. The ctime is set to the current system
time.
$touch -t 5006070405 file1.txt
44 of 180
45. Creating a file in Linux (contd...)
- Following command is same as the previous command, but ex-
plicitly specifying the century (20).
$touch -t 205006070405 file1.txt
- The following command sets the atime of file1.txt to March 4,
1950, 5:06 AM and 59 seconds. The ctime is set to the current
system time. The mtime is not changed.
$touch -at 195003040506.59 file1.txt
- As pointed out earlier ctime of a file can’t be set to past or
future. Any change in file metadata, sets the ctime to current
system time. One way of setting ctime to current system time
is to alter file permissions using chmod.
45 of 180
46. Creating a file in Linux (contd...)
Note 1:
ctime is the inode change time. If reading a file, its atime will be updated, which should
cause inode member i_atime changed, which is an inode change. So ctime should also be
updated. But if I try to ls a directory on redhat, only the directory atime gets updated, not
ctime. Why?
Answer:
Inodes are data about a file in a given filesystem. What is stored in an inode is system
dependent, there is no standard. POSIX compliant systems specifically update ctime (where
ever it is kept) only with a change involving:
1. file creation
2. file permissions
3. acl changes
Otherwise ctime would have no meaning because it would be identical to atime. Every read
access would update the file status time as well as the access time Check out the utime()
call on your system, or google for ’opengroup.org: utime’. utime() does update ctime.
if we consider inode change, it means to owner/group/mode change
Note 2:
46 of 180
47. Creating a file in Linux (contd...)
One of the things which confuses many Linux users is why the access time attribute of
a file does not change, although the file has been clearly accessed a number of times
recently.
The answer to this question lies in the mount option used by Linux while mounting the
filesystem.
The Linux Kernel starting from version 2.6.30 switched to using the relatime option by
default during file system mount.
This option causes the atime attribute to update only if the previous atime is older than
mtime or ctime, or the previous atime is over 24 hours old.
If the Kernel was to update the atime everytime a file was accessed that would be a
big performance killer for disks. Specially in servers with lots of files which are accessed
frequently, updating the atime attribute everytime a file is accessed would be a huge
I/O burden, that is why the Kernel now defaults to relatime.
If you want to have the original atime functionality you must use the strictatime option.
In that case use the following command:
$sudo mount -o remount,strictatime /home
Above command restores the original atime functionality for the current session.
47 of 180
48. The file Command
On Unix-like operating systems, the file command reports a file’s
type.
The file command attempts to classify each filesystem object
(i.e., file, directory or link) that is provided to it as an argument
(i.e., input).
Thus, it can usually provide immediate information as to whether
some specified object is, for example, a GIF image file, a direc-
tory, a GNU tar archive, ASCII English text, a symbolic link,
an HTML document, an empty file, bzip2 compressed data, an
executable, etc.
The file command tests each argument in an attempt to classify
it. There are three tests performed in this order: filesystem
tests, magic tests, and language tests.
48 of 180
49. The file Command (contd...)
The first test that succeeds causes the file type to be printed.
The first is a filesystem test, which uses the stat system call
to obtain information from the object’s inode (which contains
information about a file).
A system call is a request in a Unix-like operating system for a
service performed by the kernel (i.e., the core of the operating
system).
The second test checks to see if there is a magic number, which
is a number embedded at or near the beginning of many types
of files that indicates the file format (i.e., the type of file).
49 of 180
50. The file Command (contd...)
In the event that the first two tests fail to determine the type of
a file, language tests are employed to determine if it is plain text
(i.e., composed entirely of human-readable characters), and, if
so, what type of plain text, such as HTML (hypertext markup
language) or source code (i.e., the original version of a program
as written by a human).
In this situation, file also attempts to determine the natural
language (e.g., English, Turkish or Japanese) that is used in the
file.
The general form of file command is:
$file [Option(s)] object_name(s)
50 of 180
51. The file Command (contd...)
file: Important Options
- file offers several options. Following table lists some important
one:
Option Description
-b, - -brief Do not prepend (add) file names to output lines (brief mode).
-C, - -compile Write a magic.mgc output file that contains a pre-parsed version of the magic
file or directory.
-e, - -exclude
testname
Exclude the test named in testname from the list of tests made to determine the
file type. Valid test names are: apptype -EMX application type (only on EMX),
ascii - Various types of text files (this test will try to guess the text encoding,
irrespective of the setting of the ‘encoding’ option), encoding - Different text
encodings for soft magic tests, tokens -Ignored for backward compatibility, cdf
- Prints details of Compound Document Files, compress- Checks for, and looks
inside, compressed files, elf - Prints ELF file details, soft - Consults magic files,
tar - Examines tar files.
-F, - -separator
separator
Use the specified string separator as the separator between the file name and
the file result returned. Defaults to ‘:’.
-f, - -files-from
namefile
Read the names of the files to be examined from namefile (one per line) before
the argument list. Either namefile or at least one file name argument must be
present.
-h, - -no-
dereference
option causes symlinks not to be followed (on systems that support sym-
bolic links). This option is the default if the environment variable
POSIXLY_CORRECT is not defined.
51 of 180
52. The file Command (contd...)
Option Description
-i, - -mime Causes the file command to output mime type strings rather than the more
traditional human readable ones. Thus it may say ‘text/plain; charset=us-ascii’
rather than ASCII text.
- -mime-type, - -
mime-encoding
Like -i, but print only the specified element(s).
-l Shows sorted patterns list in the order that is used for the matching.
-N, - -no-pad Don’t pad file names so that they align in the output.
-r, - -raw Don’t translate unprintable characters to ooo. Normally file translates unprint-
able characters to their octal representation.
-v, - -version Print the version of the program and exit.
-0, - -print0 Output a null character ‘0’ after the end of the file name, which is helpful
if, for instance, you’d like to cut the output. This option does not affect the
separator, which is still printed.
- -help Print a help message and exit.
Note: mime stands for Multipurpose Internet Mail Extensions.
file: Examples
- Choose any one file from your home directory and determine its
type? The command is as follows:
52 of 180
53. The file Command (contd...)
$file file_name
- For all files in your home directory and determine their type?
The command is as follows:
$file *
- Repeat both scenarios discussed above to determine brief infor-
mation (excluding filename) about file/ files:
$file -b file_name
$file -b *
- Exclude ascii test for a ascii text file and check what information
related to type of file is given by file command
$file -e ascii ascii_file_name
The file command should inform ascii_file_name as a data file.
53 of 180
54. The file Command (contd...)
- The default separator between filename and result returned by
file command is :. Use some other character or string as a
separator:
$file -F - - - - *
- Determine the types of files whose name are stored in another
file say example.txt.
$file -f example.txt
- For all files output mime type (A media type, also known as a
Multipurpose Internet Mail Extensions or MIME type, indicates
the nature and format of a document, file) strings rather than
the more traditional human readable ones.
$file -i *
- Do not pad file names so that they are aligned in output.
54 of 180
55. The file Command (contd...)
$file -N *
- Check types of all files starting with a,e,i,o,u.
$file [aeiou]*
- Check types of all files in the character range a-f.
$file [a-f]*
- Check types of all files having extensions abc or txt or html.
$file *.{abc,txt,html}
- Version details.
$file -v
- Help.
$file - -help
55 of 180
56. Deleting Files in Linux
Following commands are used in Linux to remove files:
- The rm command
- The unlink command
- The shred command
The rm command:
On Unix-like operating systems, the rm command removes (deletes)
files.
rm removes each file specified on the command line. By default,
it does not remove directories.
When rm is executed with the -r or -R options, it recursively
deletes any matching directories, their subdirectories, and all
files they contain.
56 of 180
57. Deleting Files in Linux (contd...)
The removal process unlinks a file name in a filesystem from its
associated data, and marks that space on the storage device as
usable by future writes.
In other words, when you remove a file, the data in the file isn’t
changed, but it’s no longer associated with a filename.
The data itself is not destroyed, but after being unlinked with
rm, it becomes inaccessible.
Remove your files wisely! It’s not like putting something in the
Windows Recycle Bin; once you rm a file or directory, there is
no way to undo it.
rm: Important Options
- Following options are available with rm command:
57 of 180
58. Deleting Files in Linux (contd...)
Option Description
-f, - -force Ignore nonexistant files, and never prompt before removing.
-i Prompt before every removal.
-I Prompt once before removing more than three files, or when removing
recursively. This option is less intrusive than -i, but still gives protection
against most mistakes.
- -interactive[=WHEN] Prompt according to WHEN: never, once (-I), or always (-i). If WHEN
is not specified, then prompt always.
- -one-file-system When removing a hierarchy recursively, skip any directory that is on a file
system different from that of the corresponding command line argument.
- -no-preserve-root Do not treat / (the root directory) in any special way.
- -preserve-root Do not remove / (the root directory), which is the default behavior.
r, -R, - -recursive Remove directories and their contents recursively.
-d, - -dir Remove empty directories. This option permits you to remove a directory
without specifying -r/-R/–recursive, provided that the directory is empty.
In other words, rm -d is equivalent to using rmdir.
-v, - -verbose Verbose mode; explain at all times what is being done.
- -help Display a help message, and exit.
- -version Display version information, and exit.
58 of 180
59. Deleting Files in Linux (contd...)
rm: Examples
- The following command removes the file myfile.txt. If the file is
write-protected, we will be prompted to confirm that we really
want to delete it.
$rm myfile.txt
- The following command removes the file myfile1.txt. We will
not be prompted, even if the file is write-protected. if rm can
delete the file, it will.
$rm -f myfile1.txt
- The following command removes the files myfile2.txt, myfile3.txt
and myfile4.txt. If any of these files is write-protected, we will
be prompted to confirm that we really want to delete that.
$rm myfile2.txt myfile3.txt myfile4.txt
59 of 180
60. Deleting Files in Linux (contd...)
- The following command removes all files in the working direc-
tory. If any of the files is write-protected, we will be prompted
before rm removes that.
$rm *
- The following command removes all files in the working direc-
tory. rm will not prompt us for any reason before deleting them.
$rm -f *
- The following command attempts to remove every file in the
working directory, but prompt before each file to confirm.
$rm -i *
- The following command removes every file in the working direc-
tory; prompt for confirmation if more than three files are being
deleted.
60 of 180
61. Deleting Files in Linux (contd...)
$rm -I *
- The following command removes the directory mydir, and any
files and directories it contains. If a file or directory that rm
tries to delete is write-protected, we will be prompted to make
sure that you really want to delete it.
If the specified directory is empty, it may also be removed with
the -d/- -dir option, instead.
$rm -r mydir
- The following command is same as the above command, but
we will never be prompted; if rm can delete the directory and
files, it will.
$rm -rf mydir
61 of 180
62. Deleting Files in Linux (contd...)
- To remove a file whose name begins with a dash (-), say (-
zfile), we can specify a double dash (- -) separately before the
file name.
- This extra dash is necessary so that rm does not misinterpret
the filename as an option.
$rm - - -zfile
- Or, we can delete it by referring to it with a pathname.
- For instance, if the file -zfile is located in the directory /home/
jahangir/dels, we could delete it using:
$rm /home/jahangir/dels/zfile
The unlink command:
62 of 180
63. Deleting Files in Linux (contd...)
On Unix-like operating systems, the unlink command calls and
directly interfaces with the unlink system function, which re-
moves a specified file (same as done by rm).
Note that only filename is unlinked with file data which still
remains available and can be manipulated using a third party
program before the data space is used by any other program.
unlink supports only two optios: - -help and - -, which have their
usual meaning.
unlink: Examples
- The following command removes the file name xyz.txt:
$unlink xyz.txt
63 of 180
64. Deleting Files in Linux (contd...)
Note that unlink operates on only one file at a time and it does
not operate on directories.
rm and unlink: Differences
The unlink command essentially does the same thing as rm, but
it is defined very strictly by the POSIX standard.
It operates on only one file at a time; it does not operate on
directories; its behavior is not modified by any command line
options.
It does one thing and one thing only: calls the unlink() system
call on a single file.
The version of rm used by most versions of Linux (GNU rm)
has all the options and details listed above - safety checks, in-
teractive prompting, conditional deletion, recursive operation.
64 of 180
65. Deleting Files in Linux (contd...)
It is similar to unlink in that it makes the unlink() system call,
but it may also call unlinkat() (removes a directory entry relative
to a directory file descriptor) if a specified pathname is relative
rather than absolute.
In other words, rm is a much friendlier way to use unlink.
The shred command:
On Linux operating systems, the shred command overwrites to
a file to hide its contents, and optionally delete it.
The shred program was created by the GNU project. On non-
Linux operating systems, it has the command name gshred.
shred is a program that will overwrite your files in a way that
makes them very difficult to recover by a third party.
65 of 180
66. Deleting Files in Linux (contd...)
Normally, when we delete a file, that portion of the disk is
marked as being ready for another file to be written to it, but
the data is still there.
If a third party were to gain physical access to your disk, they
could, using advanced techniques, access the data that you
thought you had deleted.
The way that shred accomplishes this type of destruction digi-
tally is to overwrite (over and over, repeatedly, as many times
as you specify) the data you want to destroy, replacing it with
other (usually random) data.
Doing this magnetically destroys the data on the disk and makes
it highly improbable that it can ever be recovered.
shred: Important Options
66 of 180
67. Deleting Files in Linux (contd...)
- Following options are available with shred command:
Option Description
-f, - -force Change permissions to allow writing if necessary.
-n, - -iterations=N Overwrite N times instead of the default (3).
-s, - -size=N Shred this many bytes (suffixes like K, M, G accepted).
-u, - -remove Truncate and remove file after overwriting.
-v, - -verbose Show verbose information about shredding progress.
-x, - -exact Do not round file sizes up to the next full block; this is the default for
non-regular files such as device names.
-z, - -zero Add a final overwrite with zeros to hide shredding.
- Shred standard output.
- -help Display this help and exit.
- -version Output version information and exit.
shred: Examples
- The following command overwrites the data of myfile1.txt, my-
file2.jpg, and myfile3.doc:
67 of 180
68. Deleting Files in Linux (contd...)
$shred myfile1.txt myfile2.jpg myfile3.doc
- The following command overwrites the data of myfile1.txt, my-
file2.jpg, and myfile3.doc:
$shred myfile1.txt myfile2.jpg myfile3.doc
- Following command is same as above, but also deletes those
three files, freeing up that space on the disk for later use.
$shred -u myfile1.txt myfile2.jpg myfile3.doc
- Following command overwrites first 10 bytes of the file file1.txt
instead of all bytes.
$shred -s10 file1.txt
OR
$shred - -size=10 file1.txt
68 of 180
69. Deleting Files in Linux (contd...)
- Following command overwrites the file (file2.txt) 5 times instead
to default 3
$shred -n5 file2.txt
OR
$shred - -iterations=5 file2.txt
- Following command overwrites the file (file3.txt) and displays
message about that:
$shred -v file3.txt
- Following command adds a final overwrite with zeros to file4.txt
to hide shredding.
$shred -z file4.txt
- Following command overwrite all data on the partition /de-
v/hda3.
69 of 180
70. Deleting Files in Linux (contd...)
$shred -/dev/hda3.
Note: Take note that shred relies on an important as-
sumption: that the file system overwrites data in-place.
This assumption is the traditional way to do things, but
many modern file systems do not do things exactly this
way.
Refer to:
https://www.computerhope.com/unix/shred.htm
for details.
70 of 180
71. The mkdir Command
On Unix-like operating systems, the mkdir command creates
new directories (folders) in a file system.
mkdir: Important Options
- mkdir offers several options. Following table lists some impor-
tant one:
Option Description
directory The name of the directory to be created. If the specified directory does not
already exist, mkdir creates it. More than one directory may be specified.
A specified directory can include path information. For instance, if the current
directory is /home/hope, and we’d like to create the directory /home/hope/-
Documents/writing, we can use the command mkdir Documents/writing. If
the Documents folder does not already exist, we should specify the -p option to
create it automatically, otherwise the command will fail.
-m=mode,
- -mode=mode
We can use the -m option to set a file mode (permissions, etc.) for the created
directories. The syntax of mode is the same as with the chmod command.
-p, - -parents Create parent directories as necessary. When this option is specified, no error is
reported if a directory already exists.
71 of 180
72. The mkdir Command (contd...)
Option Description
-v,- -verbose Verbose output. Print a message for each created directory.
- -help Display a help message, and exit.
- -version Display version information, and exit.
mkdir: Examples
- Create a new directory called mydir in the current directory.
$mkdir mydir
- The following command creates the directory mydir1 in your
home directory, specified here with a tilde (∼). The command
could be given from any director and mydir1 is created in home
directory.
$mkdir ∼/mydir1
72 of 180
73. The mkdir Command (contd...)
- The following command creates the directory structures /home/
jahangir/Desktop/mypdfs/pdfdocs. If any of the parent
directories e.g. /home/jahangir/Desktop/mypdfs do not al-
ready exist, they will automatically be created.
$mkdir -p /home/jahangir/Desktop/mypdfs/pdfdocs
OR
$mkdir -p ∼/Desktop/mypdfs/pdfdocs
- The following command create the mydir2 directory, and set its
file mode (-m) so that all users (a) may read (r), write (w), and
execute (x) it.
$mkdir -m a=rwx mydir2
73 of 180
74. The mkdir Command (contd...)
For directories, this means that any user on the system may view
(read), and create/modify/delete (write) files in the direc-
tory. Any user may also change to (execute) the directory,
for example with the cd command.
- The following command displays a message when the specified
directory is created by mkdir command:
$mkdir -v mydir3
- -help and - -version have usual meanings with mkdir.
74 of 180
75. The Anatomy of Linux Folders (directories)
Every Linux installation’s folders structure (also called file sys-
tem), at an abstract level, could be explained as shown in fol-
lowing figure.
Generally, organization and type of system folders is distribution
dependent. So the following expansion of above diagram is
strictly for Ubuntu installation:
75 of 180
76. The Anatomy of Linux Folders (directories) (contd...)
This is why a users home directory is named as /home/user_name
(e.g. /home/jahangir).
76 of 180
77. The Anatomy of Linux Folders (directories) (contd...)
Generally a user is NOT allowed to operate in system folders
and home folder as long as he is not one of the following:
- A superuser.
- A member of the sudo group (administrative group).
- He is assigned privileges (by superuser) to access a particular system
folder(s).
All files and folders on your system stem from one main folder:
the root folder. There are no folders above the root folder;
all other folders are below the root folder.
Any folder contained inside another directory is called a sub-
folder or subdirectory. Subdirectories branch off the root
of the folder tree.
Unlike a real tree, folders trees are upside-down.
77 of 180
78. The Anatomy of Linux Folders (directories) (contd...)
Folders are separated by a forward slash (/). For instance, the
folder name documents/work/accounting means the folder
named accounting, which is in the folder named work, which is
in the folder named documents, which is in the current folder.
All folders on your file system are subdirectories of the root
folder.
By default, when you open a terminal and begin using the com-
mand line, you are placed in your home folder.
78 of 180
79. Navigating the Linux file system: cd Command
On Unix-like operating systems, the cd command (change di-
rectory) changes the shell’s current working directory.
cd is among the commands we use most often on the command
line.
It changes our working directory. We use it to move around in
the hierarchy of our file system.
The general form of cd command is:
$cd [-L | -P [-e]] directory
Following table describes the options:
79 of 180
80. Navigating the Linux file system: cd Command
(contd...)
Option Description
-L Force symbolic links to be followed. In other words, if you tell cd to move into a di-
rectory, which is actually a symbolic link to a directory, it moves into the directory the
symbolic link points to.
This option is the default behavior of cd; normally, it will always act as if -L was specified.
-P Use the physical directory structure without following symbolic links. In other words, only
change into the specified directory if it actually exists as named; symbolic links will not
be followed. This option is the opposite of the -L option, and if they are both specified,
this option will be ignored.
-e If the -P option is specified, and the current working directory cannot be determined, this
option tells cd to exit with an error. If -P is not specified along with this option, this
option has no function.
cd: Examples
- To change into the directory say xyz (which is in your home
directory), making it working directory, use the command:
$cd xyz
80 of 180
81. Navigating the Linux file system: cd Command
(contd...)
- To come back to your home directory and change into the doc-
uments/work/accounting directory, and make it the current
working directory, use the command sequence:
$cd ∼
$cd documents/work/accounting
If the first character of a directory name is a slash, that denotes
that the directory path begins in the root directory.
So, in contrast to the example above, the directory name /doc-
uments/work/accounting (note the beginning slash) means
the directory named accounting, which is in the directory named
work, which is in the directory named documents, which is in
the root directory.
81 of 180
82. Navigating the Linux file system: cd Command
(contd...)
To change into this directory, making it working directory, use
the command:
$cd /documents/work/accounting
To change into the root directory, making it your working di-
rectory, use the command:
$cd /
Again note that you will not be able to make any changes to
the root directory (or any other system folder) on your system
unless you are logged in as root, or using the sudo command.
The current directory, regardless of which directory it is, is rep-
resented by a single dot (.). So, running the command:
82 of 180
83. Navigating the Linux file system: cd Command
(contd...)
$cd .
would change us into the current directory. In other words, it
would do nothing.
What’s actually happening is that the dot represents the as-
sumed directory. It’s a placeholder, and we can use the dot
anywhere in a directory name. So, following commands are
identical:
$cd ./documents
$cd documents
$cd documents/.
$cd ./documents/.
83 of 180
84. Navigating the Linux file system: cd Command
(contd...)
In all of these examples, the dot represents the directory as-
sumed to be there. You can use it as a placeholder anywhere
you want to tell the shell that a directory goes in that place,
and to assume the appropriate value.
The parent directory of the current directory — in other words,
the directory one level up from the current directory, which
contains the directory we’re in now — is represented by two
dots (..).
So, If we were in the directory /home/username/documents,
and we execute the command:
$cd ..
we would be placed in the directory /home/username.
84 of 180
85. Navigating the Linux file system: cd Command
(contd...)
After we change directory, we can change back to the previous
working directory by representing it with a dash (-). When
we do this, the shell will automatically tell you the new directory
name.
Execute following commands:
$pwd
$cd Documents/financial
$pwd
$cd -
Use following commands to change directory to simlink and then
to actual physical link pointed by simlink:
$cd -L simlink_name
OR
$cd simlink_name
$cd -P simlink_name
85 of 180
86. Navigating the Linux file system: cd Command
(contd...)
The last command leads us to the physical location of folder
pointed by simlink_name.
86 of 180
87. Path: Absolute (full) Relative
Path may refer to following two things:
1. A path is a unique location to a file or a folder in a file system of an OS.
A path to a file is a combination of / and alpha-numeric characters.
2. Also refers to the environment variable which stores the directories the
OS searches to find executable when a command is given to it.
Here, we are interested in first kind of paths.
Absolute(full) path starts from the directory root (/) and goes
up to the actual object (desired file or directory). It contains the
names of all directories that come in the middle of the directory
root and the actual object. In this, name of the parent directory
is written in the left.
Consider the following figure:
87 of 180
89. Path: Absolute (full) Relative (contd...)
Have a look at following examples:
- Full path to user’s home folder: /home/usern
- Full path to folder institutes: /home/usern/institutes
- Full path to folder AMU: /home/usern/institutes/AMU
- Full path to folder IITD: /home/usern/institutes/IITs/IITD
- Full path to Document file:
/home/usern/institutes/IITs/IITB/Document
- Full path to Spreadsheet file:
/home/usern/institutes/IITs/IITR/Spreadsheet
- Full path to Presentation file:
/home/usern/institutes/AMU/MURS/Presentation
Relative path is defined as path to the actual object (desired
file or directory) relative to the present working directory in the
folder hierarchy tree.
89 of 180
90. Path: Absolute (full) Relative (contd...)
Provided that in all the cases mentioned below, present work-
ing directory is home directory (/home/usern) have a look at
following examples of defining the relative paths:
- Relative path to institutes folder: institutes
- Relative path to folder AMU: institutes/AMU
- Relative path to folder MALA: institutes/AMU/MALA
- Relative path to Document file: institutes/IITs/IITB/Document
- Relative path to Presentation file:
institutes/AMU/MURS/Presentation
Provided that in all the cases mentioned below, present working
directory is the directory IITK have a look at following examples
of defining the relative paths:
- Relative path to IITs folder: ..
- Relative path to folder institutes: ../..
- Relative path to folder AMU: ../../AMU
90 of 180
91. Path: Absolute (full) Relative (contd...)
- Relative path to folder IITD: ../IITD
- Relative path to Document file: ../IITB/Document
- Relative path to Spreadsheet file: ../IITR/Document
- Relative path to Presentation file: ../../AMU/MURS/Presentation
- Relative path to home folder: ../../..
(Note that ∼ is the full path to home folder).
Note that while defining absolute or full paths we only descend
the folder tree starting from the root directory to the desired
folder or file.
The separator used for intermediary folders while descending the
tree is forward slash (/).
When defining relative paths we may require both i.e. ascending
and descending the folder tree.
Descending is formulated in the same way as in the full paths.
91 of 180
92. Path: Absolute (full) Relative (contd...)
In ascending folders tree, each intermediary folder is denoted by
double dot (..) and the separator yet again is forward slash (/).
92 of 180
93. Removing Empty Folders: The rmdir Command
On Unix-like operating systems, the rmdir command removes
empty directories from a filesystem.
The rmdir command removes each directory specified on the
command line, if they are empty.
That is, each directory removed must contain no files or direc-
tories, or it cannot be removed by rmdir.
If any specified directory is not empty, rmdir will not remove
it, and will proceed to try and remove any other directories you
specified.
Directories are processed in the order you specify them on the
command line, left to right.
93 of 180
94. Removing Empty Folders: The rmdir Command
(contd...)
To remove both a parent directory and a subdirectory of that
parent, the subdirectory must be specified first, so the parent
directory is empty when rmdir tries to remove it.
rmdir is functionally equivalent to the command rm -d.
rmdir: Important Options
- rmdir supports few options. Following table describes them:
Option Description
-p Each directory argument is treated as a pathname of which all components will
be removed, if they are empty, starting with the last component. (See rm for
fully non-discriminant recursive removal.)
-v, - -verbose Display verbose information for every directory processed.
- -ignore-fail-on-
non-empty
Do not report a failure which occurs solely because a directory is non-empty.
Normally, when rmdir is instructed to remove a non-empty directory, it reports
an error. This option suppresses those error messages.
- -help Display a help message, and exit.
- -version Output version information, and exit.
94 of 180
95. Removing Empty Folders: The rmdir Command
(contd...)
rmdir: Examples
- The following command removes the folder fdir, if it is empty.
$rmdir fdir
- The following command removes the folders fdir1,f dir2, and
fdir3, if they are empty. If any are not empty, an error message
will be printed for that directory, and the others will be removed.
$rmdir fdir1 fdir2 fdir3
- The following command removes the directory mydir/mysubdir
if it’s empty. Then, removes directory mydir, if it’s empty after
mydir/mysubdir was removed.
$rmdir mydir/mysubdir mydir
95 of 180
96. Removing Empty Folders: The rmdir Command
(contd...)
- The following command is same as the above command. rmdir
attempts to remove mydir/mysubdir, then attempts to remove
dir.
$rmdir -p mydir/mysubdir
Note: If you want to remove a directory that is not empty
(and also remove everything it contains), you can use the rm
command with the -r (recursive) option.
96 of 180
97. Linux cp Command
On Unix-like operating systems, the cp command makes copies
of files and folders.
The general form of the command is:
$cp [Option...] source destination
Like many core Linux commands, if the cp command is success-
ful, by default, no output is displayed. To view output when
files are copied, use the -v (verbose) option.
By default, cp will overwrite files without asking. If the desti-
nation file name already exists, its data will be destroyed.
If you want to be prompted for confirmation before files are
overwritten, use the -i (interactive) option.
97 of 180
98. Linux cp Command (contd...)
cp: Important Options
- cp supports a large number of options. Following table describes
them:
Option Description
-a, - -archive Same as -dR - -preserve=ALL. When performing the copy, attempt to preserve
as much of the original file structure, attributes, and associated metadata as pos-
sible. This metadata includes security context data if you are running SELinux.
- -attributes-only Don’t copy the file data, only create a file with the same attributes. If the
destination file already exists, don’t alter its contents. You can control exactly
which attributes are copied with the - -preserve option.
- -
backup[=control]
Make a backup of each existing destination file that would otherwise be overwrit-
ten or removed. The control parameter specifies what version control method
to use; see version control for details.
As a special case, cp - -force - -backup will make a backup of source when source
and dest are the same, regular file.
-b Like - -backup, but does not accept a control argument; the default control
method is always used.
-d Copy symbolic links themselves, rather than the files they refer to, and preserve
hard links between source files in the copies. Same as - -no-dereference - -
preserve=links.
98 of 180
99. Linux cp Command (contd...)
Option Description
- -copy-contents When operating recursively, copy contents of special files, such as FIFOs and
devices found in /dev. You usually don’t want to use this option, because it
can have undesired results, such as hanging forever or filling up your entire disk.
However, this option is available for special, expert use cases.
-f, - -force If an existing destination file cannot be opened, remove it and try again. This
option has no effect if the -n/- -no-clobber option is used. However, it applies
independently of -i/–interactive; neither option cancels the effect of the other.
-i, - -interactive Prompt before overwrite (overrides a previous -n option).
-H Follow symlinks specified on the command line, but preserve discovered links.
If one of the arguments on the command line is a symbolic link, copy the
referenced file, not the link itself. However, if a symbolic link is discovered
during recursive traversal, it will be copied as a symlink, not a regular file.
-l, - -link Create hard links to files instead of copying them.
-L, - -dereference Always follow symbolic links in source; if source is a symlink, copy the file linked
to rather than the symlink itself. When this option is specified, cp cannot create
symlinks in the destination copies.
-n, - -no-clobber Do not overwrite an existing file. If -i/- -interactive was previously specified, this
option overrides it. This option cannot be specified with -b/- -backup, because
backups are only created when a file would have been overwritten.
-P, - -no-
dereference
Never follow symbolic links in source; copy symlinks as symlinks. Existing sym-
links encountered in the destination may still be followed, however.
-p Same as - -preserve=mode,ownership,timestamps.
99 of 180
100. Linux cp Command (contd...)
Option Description
- -
preserve=[attr_list]
Preserve the specified attributes, separated by a comma. Attributes are:
- mode: Preserve file mode bits (as set with chmod), and any ACLs.
- ownership: Preserve owner and group (as set with chown). Ability to
preserve these attributes is restricted in the same way as using chown.
- timestamps: Preserve time of last file access and modification (atime and
mtime, as set with touch), if possible.
- links: Preserve in the destination files any links between the source files.
With -L or -H, this option can potentially copy symbolic links as hard
links.
- context: Preserve SELinux security context of source files, or fail with
verbose diagnostics.
- xattr: Preserve extended attributes of source files, or fail with verbose
diagnostics.
- all: Preserve all of the above. Same as specifying all the above attributes
individually, with the exception that failing to copy context or xattr will
not give an exit status of failure.
If not specified, the default value of attr_list is mode,ownership,timestamps.
- -no-
preserve=attr_list
Don’t preserve the specified attributes.
- -parents Create missing parent directories in the destination, if necessary, when copy-
ing to target directory, according to the pathname specified in source.
For instance, cp –parents dir2/dir3/file dir1 creates the destination file
dir1/dir2/dir3/file, even if dir2 and dir3 do not presently exist under dir1.
100 of 180
101. Linux cp Command (contd...)
Option Description
-R, -r, –recursive Copy directories recursively.
-reflink=[when] Perform an optimized CoW (copy-on-write) clone, if the destination filesystem
supports it. The resulting copy will share the same bytes on disk as the original
file, until the copy is modified. Be aware that this means if the source bytes are
corrupted, the destination will share the corrupted data.
The when parameter defines what will happen if copy-on-write is not supported
by the destination filesystem. If when is always (the default), the copy will fail.
If auto, copying will proceed, using standard copy behavior.
- -remove-
destination
Remove each existing destination file before attempting to open it (contrast with
the –force option, which only removes the destination after a failed attempt to
open).
- -strip-trailing-
slashes
Remove any trailing slashes from each source argument.
-s, - -symbolic-
link
Make symbolic links instead of copying the files themselves. All source files
must be absolute pathnames starting with a slash, unless the destination files
are in the current directory.
-S, - -
suffix=suffix
Override the usual backup suffix.
-t, - -target-
directory=directory
Copy all source arguments into directory
-T, - -no-target-
directory
Treat destination as a normal file.
-u, - -update Copy only when the source file is newer than the destination file or when the
destination file is missing.
101 of 180
102. Linux cp Command (contd...)
Option Description
-v, - -verbose Verbose mode; explain what is being done.
- -sparse=when Control creation of sparse files. A sparse file contains holes, where a hole is
a sequence of zero bytes that occupies no physical disk space. When the file is
read, the holes are read as zeros. This can conserve disk space since many files
contain long sequences of zeros. By default, cp detects sparse files and creates
sparse destination files as well. The when parameter defines what cp should do
when a source file is detected to be sparse:
- auto: If the source is sparse, attempt to make the destination sparse. If
destination exists and is a non-regular file, do not attempt to make it
sparse. This is the default.
- always: For each sufficiently long sequence of zero bytes in the source,
attempt to make a sparse hole in the destination, even if the input file is
not sparse. This can be useful if the source filesystem does not support
sparse files; a sparse file can be appropriately created on the destination
filesystem.
- never: Never make the output file sparse. Some special files, such as a
swap file, must never be sparse. In these cases, this option should be used.
- -help Display a help message, and exit.
- -version Output version information, and exit.
102 of 180
103. Linux cp Command (contd...)
Option Description
-x, - -one-file-
system
Only operate on the filesystem where the command was executed. If cp tries to
cross the boundary to another filesystem, those files will be skipped. This in-
cludes networked drives, another partition — any file that resides on a filesystem
with a different mount point.
The directory representing the mount point itself will be copied, but not tra-
versed.
If -v is specified, you will see exactly which files have been skipped.
cp: Examples
- Let’s say you have a file named pic1.jpg in your working direc-
tory, and you want to make a copy of it called pic2.jpg. To do
that run following command:
$cp pic1.jpg pic2.jpg
- The source and destination files may also reside in different
directories. For instance following command,
103 of 180
104. Linux cp Command (contd...)
$cp /home/jahangir/pictures/pic1.jpg /home/jahangir/backup
OR
$cp ∼/pictures/pic1.jpg ∼/backup
...copies file pic1.jpg in pictures director under my home di-
rectory to the directory backup which is also under my home
directory.
- The file is copied with same name i.e. pic1.jpg. If you want
to copy it with a different name (say pic2.jpg )just specify that
name in destination as follows:
$cp /home/jahangir/pictures/pic1.jpg /home/jahangir/backup/pic2.jpg
OR
$cp ∼/pictures/pic1.jpg ∼/backup/pic2.jpg
104 of 180
105. Linux cp Command (contd...)
Note that if pic2.jpg already exists in the destination directory,
it will be overwritten without any warning. To have a warning
message displayed, use -i switch with cp as follows:
$cp -i /home/jahangir/pictures/pic1.jpg /home/jahangir/backup/pic2.jpg
OR
$cp -i ∼/pictures/pic1.jpg ∼/backup/pic2.jpg
- We can also specify multiple source files one after the other (or
can use wild card), and cp will expect that the final argument
is a directory name, and copy them all there. For instance,the
following command:
$cp ∼/pictures/pic1.jpg pic2.jpg pic3.jpg pic4.jpg ∼/backup
OR
$cp ∼/pictures/pic*.jpg ∼/backup
105 of 180
106. Linux cp Command (contd...)
- We can use cp to copy entire directory structures from one place
to another using the -R option. This is called recursive copy.
- Let’s say I have a directory, /home/jahangir/files, which con-
tains many files and subdirectories. I want to copy all those
files, and all the subdirectories (and the files and subdirectories
they contain), to a new location, /home/jahangir/myallfiles. I
can copy all of them using the following command:
$cp -R ∼/files ∼/myallfiles
Not that: When performing a recursive copy:
* If the directory myallfiles already exists, the directory files will be placed
inside.
* If myallfiles does not already exist, it will be created and the contents
of the files directory will be placed inside it.
106 of 180
107. Linux cp Command (contd...)
- Another useful trick is to use cp to create symbolic links to our
source files.
- We are already familiar with using the ln command to create
symlinks. cp is a great way to create multiple symlinks all at
once.
- cp creates symbolic links if we specify the -s option. So, for
instance, the following command:
$cp -s myfile.txt myfile1.txt
...creates a symbolic link, myfile1.txt, which points to myfile.txt.
- We can also create symbolic links from multiple source files,
specifying a directory as the destination.
107 of 180
108. Linux cp Command (contd...)
- Let’s say I have a set of files, file01.txt, file02.txt, etc. in the di-
rectory /home/jahangir/myfiles. I want to create symbolic links
to these files in the existing directory /home/jahangir/myfiles2.
The following command will do the trick:
$cp -s ∼/myfiles/file*.txt ∼/myfiles2
The directory myfiles2 will now contain symbolic links to the
file*.txt in the directory /home/jahangir/myfiles. The myfiles2
directory must already exist for the operation to succeed; if it
doesn’t exist, cp gives us an error message and nothing will be
copied.
Note:
* To create symbolic links in another directory, cp needs you to specify
the full pathname, including the full directory name, in your source file
name(s). Relative paths will not work.
108 of 180
109. Linux cp Command (contd...)
The -s switch also works with a recursive copy, as well. So the
following command:
$cp -R -s ∼/myfiles ∼/myfiles2
...will re-create the directory structure of /home/jahangir/my-
files, including any subdirectories and their contents; any files
will be created as symlinks to the originals, but the directories
will not be symbolic links, only regular directories.
If myfiles2 already exists, cp creates a directory inside it called
myfiles which contains the directory structure and symlinks; if
myfiles2 does not already exist, it will be created, and contain
the subdirectories and symlinks to the files that myfiles contains.
- Another fantastic option with cp command is - -backup. Have
a look at following command:
109 of 180
110. Linux cp Command (contd...)
$cp –backup origfile newfile
If newfile already exists, make a backup of the existing newfile
before overwriting it with a copy of origfile. By default, the
backup of newfile will be named newfile∼.
In the following command, if newfile already exists, it makes a
backup of the existing newfile before overwriting it with a copy
of origfile. The backup of newfile will be named newfile.∼1∼ if
no other backup exists, or newfile.∼2∼ if newfile.∼1∼ exists,
etc.
110 of 180
111. Linux mv Command
On Unix-like operating systems, the mv command moves and
renames files and directories.
Depending of its use it has following general forms.
Rename a file named source to destination:
$mv [Options] [-T] source destination
Move source file(s) to a directory named destination:
$mv [Options] source [source1...] destination
Same as the previous syntax, but specifying the directory first,
and the source file(s) last:
111 of 180
112. Linux mv Command (contd...)
$mv -t destination source [source1...]
mv: Important Options
- mv supports several options. Following table describes them:
Option Description
- -backup[=vcm] Make a backup of each existing destination file, using the version control method
vcm. If vcm is omitted, –backup behaves the same as -b (backups are created,
using the default version control method). See Backing up files for details.
-b Like - -backup, but does not accept a backup method. Instead, the method
specified by the VERSION_CONTROL environment variable is used. Simple
backups are created if the variable is not set. See version control methods for
details.
-f, - -force Always overwrite existing files without prompting. This can be useful if you
need to overwrite multiple files whose permissions are read-only; if you don’t
specify -f, you will be prompted for every file.
-i, - -interactive Prompt before overwriting an existing file, regardless of the file’s permissions.
-n, - -no-clobber Never overwrite any existing file.
112 of 180
113. Linux mv Command (contd...)
Option Description
- -strip-trailing-
slashes
Remove any trailing slashes from each source argument.
-S, - -
suffix=suffix
Specify the file name suffix to be used for all backup files. The default is ∼.
-t, - -target-
directory=destination
Move all sources into the directory destination.
-T, - -no-target-
directory
Treat destination as a normal file, not as a directory.
-u, - -update Don’t overwrite files if they’re newer. A move will only happen if the destination
file is older than the source file, or the destination file does not already exist.
-v, - -verbose Provide verbose output. Print the name of every file moved.
- -help Display a help message, and exit.
- -version Display version information, and exit.
Note:
If you specify more than one of the options -i, -f, or -n, only the
final option you specify takes effect.
mv: Examples
113 of 180
114. Linux mv Command (contd...)
- The following command moves the file myfile.txt into the di-
rectory myfiles. If myfiles is a file, it will be overwritten. If the
file is marked as read-only, but you own the file, you will be
prompted before overwriting it.
$mv myfile.txt myfiles
- The following command functions as follows. If myfiles is a
file or directory, and myfiles2 is a directory, move myfiles into
myfiles2. If myfiles2 does not exist, the file or directory myfiles
is renamed myfiles2.
$mv myfiles myfiles2
- The following command moves the file myfile.txt into the parent
directory of the current directory.
$mv myfile.txt ../
114 of 180
115. Linux mv Command (contd...)
- The following command moves the files myfile1 and myfile2 into
the directory myfiles.
$mv -t myfiles myfile1 myfile2
- The following command is same as previous command:
$mv myfile1 myfile2 myfiles
- WIth -n option, mv operates as follows. In following command
if file2 exists and is a directory, file is moved into it. If file2
does not exist, file is renamed file2. If file2 exists and is a file,
nothing happens.
$mv -n file file2
- WIth -f option, mv operates as follows. Same as above except
that if file2 exists and is a file, it will be overwritten.
115 of 180
116. Linux mv Command (contd...)
$mv -f file file2
- WIth -i option, mv operates as follows. Same as above except
that if file2 exists and is a file, it will be prompted to overwritten
or not.
$mv -i file file2
- The following command is same as mv -i. Prompt before over-
writing. The f option is ignored.
$mv -fi file file2
- The following command is same as mv -f. The i option is ig-
nored.
$mv -if file file2
116 of 180
117. Linux mv Command (contd...)
- The following command renames the file My file.txt to My
file 2.txt. Here, the spaces in the file name are escaped, pro-
tecting them from being interpreted as part of the command.
$mv Myfile.txt Myfile2.txt
- The following command is same as the previous command.
$mv My file.txt My file
2.txt
- Have a look at the following command:
$mv My file.txt myfiles
The above command operates in following manner:
* If myfiles a directory, My file.txt is moved into myfiles.
* If myfiles a file, My file.txt is renamed myfiles, and the original myfiles is overwritten.
* If myfiles does not exist, My file.txt is renamed myfiles.
- Now, consider the following command:
117 of 180
118. Linux mv Command (contd...)
$mv My*.txt myfiles
The above command operates in following manner:
* If myfiles is a directory, all files with the extension .txt, whose name begins with My, will be moved
into myfiles.
* If myfiles does not exist or is not a directory, mv reports an error and does nothing.
- Now, consider the following command:
$mv My file??.txt myfiles
The above command operates in following manner:
* If myfiles is a directory: any file with zero, one, or two characters between My file and .txt in their
name is moved into myfiles.
* If myfiles doesn’t exist, or is not a directory, mv reports an error and does nothing.
118 of 180
119. Backing up files: mv command revisits
If we use the -b or –backup options, mv will rename the desti-
nation file if it exists, appending a suffix to its file name.
This saves a copy of the original file instead of overwriting it.
There are two types of backups: simple and numbered:
1. Simple backups delete an existing backup file if it already exists. Only one backup
file is kept. The default suffix for simple backups is a tilde (∼). We can change
this suffix with the - -suffix option, or by setting the SIMPLE_BACKUP_SUFFIX
environment variable. For example, file.txt would be backed up as file.txt∼.
2. Numbered backups keep existing backup files, creating additional backups with an
incrementing number in the file name. No backup files are deleted. The suffix for
numbered backups is .∼n∼, where n is an integer. For example, file.txt would be
backed up as file.txt.∼1∼, then file.txt.∼2∼, etc.
Version control methods:
Additional rules for creating backup files are available, called
version control methods.
119 of 180
120. Backing up files: mv command revisits (contd...)
The version control method may be specified with the - -backup
option, or by setting the environment variable, VERSION_CONTROL
The methods are:
Method Name Description
none, off Never make backups, even if the - -backup
option is given.
numbered, t Make numbered backups.
existing, nil numbered if numbered backups already
exist, simple otherwise.
simple, never Always make simple backups.
mv: Backup Examples
120 of 180
121. Backing up files: mv command revisits (contd...)
- In following command, if file2 exists, it will be renamed to
file2∼.
$mv -b file1.txt file2
- In following command, if file2 exists, it will be renamed to
file2.bak.
$mv -b - -suffix=.bak file1.txt file2
- In following commands if file2 exists, it will be renamed file2.∼1 ∼.
If file2.∼1∼ exists, it will be renamed file2.∼2∼, etc.
$mv - -backup=numbered; mv file file2
- The following command is same as the previous command. The
environment variable is defined for this command only.
$VERSION_CONTROL=numbered
mv -b file file2
121 of 180
122. Backing up files: mv command revisits (contd...)
- By exporting the VERSION_CONTROL environment variable,
all mv -b commands for the current session will use numbered
backups.
$export VERSION_CONTROL=numbered; mv -b file file2
In following command even though the VERSION_CONTROL
variable is set, no backups are created because -b (or - -backup)
was not specified. If file2 exists, it is overwritten.
$export VERSION_CONTROL=numbered; mv file file2
122 of 180
123. File Ownerships Permissions
Linux is designed to support a large number of users.
Because of this, it needs to keep careful track of who is allowed
to access a file or directory (ownership), and how they can access
it (permissions).
Every file and directory on a Unix/Linux system is assigned three
types of owners.
Note: I might use the term file here but it is applicable to
directories as well.
1. User: A user is the owner of the file. By default, the person who
created a file becomes its owner. Hence, a user is also sometimes
called an owner.
123 of 180
124. File Ownerships Permissions (contd...)
2. Group: A user- group can contain multiple users. All users belonging
to a group will have the same access permissions to the file. Suppose
you have a project where a number of people require access to a file.
Instead of manually assigning permissions to each user, you could add
all users to a group, and assign group permission to file such that only
this group members and no one else can read or modify the files.
It saves time because instead of manually adding permission for each
user, you can simply add them to a group and change the permission
for the group.
3. Other: Any other user who has access to a file. This person has
neither created the file, nor he belongs to a usergroup who could own
the file. Practically, it means everybody else. Hence, when you set the
permission for others, it is also referred as set permissions for the world.
When a file is created, the creater (user) and his primary group
are its default owners.
124 of 180
125. File Ownerships Permissions (contd...)
Only root can change the owner of a file. The owner cannot
transfer ownership, unless the owner is root, or uses sudo to run
the command.
Now, the big question arises how does Linux distinguish between
these three user types so that a user ’A’ cannot affect a file which
contains some other user ’B’s’ vital information/data.
It is like you do not want your colleague, who works on your
Linux computer, to view your images. This is where permissions
come into play.
Every file and directory in Linux has the following three permis-
sions for all the three kinds of owners:
Permissions for file
1. Read (r): Can view or copy file contents.
125 of 180
126. File Ownerships Permissions (contd...)
2. Write (w): Can modify file contents.
3. Execute (e): Can run the file (if its executable).
Permissions for directories
1. Read (r): Can list all files and copy the files from directory.
2. Write (w): Can add or delete files into directory (needs execute permis-
sion as well).
3. Execute (e): Can enter the directory.
126 of 180
127. File Ownerships Permissions (contd...)
The owner (user) or the superuser can change the permissions
of files owned him.
127 of 180
128. More on File Ownerships Permissions
Now that you are aware of the basic terminology of
file permissions and ownership, it’s time to see it in
action.
Listing File Ownerships Permissions:
We can use the ‘stat command‘ or the ‘ls command’
to check the file permissions.
If we use the ls command with option -l on a file, we’ll
see an output like this:
-rw-rw-r– 1 jahangir jahangir 15 Nov 7 23:06 abc
Following is the pictorial explaination of output:
128 of 180
129. More on File Ownerships Permissions
(contd...)
Two things which require brief introduction are file type and
hard links count.
File Types in Linux:
When navigating the Linux file system we are sure to encounter
different file types.
129 of 180
130. More on File Ownerships Permissions
(contd...)
The most used and obvious file types are regular files and direc-
tories.
However, the Linux operating system has more to offer in terms
of file types as it also includes another 5 file types.
There is only one command we need to know, to identify and
categorize all the seven different file types found on the Linux
system.
The command is ls -ld file_name.
ls command will show the file type as an encoded symbol found
as the first character of the file permission part.
It is important to point out that Linux file types are not to be
mistaken with file extensions.
130 of 180
131. More on File Ownerships Permissions
(contd...)
Have a look at a short summary of all the seven file types:
Symbol Meaning Description
- regular file The regular file is a most common file type found on the Linux system.
It governs all different files such us text files, images, binary files, shared
libraries, etc.
d directory Directory is second most common file type found in Linux.
c character device
file
Character and block device files allow users and programs to communicate
with hardware peripheral devices. Most of them are present in /dev
b block device file Block devices are similar to character devices. They mostly govern hard-
ware as hard drives, memory, etc. The directory /dev contains most of
them
s local socket file Local domain sockets are used for communication between processes. Gen-
erally, they are used by services such as X windows, syslog and etc.
p named pipe Similarly as Local sockets, named pipes allow communication between two
local processes.
l symbolic link With symbolic links an administrator can assign a file or directory multiple
identities. Symbolic link can be though of as a pointer to an original file.
Hard Links:
131 of 180
132. More on File Ownerships Permissions
(contd...)
A hard link to a file points to the inode of the file instead of
pointing to the file itself.
This way the hard link gets all the attributes of the original file
and points to the same data block as the original file.
We shall discuss about them later on.
132 of 180
133. Changing File Permissions: chmod command
If you are the owner of a file/ directory or superuser, you can
change the permissions of the file/directory using chmod (change
the mode of access) command.
The chmod command:
On Unix-like operating systems, the chmod command sets the
permissions of files or directories.
The general form of chmod command is:
$chmod options permissions file_name
If no options are specified, chmod modifies the permissions of
the file specified by file name to the permissions specified by
permissions.
133 of 180
134. Changing File Permissions: chmod command
(contd...)
permissions define the permissions for the owner of the file (the
user), members of the group who owns the file (the group),
and anyone else (others).
There are two ways to represent these permissions:
- Symbolic mode: with symbols (alphanumeric characters).
- Absolute mode: with octal numbers (the digits 0 through 7).
Let’s say you are the owner of a file named xfile, and you want
to set its permissions so that:
1. The user can read, write, and execute it;
2. Members of your group can read and execute it;
3. and others may only read it.
Symbolic mode uses following characters to denote owners (user,
group and other):
134 of 180
135. Changing File Permissions: chmod command
(contd...)
Symbol Meaning
u user owner
g group owner
o other
a all (user + group + other)
The following command which demonstrates how to use chmod
in symbolic mode to set above stated permissions:
$chmod u=rwx,g=rx,o=r xfile
In symbolic mode we can also use mathematical operators to
perform the permission changes. Following operators are used:
Operator Meaning
+ For adding permissions
– For removing permissions
= For overriding existing permissions with new value
135 of 180
136. Changing File Permissions: chmod command
(contd...)
The following command demonstrates how to use chmod in
symbolic mode with operators to set above stated permissions:
$chmod u+rwx,g-x+rx,o-wx+r xfile
Following is the equivalent command if you use chmod in abso-
lute mode:
$chmod 754 xfile
Note:
Note that if you use the equals operator (=), any permissions
not specified will be specifically prohibited. For example, the
command
$chmod a= xfile
136 of 180
137. Changing File Permissions: chmod command
(contd...)
Will set the file permossion for all owners so that new files are
inaccessible to everyone.
chmod: Important Options
- Following options are available with chmod command:
Option Description
c, - -changes Like - -verbose, but gives verbose output only when a change is actually
made.
-f, - -silent, - -quiet Quiet mode; suppress most error messages.
-v, - -verbose Verbose mode; output a diagnostic message for every file processed.
- -no-preserve-root Do not treat ’/’ (the root directory) in any special way, which is the default
setting.
- -preserve-root Do not operate recursively on ’/’.
- -reference=RFILE Set permissions to match those of file RFILE, ignoring any specified MODE.
-R, - -recursive Change files and directories recursively.
137 of 180
138. Changing File Permissions: chmod command
(contd...)
- Options - -help and - -version have usual meaning with chmod.
chmod: Examples
- Set the permissions of xyz.txt to owner can read and write;
group can read only; others can read only:
$chmod 644 xyz.txt
- Recursively Change the permissions of the directory mydir, and
all folders and files it contains, to mode 755 (User can read,
write, and execute; group members and other users can read
and execute, but cannot write):
$chmod -R 755 mydir
138 of 180
139. Changing File Permissions: chmod command
(contd...)
- Change the permissions for the owner of myfile.jpg so that the
owner may read and write the file. Do not change the permis-
sions for the group, or for others.
$chmod u+rw myfile.jpg
- Set the permission of rfile.txt to read and write by everyone.
$chmod 666 rfile.txt
OR
$chmod a+rw rfile.txt
- The following command sets user and group permissions of filw
myfile1.txt to read, write and execute while the permissions
for others are set to read only.
$chmod ug+rwx,o=r myfile1.txt
139 of 180
140. System’s file mode creation mask
Means restricting the default files/ directories permissions as-
signed by the system when files/ directories are created.
The umask command
On Unix-like operating systems, the umask command returns or
sets, the value of the system’s file mode creation mask.
On Linux and other Unix-like operating systems, new files are
created with a default set of permissions.
Specifically, a new file’s permissions may be restricted in a spe-
cific way by applying a permissions mask called the umask.
The umask command is used to set this mask, or to show us its
current value.
The general form of umask command is:
140 of 180
141. System’s file mode creation mask (contd...)
$umask [-S] [mask]
Where:
- -S: Accepts a symbolic representation of a mask, or return one.
- mask: If a valid mask is specified, the umask is set to this value. If no
mask is specified, the current umask value is returned.
OS default file creation permissions umask
In Linux, the default permissions value is 666 for a regular file,
and 777 for a directory.
When creating a new file or directory, the kernel takes this de-
fault value, subtracts the umask value, and gives the new
files/ directories the resulting permissions.
141 of 180
142. System’s file mode creation mask (contd...)
So how does the umask actually work?
The umask masks (restricts) permissions by restricting them by
a certain value.
Essentially, each digit of the umask is subtracted from the
OS’s default value to arrive at the default value that we define.
It’s not really subtraction; technically, the mask is negated (its
bitwise compliment is taken) and this value is then applied to
the default permissions using a logical AND operation.
The result is that the umask tells the operating system which
permission bits to turn off when it creates a file.
Have a look at following (Mask in this figure means umask):
142 of 180
144. System’s file mode creation mask (contd...)
Identifying system’s current umask
To find the system’s current umask use the following command:
$umask
Above command returns system’s current umask in absolute
mode (as a four digit number). The first digit is a special
permission digit and can be ignored for our purposes.
With -S option the umask command returns system’s current
umask in symbolic mode:
$umask -S
Don’t get confused with the output of this command. If umask
is 002 its going to dispaly:
u=rwx, g=rwx, o=rx
144 of 180
145. System’s file mode creation mask (contd...)
This is nothing but the complemented value of mask. The
symbolic notations evaluate to 775, which is nothing but com-
plemented value of 002 in octal number system.
So symbolic mask is directly AND with default system permis-
sions. There is no need to take the complement as it is already
complemented.
Setting system’s umask
First of all note that any new setting of the umask will only be
applicable to the current running instance of the shell.
Creating a new instance of the shell will reset the umask to
default system value.
Like chmod umask values could also be set in two modes:
- Symbolic mode
145 of 180
146. System’s file mode creation mask (contd...)
- Absolute mode
The following command sets the mask so that when files are
created, they will have permissions which allow write permission
for the user (file owner). The rest of the file’s permissions would
be unchanged from the operating system default.
$umask u+w
Have a look at following command:
$umask u-x,g=r,o+w
The above command sets the mask so that when subsequent
files are created, they have permissions that:
- prohibits the execute permission from being set for the file’s owner
(user), while leaving the rest of the owner permissions unchanged;
146 of 180
147. System’s file mode creation mask (contd...)
- enable read permission for the group, while prohibiting write and execute
permission for the group;
- enable write permission for others, while leaving the rest of the other
permissions unchanged.
Note that if you use the equals operator (=), any permissions
not specified will be specifically prohibited. For example, the
following command:
$umask a=
sets the file creation mask so that new files are inaccessible to
everyone.
To set a umask of 022, use the following command:
$umask 022
147 of 180
148. System’s file mode creation mask (contd...)
The following command sets the mask so that new files will
be readable and writable by the user who owns the file, but
may not be executed; group members and others will have no
permissions to access the file:
$umask u=rw,go=
The following command makes new files inaccessible to everyone
- no one can read, write, or execute them.
$umask 777
The following command make new files completely accessible
(read, write, and execute) to absolutely everyone. However,
this is a bad idea. Don’t do this.
$umask 000
148 of 180