Link, Cut, Tee, Sort, Clear, Diff, CMP, Uniq, Comm
Link, Cut, Tee, Sort, Clear, Diff, CMP, Uniq, Comm
- The ‘ln’ command in Linux is a powerful utility that allows you to create links
between files
- These links can either be hard links or soft (symbolic) links.
- These links behave differently when the source of the link (what is being linked
to) is moved or removed.
- Symbolic links are not updated (they merely contain a string which is the path
name of its target);
- Hard links always refer to the source, even if moved or removed.
- For example, if we have a file a.txt. If we create a hard link to the file and
then delete the file,
we can still access the file using hard link. But if we create a soft link of the
file and then delete the file,
we can’t access the file through soft link and soft link becomes dangling.
- Basically hard link increases reference count of a location while soft links work
as a shortcut (like in Windows)
- A soft link is similar to the file shortcut feature which is used in Windows
Operating systems.
- Each soft linked file contains a separate Inode value that points to the original
file.
- As similar to hard links, any changes to the data in either file is reflected in
the other.
Soft links can be linked across different file systems, although if the original
file is deleted or moved,
the soft linked file will not work correctly (called hanging link).
- ls -l command shows all links with first column value l? and the link points to
original file.
- Soft Link contains the path for original file and not the contents.
- Removing soft link doesn’t affect anything but removing original file, the link
becomes “dangling” link which points to nonexistent file.
- A soft link can link to a directory.
- If we change the name of the original file then all the soft links for that file
become dangling i.e. they are worthless now.
- Link across file systems: If you want to link files across the file systems, you
can only use symlinks/soft links.
Syntax
$ ln -s [original filename] [link name]
2. Hard Links
- Each hard linked file is assigned the same Inode value as the original, therefore
they reference the same physical file location.
- Hard links more flexible and remain linked even if the original or linked files
are moved throughout the file system.
- ls -l command shows all the links with the link column shows number of links.
- Links have actual file contents
- Removing any link, just reduces the link count, but doesn’t affect other links.
- Even if we change the filename of the original file then also the hard links
properly work.
- We cannot create a hard link for a directory to avoid recursive loops.
- If original file is removed then the link will still show the content of the
file.
- The size of any of the hard link file is same as the original file and
if we change the content in any of the hard links then size of all hard link files
are updated.
- The disadvantage of hard links is that it cannot be created for files on
different file systems
and it cannot be created for special files or directories.
Syntax
$ ln [original filename] [link name]
===================================================================================
=======================================================
2) cut
The cut command in linux is a command for cutting out the sections from each line
of files and writing the result to standard output.
It can be used to cut parts of a line by byte position, character, and field.
Syntax
cut [option] filename
===================================================================================
==========================================================
3) tee
-It reads the standard input and writes it to both the standard output and one or
more files.
- The command is named after the T-splitter used in plumbing.
- It basically breaks the output of a program so that it can be both displayed and
saved in a file.
- It does both the tasks simultaneously, copies the result into the specified files
or variables and also display the result.
The tee command is used to split the output of a program so that it can be both
displayed on the screen and saved to a file. For example:
ls -l | tee output.txt
options
-a
It basically do not overwrite the file but append to the given file.
Ex
we want to count number of lines in our file1.txt and also want to save the output
to new text file2.txt so to do both activities at same time,
we use tee command.
file1.txt
IBM
INFOSYS
MICROSOFT
file2.txt
ORACLE
ABB
PWD
$ wc -l file1.txt|tee -a file2.txt
output
3 file1.txt
3 file.txt
===================================================================================
=======================================================
4) sort
SORT command is used to sort a file, arranging the records in a particular order.
SORT command sorts the contents of a text file, line by line.
The sort command is a command-line utility for sorting lines of text files. It
supports sorting alphabetically, in reverse order, by number.
Syntax
sort filename
questions ->
1. Can the ‘sort’ command be used to sort files in descending order by default?
2. How can I sort a file based on a specific column using the ‘sort’ command?
3. How can I check if a file is already sorted using the ‘sort’ command?
4. Is it possible to sort a file with numeric data in reverse order using the
‘sort’ command?
===================================================================================
===============================================================
5) clear
==============================================================
6) diff
- In the world of Linux, managing and comparing files is a common task for system
administrators and developers alike.
- The ability to compare files line by line is crucial for identifying differences,
debugging code, and ensuring the integrity of data.
- One powerful tool that facilitates this process is the diff command.
Syntax
diff [OPTION]... FILE1 FILE2
Example
a.txt
Gujrat
Uttar Pradesh
Kolkata
Bihar
Jammu Kashmir
b.txt
Tamilnadu
Gujrat
Andhra Pradesh
Bihar
Uttar Pradesh
Output
0a1
> Tamilnadu
2,3c3
< Uttar Pradesh
< Kolkata
----
> Andhra Pradesh
5c5
< Jammu Kashmir
---
> Uttar Pradesh
Like in our case, 0a1 which means after lines 0(at the very beginning of file)
you have to add Tamilnadu to match the second file line number 1.
It then tells us what those lines are in each file preceded by the symbol:
Lines preceded by a < are lines from the first file.
Lines preceded by > are lines from the second file.
Next line contains 2,3c3 which means from line 2 to line 3 in the first file needs
to be changed to match line number 3 in the second file.
It then tells us those lines with the above symbols.
===============================================================
7) cmp
When working with Linux or UNIX systems, you may often need to compare files to
check for differences.
The ‘cmp’ command is a powerful tool that allows you to compare two files byte by
byte
- ‘cmp’ command in Linux/UNIX is used to compare the two files byte by byte and
helps you to find out whether the two files are identical or not.
- When ‘cmp’ is used for comparison between two files, it reports the location of
the first mismatch to the screen if a difference is
found and if no difference is found i.e. the files compared are identical.
- ‘cmp’ displays no message and simply returns the prompt if the files compared are
identical.
Syntax
cmp [OPTION]... FILE1 FILE2
example
$cmp file1.txt file2.txt
file1.txt file2.txt differ: byte 9, line 2
===============================================================
8) uniq
- The uniq command in Linux is a command-line utility that reports or filters out
the repeated lines in a file.
- In simple words, uniq is the tool that helps to detect the adjacent duplicate
lines and also deletes the duplicate lines.
- Uniq filters out the adjacent matching lines from the input file(that is required
as an argument) and writes the filtered data to the output file.
Syntax
uniq filename
options
=================================================================
9) comm
- 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:
Syntax
$comm [OPTION]... FILE1 FILE2
- For using comm command it requires the files should be in sorted order.
example
$ cat city1_sort
Bengaluru
Chennai
Mumbai
Pune
$ cat city2_sort
Baramati
Chennai
Nagpur
Nashik
Pune
Baramati
Bengaluru Chennai
Nagpur
Nashik
Mumbai
Pune
====================================================================