0% found this document useful (0 votes)
2 views16 pages

Usp Module 2

This document provides an overview of various Linux commands used for file management, including listing file attributes with 'ls', changing file permissions with 'chmod', and handling files using commands like 'cat', 'cp', 'mv', 'rm', and others. It explains both absolute and relative methods for changing permissions, along with examples of using each command. Additionally, it covers file compression and archiving utilities such as 'gzip', 'tar', and 'zip'.

Uploaded by

1dt25cs427
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Usp Module 2

This document provides an overview of various Linux commands used for file management, including listing file attributes with 'ls', changing file permissions with 'chmod', and handling files using commands like 'cat', 'cp', 'mv', 'rm', and others. It explains both absolute and relative methods for changing permissions, along with examples of using each command. Additionally, it covers file compression and archiving utilities such as 'gzip', 'tar', and 'zip'.

Uploaded by

1dt25cs427
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MODULE-2

1. Which command is used for listing file attributes? Explain significance of each field Or
Explain ls command with examples
The ls command with options:
 The ls command is to obtain a list of all file names in the current [Link] with
options:

Listing File Attributes(ls –l):


ls command is used to obtain a list of all filenames in the current directory. The output in UNIX
lingo is often referred to as the listing. Sometimes we combine this option with other options
for displaying other attributes, or ordering the list in a different sequence. It lists seven
attributes of all files in the current directory and they:
1. File type & Permission:The first column shows the type and permissions associated with
each file. The first character in this column is mostly a -, which indicates that file is an ordinary
one.
2. Links:The second column indicates the number of links associated with the file. This is
actually the number of filenames maintained by the system of that file.
3. Ownership: When you create a file, you automatically become its owner. The third column
shows kumar is the owner of all of these files. The owner has full authority to tamper with a
files contents and permissions—a privilege not available with others except the root user.
4. Group Ownership: The fourth column represents the group owner of the file. Every user is
attached to a group owner. Every member of that group can access the file depending on the
permission assigned.
5. File size: File size in bytes is displayed. It is the number of character in the file rather than the
actual size occupied on disk.
6. Last modification time: Last modification time is the next field. If you change only the
permissions or ownership of the file, the modification time remains unchanged. If at least one
character is added or removed from the file then this field will be updated.
7. File name: The last column displays the filenames arranged in ASCII collating sequence.
For Ex:
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir

2. What do you mean by Changing file permissions, Explain the Absolute and
relative permissions changing method? OR Explain the use of chmod to change
file permissions using Absolute and relative methods?
A file or a directory is created with a default set of permissions, which can be determined by
umask. Let us assume that the file permission for the created file is -rw-r-- r--. Using chmod
command, we can change the file permissions and allow the owner to execute his file.
The command can be used in two ways:
1. In a relative manner by specifying the changes to the current permissions.
2. In an absolute manner by specifying the final permissions.
Relative Permissions:
• chmod only changes the permissions specified in the command line and leaves the other
permissions unchanged.
• Its syntax is:
chmod category operation permission filename(s)
• chmod takes an expression as its argument which contains:
• user category (user, group, others).
• operation to be performed (assign or remove a permission).
• type of permission (read, write, execute).
Category : u – user g – group o – others a - all (ugo)
• operations : + assign - remove = absolute
• permissions: r – read w – write x - execute
Example:
• Initially,
-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart
$chmod u+x xstart
-rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart
• The command assigns (+) execute (x) permission to the user (u),
other permissions remain unchanged.
$chmod ugo+x xstart or chmod a+x xstart or chmod +x xstart
$ls –l xstart
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
• chmod accepts multiple file names in command line
$chmod u+x note note1 note3

