BCA1 Linux
BCA1 Linux
BCA1 Linux
The file:
* UNIX file doesn‟t contain the eof (end – of - file) mark. A file‟s size is not stored in
the file nor even its name.
* All file attributes are kept in a separate area of the hard disk, not directly accessible to
humans, but only to the kernel.
* UNIX treats directories and devices as file as well. A directory is simply a folder
where you store filenames and other directories.
* All physical devices like the hard disk, memory, CD-ROM, printer and modem are
treated as files.
1) Ordinary file: Also known as regular file it contain only data as a steam of
characters.
2) Directory file: It‟s commonly said that a directory contains file and other directories,
but it contains their names and a number associated with each name.
3) Device file: To read or write a device, you have to perform these operations
on its associated file.
1. Text file
2. Binary file
A text file contains only printable characters, and you can often view the contents and
make sense out of them. All C and JAVA program sources, shell and Perl scripts are
text files. A text file contains lines of characters where every line is terminated with the
new line character, also known as linefeed (LF).
A binary file one the other hand 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 & executable that you produce by compiling C programs are also binary
files. Picture, sound and video files are binary files. Displaying such files with a simple
cat command produces unreadable output & may even disturb your terminals setting.
2. Directory file:
A directory contains no data, but keeps some details of the files & subdirectories that it
contains. A directory file contains an entry for every file and subdirectory that it houses.
If you have 20 files in a directory, there will be 20 entries in the directory. Each entry
has components:
A directory contains the file name and no the files contents. You can‟t write a directory
file, but you can perform some action that makes the kernel write a directory. When you
create or remove a file, the kernel automatically updates its corresponding directory by
adding or removing the entry associated with the file.
3. Device file:
Printing files, installing software from CD- ROM or backing up files to tape. All of
these activities are performed by reading or writing the file representing the device to
treat devices as file as some of the commands used to access an ordinary file also work
with device files.
Device filenames are generally found inside a single directory structure, 1 dev, its not
really stream of characters. In fact, it doesn‟t contain anything at all. 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 use them to operate the device.
A filename can consist of up to 255 characters, though this figure is normally not
reached. Files may or may not have extensions, & can consist of practically any ASCII
character expect the / & the NULL character. You are permitted to use control
characters or other unprintable characters in a filename.
All files in UNIX are "related" to one another. The file system in UNIX is a collection
of these related files organized in a hierarchical structure. This system has also been
adapted by DOS and Windows.
The implicit feature of every UNIX file system is that there is a top, which serves
as the reference point for all files. This top is called root and is represented by a /(front
slash). Root is actually a directory.
The root directory (/) has a number of subdirectories under it. These
subdirectories in turn have more subdirectories & other files under them. In these
parent-child relationships, the parent is always a directory home and kumar are both
directories as they are both parents of at least one file or directory.
* one group – contains the files that are made available during system installation.
/bin and / user / bin – Company used UNIX commands are found PATH variable
always shows these directories in its list.
/ sbin and / user / sbin – if there‟s a command that you can‟t execute but the system
administrator can then it would probably be in one of these directories.
/ etc – contain the configuration file of the system you can change a very important
aspect of system functioning by editing a text file in this directory.
/ dev – This directory contain al device files pfs, dsk&rdsk in this directory device
files are stored.
/ lib and / user / lib – contain all library file in binary form you will need to link
your C programs with file in the directories.
/ user / share / man – This is where the man page are stored. These are separate
subdirectories that contain the pages for each section. These subdirectories may have
different names on your system.
Second group:
Content of these directories would change as more software and utilities are added to
the system. Users also work with their own files they write programs, send and receive
mail and also create temporary files.
/ tmp – the directories where users are allowed to create temporary files. These
files are wiped away regularly by the system.
/ var – The variable part of the file system contains all your print jobs and your
outgoing and incoming progs.
Home directories in / home / Kumar. Your system may use a different location
for home directories.
When you log on to the system, UNIX automatically places you in a directory
called the home directory. It is created by the system when a user account is apend. If
you log in using the login name kumar, you will land up in a directory that could have
the pathname /home/kumar.
$ echo $HOME
Home Variable:
It‟s often convenient to refer to a file Foo located in your home directory as
$HOME/foo . Most shells also use the ~ symbol for this purpose. So, $HOME/foo is the
same as ~/foo in these shells.
If user Sharma has the file foo in his home directory, then Kumar can access it as ~
Sharma/foo. A tilde followed by / (like ~/foo) refers to one‟s own home directory, but
when followed by a string (~Sharma) refers to the home directory of that users
represented by the string.
A user is placed in a specific directory of the file system. 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.
Syntax: Pwd(option)
$ pwd
/home/kumar
Pwd displays the absolute, as you navigate the file s/m with the cd command, you‟ll be
using pwd to know your current directory.
1. You can move around in the file system by using the cd command.
$ pwd
/home / kumar
$ cdprogs
$ pwd
Progs must be in current directory. Though pwd displays the absolute path name.
Cd doesn‟t need to use one. When you need to switch to the /bin directory where most
of the commonly used UNIX commands are kept, you should use the absolute
pathname:
$ pwd
/bin
$ pwd
$ cd
$ pwd
By Smt.Kalpana C Dalwai,Assistant Professor, Karnatak Science College Dharwad Page 7
Introduction to UNIX
/ home / kumar
Ex: $ pwd
/home/kumar
pwd displays the absolute pathname. As you navigate the file system with the cd
command, you will be using pwd to know your current directory.You can force an
immediate return to your home directory by simply using cd:
$ cd /home/ sharma
$ pwd
/ home / sharma
$ pwd
/ home/ kumar
Directories are created with the mkdir command. The command is followed by names
of the directories to be created.
Ex: $ mkdirMydir
$ mkdirkcD.
$mkdirMydirkcDpictures.
$ mkdiretc |kcd
mkdir : cannot create directory „etc|kcd‟ : permission denied. User does not have
permission to create directory in parent directory.
$mkdir test
* The permissions set for the current directory don‟t permit the creation of files &
directories by the user.
Options:
rmdir can also delete more than one directory in one shot.
$rmKcdKscd,
If you want to remove a directory that is not empty you can use „rm‟ command with –r
option.
Ex: $ rm –r
Absolute pathnames:
Absolute path is defined as the specifying the location of a file or directory from the
root directory.
When you have more than one / in pathname, for each /, you have a descend one level in
the file system.
Thus Kumar is, one level below home, and two levels below root .no two file in a UNIX
system can have identical absolute pathname. You can have two files with the same
name, but in different directories, their pathname will also be different.
Ex:
/ home / kumar / prage / c2f.pl can cd-exist with file
/ home / kuamr / safe / C2f.pl
When you specify the date command, the system has to locate the file date from a list of
directories specified in the PATH variable, and then execute it.
$ / bin / date
Relative pathname:
Relative pathname is defined as path related to the present working directory. It is starts
with your current directory, never begin with a /.UNIX offers a shortcut the relative
pathname that uses the current or parent directory as reference and specifies the path
relative to it. A relative pathname uses one of these cryptic symbols.
$ pwd
$ cd ..
$ pwd
The command cd.. Translate to this: changing your directory to the parent of the current
directory you can combine any number of such sets of ..separated by /.
$ cd /
$ pwd
$ pwd $ pwd
$ pwd $ pwd
Ex:
$lsperl
ls also be used with multiple filenames, and has options that list most of the file
attributes.
Option :
-R Recursive listing
Ex: $ls –x
$ lc –Fx
$ lc –aXF
Use of two symbols * and / as type indicators. The * indicates that the file contains
executable code and the / refers to a directory.
1. Cat : It is mainly used to display the content of a small file on the terminal.
Syntax:
cat options [files…..]
Ex: 1) $ cat sham.txt
Options:
Cat normally used for displaying text file only. If you have nonprinting ASCII
characters in your input. You can use cat with the –v option to display these characters.
2) –n :Numbering Lines
The C compilers indicate the line number where error are detected and this numbering
facility often helps a programmer in debugging programs, but then your vi editor can
show line number too, and if your version of cat doesn‟t support –n, you can use the „pr‟
command to do the same job.
1) Redirect output cat command to file rather than standard output. The file will be
created if it doesn‟t exist and will be overwritten if it does exist.
2) Append the output of cat command to end of existing file. The destination file
will be created if it doesn‟t exist.
Main()
$ cat> BCA
BCA VI sem student are not wearing uniform regularly today onwards they will pay
20rs per day.
Main()
Cat is a versatile command. It can be used to create, display concatenate and append to
file. More importantly it doesn‟t restrict itself to handling file only, it also act on a
stream. You can supply input to cat not only by specifying a filename, but also from the
output of another command.
The cp command copies a file. It creates an exact image of the file on disk with a
different name. syntax requires at least two filename.
If destination file file2 doesn‟t exist, it will first be created before copying takes place.
If not it will simply overwritten without any warning from the system. So be careful
when you choose your destination filename. Just check with the ls command wither or
the file exist.
Only one file to be copied. The destination can be either file or directory there is 2 way
to copy
CP can be used to copy more than one file with invocation of command but last name
must be directory
Or
$ cp chapo1 progs
Options:
1) –i : interactive copying.
The –i option warns the user before overwriting the destination file.
A file once delete cant be recovered.rm wont normally remove a directory. But it can
remove files from one rm chap01 could be dangerous to use.
You may sometimes need to delete all files in directory as part of a cleanup operation.
The * represents all files.
$-
Options:
1) –i : interactive deletion
In cp the –i option make the command ask the user for confirmation before removing
each file.
A Y removes the file any other response leave the file undeleted
2) –R : or –r recursive deletion
With the –r or –R option rm perform free traversal for all subdirectories & files within
subdirectories. At each stage it deletes.
3) –f : forcing removal
rm prompts for removal if a file is write protected. The –f option overrides this minor
protection & force removal when we combine it with the –r option it could be the most
ristly thing to do
$ rm –rf *
mv :renaming files:
it renames a file.
Moves file or directories from one place to another move single, multiple files &
directories.
mv doesn‟t create a copy of the file. It renames it. No additional space is consumed on
disk.
Options :
1) –num: type the number of lines that you want to display
2) –d : to help user to navigate, it display
“[ press „space‟ to continue ,‟9‟ to quit]” and display.
“[press „h‟ for instructions]” when wrong key is procced
Ex: $ more –d kcd.txt
No user allowed direct access to the printer. Instead, one has to spool (line up ) a
job along with others in a print queue. Spooling ensures the orderly printing of jobs
and relives the user from of necessary of administering the print resources
Example $lp kcd.txt
Request id is prl-320 (1 file)
Several users can print their files in this way without conflict. lp notifies the
request id .
Options:
-d :reports the current default printer
A regular file may contain plain text, a C program or executable code. UNIX provides
the file command to determine the type of file, especially of an ordinary file. you can
use it with one or more filenames as arguments.
$ file archive.zip
File correctly identifies the basic file types. Using the * to signify all files.
$ files *
UNIX features a universal word. Counting program that also counts line & characters. It
takes one or more filenames as arguments & displays a four columnar output. Before
you use WC on the file infile, just use cat to view its cotents:
S cat infile
I am the WC command
With options
You can now use WC without options to make a “word count” of the data in the file:
$ WC infile
3 20 103 infile.
We count 3 lines,20words
& 103 characters. The file name has also been shown in the fourth column.
Option:
-l : no of lines display.
A line is ony group of character not containing a new line.
Ex : $we -l infile
3 infile –no of lines
-w : no of words display
A word is agroup of characters not containing space, tab or newline.
Executable files contain nonprinting characters, and most UNIX commands don‟t
display them properly.
Options:
$ od –b odfile
Each line displays 16 bytes of data in octal, proceeded by the offset in the file of the
first byte in the line. When the –b and –c options are combined the output is
$ od –bcodfile
T H e - 007 -c h a
r a c t e f -
r I n 9 5 - b
E | | /n
You may often need to know whether two file are identical so one of them can be
deleted. Cmp (compare) command. Obviously it needs two filenames.
Two files are compared byte by byte and the location of the first mismatch (in
the ninth character of the first line) is echoed to the screen.
If two file are identical, cmp displays no message, but simply returns the prompt.
$_
It requires the both files are sorted & have some differences. When you run comm, it
displays a three columnar output.
Anil Ayush
Pranav Pranav
Vaidik Vaidik
Veeraj
Both file are sorted and have some differences. When you run comm, it displays a
three columns output.
1 8
2 3
3 2
1 2
8 3
Options:
-3 : common.
Options:
Example:
file1.txt: 1 :this2
File.txt: 2 : file
File2.txt:3 : ha2
File2.txt:6 :aree
File2.txt:19 :also
0r
Comp and comm tells us which lines in me file have to be changed to make
the two files identical.
0 Gujrath Tamilnadu
1 uttarpradesh Gujrath
2 Kolkata Andrapradesh
3 Bihar Bihar
4 Jammu Uttarpradesh
0a1
>Tamilnadu
2.3c3
<uttarpradesh
Andrapradesh
5c5
Uttarpradesh
The first line of different o/p contain
line numbers corresponding to the first file
A special symbol
0a1 – after line 0 add Tamilnadu to match the second file line number 1.
2,3c3 – from line no 2 to 3 in first file needs to be changed to match line no3 in second
file.