File Handling in Unix - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

File Handling in Unix

Description:

Link Count - number of links to the file's inode. User Name - the login ID of the user ... copies a set of files to a directory. second argument must be a directory ... – PowerPoint PPT presentation

Number of Views:964
Avg rating:3.0/5.0
Slides: 25
Provided by: drtimgo
Category:
Tags: file | handling | keys | unix

less

Transcript and Presenter's Notes

Title: File Handling in Unix


1
Chapter 3
  • File Handling in Unix

2
What is a file
  • A file is a stream of bytes.
  • A file is a stream of ASCII bytes.
  • A directory is a file
  • Input devices read streams of ASCII bytes
  • Output devices write streams of ASCII btyes

3
pwd
  • Print Working Directory shows you where you are
    in the file system
  • Useful when you are trying to do something you
    may not be allowed to doso you can see why you
    dont have permission.

4
mkdir
  • Used to create directories
  • You can only create directories in file systems
    you have permission to write to
  • A nice set of directories makes it easy to
    organize your files

5
cd
  • Change directory
  • Allows you to change your current working
    directory to any other you have permission to
    enter
  • cd by itself sends you to your home dir
  • the address can be relative or absolute

6
relative addressing
  • The address must not start with a /
  • the current address is applied to the left of the
    address specified
  • is often a shorter way to specify an address
  • two useful relative addresses
  • dot (.) identifies the current working directory
  • dot dot (..) identifies the parent directory of
    the current working directory

7
absolute addressing
  • The address must start with a /
  • the address must be the complete path to the file
  • some absolute addresses are shorter than using
    relative addressing, especially when changing
    file systems

8
touch
  • touch filename will create the filename if it
    does not exist
  • touch filename will change the date last accessed
    if the file exists
  • you can specify a date that touch is to use with
    the -t option

9
ls
  • Used to list one or more file names in the
    current directory
  • can show many different data elements about a
    file
  • Understanding the long file listing is very
    important

10
ls -l listing
  • Leftmost byte is file type
  • d is a directory file
  • - is a regular file
  • l is a link

11
permissions mask
  • First 3 bytes - User
  • The user owns the file
  • r (read)
  • w (write)
  • x (execute)
  • - permission denied
  • 2nd 3 bytes - Group
  • 3rd 3 bytes - Other

12
ls -l listing
  • Link Count - number of links to the files inode
  • User Name - the login ID of the user
  • Users Group - the group from /etc/passwd
  • File Size - Either in bytes or blocks
  • Creation/Modified Date - can be altered by touch
  • File Name - the actual link name

13
file names
  • There is no required formatting for Unix file
    names.
  • Most systems allow at least 256 byte file names
  • While you can use special characters in file name
    it is a Bad Thing to do so.
  • If you must use special characters you must
    protect them from interpretation by the shell

14
dots and file names
  • To the shell, the dot is just another character
    in a file name unless it is the first character.
  • A file name may have many dots
  • a.file.name.with.lots.of.d.o.t.s. is just fine
  • The only usual tool that requires a dot in the
    file name is the C compiler which wants prog.c as
    input.

15
dotted files
  • A filename that starts with a dot is called a
    dotted file, a hidden file or a protected file.
  • dotted files are exempt from file matching
    metacharacter expansion.
  • To see dotted files, use the -a (all) option on
    the ls command

16
cp
  • Two forms
  • cp sourcefile targetfile
  • copies (renames) a single file
  • cp sourcefiles targetdirectory
  • copies a set of files to a directory
  • second argument must be a directory
  • The -i (inquire/interactive) option can prevent
    you from overwriting existing files.

17
cpsome useful options
  • -i (inquire, keep from overwriting existing
    files.)
  • -r (recursive copy the entire directory structure
    from here on down)
  • -v (verbose show what is happening)
  • The one who makes the copy owns the file.

18
mv
  • Also called rename, changes the path and/or name
    of a file.
  • Does not change the ownership or permissions.
  • Can move whole directories
  • the -i option is very useful!

19
cat
  • Concatenation (of two or more files or a single
    file and standard out.)
  • Often used to view short files
  • Can be used to combine files with redirection of
    output
  • Can also be used to create short files

20
more
  • Designed to display a file one page at a time
  • Some commands
  • ltspacegt display next full page of text
  • ltreturngt display next line of text
  • q quit more
  • / ltpatterngt search for pattern in file
  • . (dot) repeat previous command

21
less
  • Created to give more functionality to more
  • Allows the use of arrow keys to move
  • more commands also work
  • Remember, less is more! ?

22
rm
  • Removes a link to an inode
  • when the inode count drops to zero, the file is
    deleted
  • Couple of important options
  • -i prompts before each removal
  • -r sets up a recursive remove, removes everything
    from the current directory on down this can be a
    Bad Thing!

23
rmdir
  • Designed to remove empty directories
  • wont remove directory if it contains files
  • can use rm -r to remove a directory and the files
    it contains
  • Once a file or directory has been deleted by the
    file system, you cant get it back.

24
The Structure of /etc/passwd
  • You should memorize the 7 fields in /etc/passwd
  • This file is critical for many Unix tasks
  • It is the file consulted for login, to apply file
    permissions, etc.
Write a Comment
User Comments (0)
About PowerShow.com