0% found this document useful (0 votes)
4 views

L2 Unix the File System

Uploaded by

Irteka Parween
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

L2 Unix the File System

Uploaded by

Irteka Parween
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

The File:

 The file is a container for storing information.


 There are three categories of files in Unix:
o Ordinary file: Also known as regular file. It contains only data as a stream of
characters.
o Directory file: It is commonly said that a directory contains files and other
directories. It contains their names and a number associated with each name.
o Device file: All devices and peripherals are represented by files. To read or write
a device, you have to perform these operations on its associated file.

Ordinary (Regular) File:

 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.

The Parent-Child Relationship:


 The implicit feature of every UNIX file system is that there is a top, which serves as the
reference point for all the files.
 This top is called root and is represented by a / (frontslash).
 root is actually a directory. It consists of a number of sub-directories under it.

The HOME variable: The HOME directory:


 When you log on to the system, UNIX automatically places you in a directory called the
HOME directory.

pwd: Checking your current directory:


 You can move around from one directory to another, but at any point of time, you are
located in only one directory. This directory is known as your current directory.
 The pwd (print working directory) command tells you about your current directory.

cd: Changing the current directory:


 You can move around in the file system by using the CD (change directory) command.
When used with an argument, it changes the current directory to the directory specified as
argument.
 For example:


 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:

ls: Listing Files:


 The UNIX system has large number of files that control its functioning, and users also
create files on their own.
 These files are organized in separate folder called directories.
 We can list the names of the files available in this directory with the ls command.
 Directories often contain many files, and we may simply be interested to know whether a
particular file is available. In that case, just use ls with the filename like this:

 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:

ls -F (Identifying Directories and Executables):


 The output of ls that we have seen so far merely showed the filenames. We didn’t know
how many of them, if any, are directory files. To identify directories and executable files,
the -F option should be used like this:
 In the above screenshot, we have first used the ls -x to fetch the names of the files column
wise. After the execution of the ls -x command, we get the names of the 4 files which are
available in the current directory (i.e. root directory). Out of these 4 files, we have to find
out that which are the directories and which are the files. For this purpose, we used ls -F
command. As bench.py, hello.c, hello.js, and readme.txt are all files (i.e. there is no
directory located inside the root directory), therefore we didn’t get any directory symbol
on the console. Now we have created a directory, named as A, in the current directory
(i.e. in the root directory), using the mkdir command. After creating directory A, we
again used the ls -F command. This time we get the directory A along with the 4 files. To
identify a directory among the files, the system uses two things: colour, and / (slash) sign.
Note that the directory A is represented using colour (here Blue) and with a / sign.

ls -a (Showing Hidden Files Also):


 ls doesn’t normally show all files in a directory. There are certain hidden files (filenames
beginning with a dot), often found in the home directory that normally don’t show up in
the listing. The -a option (all) lists all hidden files as well:

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).

You might also like