Sie sind auf Seite 1von 9

UNIX - FILE MANAGEMENT

http://www.tutorialspoint.com/unix/unix-file-manag ement.htm Copyright tutorialspoint.com


All data in UNI is org ani!ed into files. All files are org ani!ed into directories. "hese directories are org
ani!ed into a tree-li#e structure called the filesystem.
$hen you wor# with UNI% one way or another you spend most of your time wor#ing with files. "his tutorial
would teach you how to create and remo&e files% copy and rename them% create lin#s to them etc.
In UNI there are three 'asic types of files:
1. Ordinary Files: An ordinary file is a file on the system that contains data% text% or prog ram
instructions. In this tutorial% you loo# at wor#ing with ordinary files.
2. Directories: (irectories store 'oth special and ordinary files. )or users familiar with $indows or
*ac +,% UNI directories are e-ui&alent to folders.
3. Special Files: ,ome special files pro&ide access to hardware such as hard dri&es% C(-.+* dri&es%
modems% and /thernet adapters. +ther special files are similar to aliases or shortcuts and ena'le
you to access a sing le file using different names.
Listing Files:
"o list the files and directories stored in the current directory. Use the following command:
$ls
0ere is the sample output of the a'o&e command:
$ls
bin hosts lib res.03
ch07 hw1 pub test_results
ch07.bak hw2 res.01 users
docs hw3 res.02 work
"he command ls supports the - option which would help you to g et more information a'out the listed files:
$ls -l
total 1962188
drwxrwxr-x 2 arood arood !096 "ec 2# 09$#9 ul
-rw-rw-r-- 1 arood arood #3!1 "ec 2# 08$38 ul.%p&
drwxr-xr-x 2 arood arood !096 'eb 1# 2006 uni(
drwxr-xr-x 2 root root !096 "ec 9 2007 urlspedia
-rw-r--r-- 1 root root 276!80 "ec 9 2007 urlspedia.tar
drwxr-xr-x 8 root root !096 )o( 2# 2007 usr
drwxr-xr-x 2 200 300 !096 )o( 2# 2007 webthub-1.01
-rwxr-xr-x 1 root root 3192 )o( 2# 2007 webthub.php
-rw-rw-r-- 1 arood arood 20!80 )o( 2# 2007 webthub.tar
-rw-rw-r-- 1 arood arood #6#! *u& 9 2007 +our,ile.id
-rw-rw-r-- 1 arood arood 1662## *u& 9 2007 +our,ile.sw,
drwxr-xr-x 11 arood arood !096 -a+ 29 2007 .lib-1.2.3
$
0ere is the information a'out all the listed columns:
1. )irst Column: represents file type and permission g i&en on the file. 1elow is the description of all
type of files.
2. ,econd Column: represents the num'er of memory 'loc#s ta#en 'y the file or directory.
3. "hird Column: represents owner of the file. "his is the Unix user who created this file.
4. )ourth Column: represents g roup of the owner. /&ery Unix user would ha&e an associated g roup.
5. )ifth Column: represents file si!e in 'ytes.
6. ,ixth Column: represents date and time when this file was created or modified last time.
7. ,e&enth Column: represents file or directory name.
In the ls -l listing example% e&ery file line 'eg an with a d% -% or l. "hese characters indicate the type of file
that2s listed.
!re"i# Description
- .eg ular file% such as an A,CII text file% 'inary executa'le% or hard lin#.
$ 1loc# special file. 1loc# input/output de&ice file such as a physical hard dri&e.
c Character special file. .aw input/output de&ice file such as a physical hard dri&e
d (irectory file that contains a listing of other files and directories.
l ,ym'olic lin# file. 3in#s on any reg ular file.
p Named pipe. A mechanism for interprocess communications
s ,oc#et used for interprocess communication.
Meta %&aracters:
*eta characters ha&e special meaning in Unix. )or example ' and ( are metacharacters. $e use ' to match
4 or more characters% a -uestion mar# ( matches with sing le character.
)or /xample:
$ls ch/.doc
(isplays all the files whose name start with ch and ends with .doc:
ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch0!-1.doc ch0!0.doc ch0#.doc ch06-2.doc
ch01-2.doc ch02-1.doc c
0ere ' wor#s as meta character which matches with any character. If you want to display all the files
ending with 5ust )doc then you can use following command:
$ls /.doc
*idden Files:
An in&isi'le file is one whose first character is the dot or period character 6.7. UNI prog rams
6including the shell7 use most of these files to store config uration information.
,ome common examples of hidden files include the files:
)pro"ile: the 1ourne shell 6 sh7 initiali!ation script
)+s&rc: the 8orn shell 6 #sh7 initiali!ation script
)cs&rc: the C shell 6 csh7 initiali!ation script
)r&osts: the remote shell config uration file
"o list in&isi'le files% specify the -a option to ls:
$ ls -a
. .pro,ile docs lib test_results
.. .rhosts hosts pub users
.eacs bin hw1 res.01 work
.exrc ch07 hw2 res.02
.kshrc ch07.bak hw3 res.03
$
,ing le dot ): "his represents current directory.
(ou'le dot )): "his represents parent directory.
Note: I ha&e put stars 697 5ust to show you the location where you would need to enter the current and new
passwords otherwise at your system% it would not show you any character when you would type.
%reating Files:
:ou can use ,i editor to create ordinary files on any Unix system. :ou simply need to g i&e following command:
$ (i ,ilenae
A'o&e command would open a file with the g i&en filename. :ou would need to press #ey i to come into edit
mode. +nce you are in edit mode you can start writing your content in the file as 'elow:
0his is unix ....,ile 1 created it .....,orthe,irsttie
12 &oin& to sa(e this content in this ,ile.
+nce you are done% do the following steps:
;ress #ey esc to come out of edit mode.
;ress two #eys S&i"t - .. tog ether to come out of the file completely.
Now you would ha&e a file created with "ile/a/e in the current directory.
$ (i ,ilenae
$
Editing Files:
:ou can edit an existing file using ,i editor. $e would co&er this in detail in a separate tutorial. 1ut in
short% you can open existing file as follows:
$ (i ,ilenae
+nce file is opened% you can come in edit mode 'y pressing #ey i and then you can edit file as you li#e. If you
want to mo&e here and there inside a file then first you need to come out of edit mode 'y pressing #ey esc
and then you can use following #eys to mo&e inside a file:
l #ey to mo&e to the rig ht side.
& #ey to mo&e to the left side.
+ #ey to mo&e up side in the file.
0 #ey to mo&e down side in the file.
,o using a'o&e #eys you can position your cursor where e&er you want to edit. +nce you are positioned then
you can use i #ey to come in edit mode. /dit the file% once you are done press esc and finally two #eys S&i"t
- .. tog ether to come out of the file completely.
Display %ontent o" a File:
:ou can use cat command to see the content of a file. )ollowing is the simple example to see the content of
a'o&e created file:
$ cat ,ilenae
0his is unix ,ile....1 created it ,or the ,irst tie.....
12 &oin& to sa(e this content in this ,ile.
$
:ou can display line num'ers 'y using -$ option along with cat command as follows:
$ cat ,ilenae -b
1 0his is unix ,ile....1 created it ,or the ,irst tie.....
2 12 &oin& to sa(e this content in this ,ile.
$
%o1nting 2ords in a File:
:ou can use the 3c command to g et a count of the total num'er of lines% words% and characters contained
in a file. )ollowing is the simple example to see the information a'out a'o&e created file:
$ wc ,ilenae
2 19 103 ,ilenae
$
0ere is the detail of all the four columns:
1. )irst Column: represents total num'er of lines in the file.
2. ,econd Column: represents total num'er of words in the file.
3. "hird Column: represents total num'er of 'ytes in the file. "his is actual si!e of the file.
4. )ourth Column: represents file name.
:ou can g i&e multiple files at a time to g et the information a'out those file. 0ere is simple syntax:
$ wc ,ilenae1 ,ilenae2 ,ilenae3
%opying Files:
"o ma#e a copy of a file use the cp command. "he 'asic syntax of the command is:
$ cp source_,ile destination_,ile
)ollowing is the example to create a copy of existing file "ilena/e.
$ cp ,ilenae cop+,ile
$
Now you would find one more file copy"ile in your current directory. "his file would 'e exactly same as
orig inal file "ilena/e.
4ena/ing Files:
"o chang e the name of a file use the /, command. Its 'asic syntax is:
$ ( old_,ile new_,ile
)ollowing is the example which would rename existing file "ilena/e to ne3"ile:
$ ( ,ilenae new,ile
$
"he /, command would mo&e existing file completely into new file. ,o in this case you would fine only
ne3"ile in your current directory.
Deleting Files:
"o delete an existing file use the r/ command. Its 'asic syntax is:
$ r ,ilenae
%a1tion: It may 'e dang erous to delete a file 'ecause it may contain useful information. ,o 'e careful
while using this command. It is recommended to use -i option along with r/ command.
)ollowing is the example which would completely remo&e existing file "ilena/e:
$ r ,ilenae
$
:ou can remo&e multiple files at a tile as follows:
$ r ,ilenae1 ,ilenae2 ,ilenae3
$
Standard Uni# Strea/s:
Under normal circumstances e&ery Unix prog ram has three streams 6files7 opened for it when it starts up:
1. stdin : "his is referred to as standard input and associated file descriptor is 4. "his is also
represented as ,"(IN. Unix prog ram would read default input from ,"(IN.
2. stdo1t : "his is referred to as standard output and associated file descriptor is <. "his is
also represented as ,"(+U". Unix prog ram would write default output at ,"(+U"
3. stderr : "his is referred to as standard error and associated file descriptor is =. "his is also
represented as ,"(/... Unix prog ram would write all the error messag e at ,"(/...

Das könnte Ihnen auch gefallen