Absolute Permissions:
• Here, we need not to know the current file permissions. We can set all nine permissions
explicitly. A string of three octal digits is used as an expression. The permission can be
represented by one octal digit for each category. For each category, we add octal digits. If we
represent the permissions of each category by one octal digit, this is how the permission can be
represented:
Read permission – 4 (octal 100)
Write permission – 2 (octal 010)
Execute permission – 1 (octal 001)
Directory Permissions:
• It is possible that a file cannot be accessed even though it has read permission, and can be
removed even when it is write protected. The default permissions of a directory are,
rwxr-xr-x (755)
• A directory must never be writable by group and others .
• Example:
$mkdir c_progs
$ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs
• If a directory has write permission for group and others also, be assured that every user can
remove every file in the directory.

Handling Ordinary Files(Basic commands)


1) Cat command:
The cat (concatenate) command in Linux is used to view, create, and combine file contents
directly from the terminal. It allows users to quickly work with file content without opening a
text editor. Primarily used to display the contents of files on the terminal.
Syntax:
cat file_name
Example: cat [Link].
View the Content of Multiple Files in Linux
The cat command can display the content of more than one file together. The output of the first
file appears first, followed by the second file.
Syntax:
cat file_name1 file_name2
Example: If we have two files , file1 and file2.
cat file1 file2

Examples of cat Command Using Options:


1. View File Content with Line Numbers using option -n
The `-n` option is used to display the content of a file along with line numbers. It
helps in identifying and referring to specific lines in a file.
Syntax:
cat -n file_name
Example:
cat -n file2
2. Suppress Repeated Empty Lines using option -s
The -s option is used to remove multiple consecutive empty lines from the output.
It helps in producing a cleaner and more readable file display.
Syntax:
cat -s file_name
Example:
cat -s [Link].
2) CP command:
The cp (copy) command in Linux is used to duplicate files or directories from one
location to another within the file system. It supports copying single files, multiple
files, and entire directories, with options to control overwriting and attribute
preservation.
 Copy data from one file to another
 Copy multiple files into a directory
 Recursively copy directories and subdirectories
 Overwrite existing files by default.
1. Copying Between Two Files in Linux: When the cp command is provided
with two file names, it copies the contents of the source file to the
destination file.
 If the destination file does not exist, it is created.
 If the destination file already exists, it is overwritten without warning.
Example 1: Copy to a New File
Create a new file by copying the contents of an existing file.
cp [Link] [Link]
 [Link] exists in the directory
 [Link] does not exist, so it is created
 Contents of [Link] are copied into [Link]
3) Mv command:
The mv (move) command in Linux is used to move or rename files and directories.
It does not create a copy of the file; instead, it changes its location or name. By
default, if a file with the same name exists at the destination, mv overwrites it.
 Used to move files/directories from one location to another.
 Can also be used to rename files or directories.
 Overwrites existing files without warning unless used with options.
 Useful for organizing files and changing file names quickly.
Syntax:
mv [options] source target
Where −
 [options] are optional flags that modify the behavior of the command.
 source is the file or directory you want to move or rename.
 target is the destination path or new name.
4) rm command:
The rm command in Linux is used to delete files and directories permanently from
the file system. It removes data immediately without sending it to any recycle bin,
so deleted files cannot be recovered.
 rm deletes files permanently from the system.
 It does not move deleted data to the Trash/Recycle Bin.
 By default, rm does not delete directories.
Syntax:
rm [options] [file_name or directory_name]
5) more command:
The more command is used to display the contents of a file one screen at a time.
It allows you to scroll through the file using various keyboard commands. It
provides several options for customizing its behavior and can be used in
combination with other commands to view various types of output.
Syntax of more Command
more [options] file
6) wc command:
The wc command in Linux is used to count lines, words, characters, and bytes in a
file or from input you provide. Whether you're analyzing logs, reading large
datasets, or just checking the size of your scripts, wc gives a quick summary. It can
take one or more files as input or read directly from user input if no file is
mentioned.
 Line count – number of lines (each line ends with a newline character).
 Word count – total number of words (words are separated by spaces, tabs,
or newlines).
 Byte count – total number of bytes (which may differ from characters in
some cases).
 Filename – name of the file that was processed.

 Let us consider two files having name [Link] and [Link]


containing 5 names of the Indian states and capitals
respectively.
$ cat [Link]
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

