L2 Unix the File System
L2 Unix the File System
It is the most common file type. All programs you write belong to this type.
It is divided into two types:
Text file.
Binary file.
Text file:
Contains only printable characters.
All C and Java program sources, shell and perl scripts are text files.
A text files contains lines of characters where every line is terminated with the
newline character, also known as linefeed (LF).
When you press enter while inserting text, the LF character is appended to every line.
Binary file:
It contains both printable and unprintable characters that cover the entire ASCII range
(0 to 255).
Most UNIX commands are binary files, and the object code and executables that you
produce by compiling C programs are also binary files.
Picture, sound, and video files are binary files as well. Displaying such files with
simple cat command produces unreadable output and may even disturbs your
terminal’s settings.
Directory File:
A directory contains no data, but keeps some details of the files and sub-directories that it
contains.
The UNIX file system is organized with a number of directories and sub-directories, and
we can also create them as per our need.
A directory file contains an entry for every file and sub-directory that it houses. If you
have 20 files in a directory, there will be 20 entries in the directory. Each entry has two
components:
The filename.
A unique identification number for the file or directory (called the inode number).
NOTE: The name of a file can only be found in its directory; the file itself doesn’t contain its
own name or any of its attributes, like its size or access rights.
Device File:
Device filenames are generally found inside a single directory structure, /dev.
A device file is not really a stream of characters. In fact, it does not contain anything at
all.
Every file has some attributes that are not stored in the file but elsewhere on the disk. The
operation of a device is entirely governed by the attributes of its associated file.
The kernel identifies a device from its attributes and then uses them to operate the device.
We have used cd A command to change the directory to A. As the directory A is not
created before so we get the message: No such file or directory. For this, we have to first
create or make directory A using the command mkdir (Make Directory) and then we can
use cd command like this:
We can get back to the previous directory by simply writing the cd command like this:
To remove a directory, we can use rmdir (remove directory) command. After removing
the directory, if you again want to use that directory then the system will display the
message: “No such file or directory”. This is shown below:
To see the contents of any file use the cat command like this:
ls options:
ls -x (Output in Multiple Columns):
When you have several files, it’s better to display the file names in multiple columns.
Modern versions of ls do that by default, but if that doesn’t happen on your system, use
the -x option to produce a multicolumn output:
NOTE: All filenames beginning with a dot are displayed only when ls is used with the -a
option. The directory (.) represents the current directory and (..) signifies the parent
directory.
ls -R (Recursive Listing):
The -R option lists all files and sub-directories in a directory tree.
Similar to the DIR/S command in DOS, the ls -R command performs the traversal of the
directory tree recursively until there are no subdirectories left.
In the above snapshot we have used the ls -R command. The output shows first the
directory A, and the other 4 files that are present in the current directory (i.e. root) and
second it shows a file named as “File1.txt” under the directory A (I had already created
the file File1.txt inside the directory A. In the upcoming sections we will learn how to
create a file in UNIX).