$ cat [Link]
Hyderabad
Itanagar
Dispur
Patna
Raipur

Example 1: Passing only one file name in the argument.


$ wc [Link]
Output
5 7 58 [Link]
here,
 5: Number of Lines
 7: Number of Words
 58: Number of bytes

Example 2: Passing more than one file name in the


argument.
$ wc [Link] [Link]
5 7 58 [Link]
5 5 39 [Link]
10 12 97 total
7) Od command:
The od (octal dump) command in Linux is a versatile tool used to display file
contents in various formats, with the default being octal. This command is
particularly useful for debugging scripts, examining binary files, or visualizing non-
human-readable data like executable code. It allows users to view data in multiple
formats, including octal, hexadecimal, decimal, and ASCII, providing a detailed
look into the file's contents.
Syntax:
od [OPTION]... [FILE]..
Common Options of od command:

1. -b Option: Display in Octal Format


It displays the contents of input in octal format.
Syntax:
$ od -b [Link]
2. -c Option: Display in Character Format
It displays the contents of input in character format.
Syntax:
$ od -c [Link]

8) Cmp command:
The cmp command in Linux/UNIX is a simple and powerful utility used to compare
two files byte by byte. It helps determine whether files are identical and reports
the exact position of the first difference when they are not.
 Compares files byte by byte
 Reports first mismatched byte and line
 Produces no output if files are identical

Example 1: Compare Two Files

This example compares two text files to identify exactly where the
content starts to differ. It is useful when you want to check small
changes between files, such as spelling mistakes or modified
characters.
cmp [Link] [Link]
Example 2: Compare Identical Files
If both files are identical, cmp produces no output.
cmp [Link] [Link]

9) Comm command:
The 'comm' command in Linux is a powerful utility that allows you to compare
two sorted files line by line, identifying the lines that are unique to each file and
those that are common to both. This command is particularly useful when you
have lists, logs, or data sets that need to be compared efficiently.
The 'comm' command is used for line-by-line comparison of two sorted files. It
reads two files as input and generates a three-column output by default:
 Column 1: Lines unique to the first file.
 Column 2: Lines unique to the second file.
 Column 3: Lines common to both files.
Syntax:
$comm [OPTION]... FILE1 FILE2
 'FILE1' and 'FILE2': The sorted files to compare.
 '[OPTION]': Flags to modify the command’s output.

Example of the 'comm' Command


Let us suppose there are two sorted files [Link] and [Link] and now we will use comm
command to compare these two.
Displaying contents of file1
$cat [Link]
Apaar
Ayush Rajput
Deepak
Hemant
Displaying contents of file2
$cat [Link]
Apaar
Hemant
Lucky
Pranjal Thakral
Now, run comm command as:
$comm [Link] [Link]
Output:
Apaar
Ayush Rajput
Deepak
Hemant
Lucky
Pranjal Thakral
Explanation:
Column 1: Lines unique to '[Link]' ('Ayush Rajput', 'Deepak').
Column 2: Lines unique to '[Link]' ('Lucky', 'Pranjal Thakral').
Column 3: Lines common to both files ('Apaar', 'Hemant').
Key Options for the 'comm' command
1. '-1' (Suppress First Column)
Suppresses lines that are unique to the first file, displaying only lines from the
second file and common lines.
2. '-2' (Suppress Second Column)
Suppresses lines that are unique to the second file, displaying only lines from the
first file and common lines.
3. '-3' (Suppress Third Column)
Suppresses lines that are common to both files.
Compressing and archiving Files.
1)Gzip and Gunzip:
Gzip is a compression utility tool used in Linux and other Unix-based systems. It's
used to compress and decompress files, reducing their size for storage and
transfer. Gzip works by replacing repeated strings of data in a file with a shorter
representation. When the file is decompressed, the original data is reconstructed
from the shorter representation.
Syntax:
gzip [Link]
This command will compress the file named [Link] and create a new file
named [Link]. The original file will be deleted, and the compressed file will be
saved in its place.
To decompress a file that has been compressed with gzip, you can use the
following command .
Syntax:
gunzip [Link]
This command will decompress the file [Link] and create a new file
named [Link]. The compressed file will be deleted, and the decompressed file will
be saved in its place.
Common Options

Option Description Example

Write output to stdout, keep original gzip -c [Link] >


-c
file [Link]

-d Decompress (same as gunzip) gzip -d [Link]


Option Description Example

-f Force compression/decompression gzip -f [Link]

-k Keep original file during compression gzip -k [Link]

-r Compress files recursively in directory gzip -r directory/

Verbose output (show compression


-v gzip -v [Link]
ratio)

2) tar:
The tar command in Linux (short for Tape Archive) is a powerful tool used to
create, view, extract, and manage archive files. It allows you to bundle multiple
files and directories into a single archive while preserving permissions and
directory structure.
 Used to create backups and archive files.
 Supports compression using gzip (-z), bzip2 (-j), or xz (-J).
 Preserves file permissions and structure.

Example 1: Create a simple tar archive

Command:
tar -cvf [Link]
3) zip and unzip:
The zip command compresses one or more files or directories into a single .zip
archive file. This helps save disk space, keeps data well organized, and makes it
easy to share or back up files. It is widely used for compressing large files before
sending them via email or uploading them to cloud storage.
Example: Compress Multiple Files
To compresses multiple files into a single ZIP archive.
zip [Link] file1 file2 file3
 zip: Command used to create a ZIP archive
 [Link] : Name of the output ZIP file (archive to be created)
 file1 file2 file3: List of files to compress and include inside the archive.
The unzip command in Linux is used to extract files and directories from ZIP
archive files. It allows users to view, test, and decompress ZIP files directly
from the terminal.
 Used to extract the contents of .zip files.
 Supports listing and testing ZIP archives without extracting them.
 Provides options to control extraction location and overwrite behavior.
Suppose you have a file named '[Link]' that you want to extract which
contains two files inside i as "[Link] and [Link]". We need to unzip it in the
current directory.
Command:
unzip [Link]
Syntax:
unzip [options] [Link]
 unzip: The command used to extract files from a ZIP archive.
 [options]: Optional flags to control behavior, such as -l to list contents or -d
to specify the extraction directory.
 [Link]: The ZIP file to be extracted, replaced with the actual archive
name.
Hard Link and Symbolic Link:
A Hard link acts as a copy (mirrored) of the selected file. It accesses the data
available in the original file. If the earlier selected file is deleted, the hard link to
the file will still contain the data of that file.
Advantages of Hard Link
 It makes efficient use of disc space by avoiding the unnecessary creation of
record blocks.
 There is no risk of link breaking as a result of the removal of the actual
file(as long as one hard hyperlink survives, the data will persist).
 The speed of Hard Links is fast.
A soft link (also known as a Symbolic link) acts as a pointer or a reference to the
file name. It does not access the data available in the original file. If the earlier file
is deleted, the soft link will be pointing to a file that does not exist anymore.
Advantages of Soft Link
 Versatility in linking files across different localities and document systems.
 Can link directories.
Disadvantages of Soft Link
 Slightly slower access than hard links.
 Deleting or moving the original file will cause soft links to fail.

Comparison
Parameters Hard link Soft link

Inode Files that are hard linked take Files that are soft linked take
number* the same inode number. a different inode number.

Hard links are not allowed for


Soft links can be used for
Directories directories. (Only a superuser*
linking directories.
can do it)

It cannot be used across file It can be used across file


File system
systems. systems.
Comparison
Parameters Hard link Soft link

Data present in the original file Soft links only point to the
Data will still be available in the hard file name, it does not retain
links. data of the file.

If the original file is removed, If the original file is


Original file's the link will still work as it removed, the link will not
deletion accesses the data the original work as it doesn't access the
was having access to. original file's data.

Hard links are comparatively Soft links are comparatively


Speed
faster. slower.

You might also like