
53615d46687133175f89357085a89e65.ppt
- Количество слайдов: 38
Unit 4 Working with Files and Directories © Copyright IBM Corporation 2008 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5. 1
Unit Objectives After completing this unit, you should be able to: • Describe the different file types • Describe file and pathnames • Create, delete, copy, move and list directories • Create, delete, copy and move files • View the content of both text and binary files © Copyright IBM Corporation 2008
File Types ordinary directory ascii text executable jpeg picture 100 164 238 475 . bashrc. profile myfile walrus special file hardware devices for example: /dev/lp 0 (printer device) logical devices for example: /dev/null (null device) html file © Copyright IBM Corporation 2008
Linux Filenames • Should be descriptive of the content • Should use only alphanumeric characters: UPPERCASE, lowercase, number, @, _ • Should not include embedded blanks • Should not contain shell metacharacters: * ? > < / ; & ! | ` ' " [ ] ( ) { } • Should not begin with + or - sign • Are case sensitive • Are hidden if the first character is a. (period) • Can have a maximum of 255 characters © Copyright IBM Corporation 2008
Directory Structure • All Linux directories are contained in one, virtual, unified filesystem. • Physical devices are mounted on mount points: – Floppy disks – Hard disk partitions – CD-ROM drives • No drive letters like A: , C: , . . . / bin dev boot home etc lib mnt proc root sbin © Copyright IBM Corporation 2008 tmp var usr . . . lost+found
/bin, /lib, /sbin / bin dev boot home etc lib mnt proc root tmp sbin var usr lost+found • /bin contains executables for every user. • /sbin contains system administration executables. • /lib contains libraries. • Should always be available: – At system boot – In single user mode – When booting from rescue disk © Copyright IBM Corporation 2008 . . .
/boot / bin dev boot home etc lib mnt proc root sbin tmp var usr • Contains kernel image and some other goodies © Copyright IBM Corporation 2008 . . . lost+found
/home / bin dev boot home etc eric mnt lib linus proc root sbin richard • Home directories of users © Copyright IBM Corporation 2008 tmp var usr . . . lost+found
/proc / bin dev boot home etc lib mnt proc root sbin tmp var usr • Is a virtual filesystem • Represents kernel and process information © Copyright IBM Corporation 2008 . . . lost+found
Other Directories in / / bin dev boot home etc lib mnt root proc sbin tmp var usr lost+found • /opt: used for some software from external providers – Separate filesystem advisable • Whatever you create for yourself © Copyright IBM Corporation 2008 . . .
Typical Filesystem Layout Typical Workstation: master boot record/partition table Windows C: on NTFS Typical Server: master boot record/partition table Linux /boot Extended Partition Linux / Linux swap Linux /usr Linux swap Linux /var Linux /tmp FAT filesystem is D: in Windows is mounted on /data in Linux /home © Copyright IBM Corporation 2008
Example Directory Structure / home tux 1 test doc tux 2 c mon_report trio_ltr walrus . bash_profile manuals test 1 © Copyright IBM Corporation 2008 tux 3 rp a b c pgms suba
Linux Pathnames Full pathnames: Start from / (the root directory) Relative pathnames: Start from the present working directory Examples: (working directory is /home/tux 1) /home/tux 1/doc/mon_report (full) doc/mon_report (relative). . /tux 3/pgms/suba (relative). /test (a file in the current dir) ~/test (same as above) © Copyright IBM Corporation 2008
Where Am I? • The pwd command (print working directory) can be used to find out what your current working directory is: $ pwd /home/tux 1 $ © Copyright IBM Corporation 2008
Change Current Directory • With the cd (change directory) command: $ cd dir_name $ cd doc $ cd /home/tux 1/doc $ cd ~tux 1/doc (relative) (full) (home) $ cd (Go to your home directory) $ cd. . (Go one directory up) $ cd - (Go to previous directory) © Copyright IBM Corporation 2008
Create Directories • With the mkdir (make directory) command: $ mkdir dir_name $ mkdir /home/tux 1/doc (full pathname) $ cd /home/tux 1 $ mkdir doc (relative pathname) © Copyright IBM Corporation 2008
Removing Directories • With the rmdir (remove directory) command: $ rmdir dir_name $ pwd /home/tux 1 $ rmdir doc test rmdir: doc: Directory not empty $ directory must be empty! © Copyright IBM Corporation 2008
Working with Multiple Directories • Create and remove multiple directories simultaneously with the -p flag. $ mkdir -p dir 1/dir 2/dir 3 $ rmdir -p dir 1/dir 2/dir 3 © Copyright IBM Corporation 2008
List the Contents of Directories • With the ls command: ls [ dir/file ] $ ls /home tux 1 tux 2 Important -l -a -t -R tux 3 options: long listing (more information) lists all files (including hidden) lists files sorted by change date lists contents recursively © Copyright IBM Corporation 2008
The touch Command • The touch command creates an empty file, or updates the modification time of an existing file. $ ls -l -rw-rw-r-- 1 tux 1 penguins 512 Jan 1 11: 10 docs $ touch docs $ ls -l -rw-rw-r-- 1 tux 1 penguins 512 Jan 1 15: 37 docs $ touch new $ ls -l -rw-rw-r-- 1 tux 1 penguins 512 Jan 1 15: 37 docs 0 Jan 1 15: 38 new © Copyright IBM Corporation 2008
Copying Files (1 of 2) • The cp command copies files: cp source[s] [target] Copying one file to another: $ cp. bashrc. old Copying multiple files into a target directory: $ cp doc/mon_report doc/walrus /tmp © Copyright IBM Corporation 2008
Copying Files (2 of 2) • cp can recursively copy directories with the -R flag. $ cp -R /home/tux 1/doc /tmp To prevent cp from overwriting existing files, use: $ cp -R -i /home/tux 1/doc /tmp cp: overwrite `/tmp/doc/walrus´? © Copyright IBM Corporation 2008
Moving and Renaming Files (1 of 2) • With the mv command: mv source[s] [target] To move a file do another directory: $ mv doc/walrus. . /tmp To rename a file: $ mv documents Use the -i option to prevent mv from overwriting existing files! © Copyright IBM Corporation 2008
Moving and Renaming Files (2 of 2) • Moving and renaming files can be combined by mv: $ cd $ pwd /home/tux 1 $ mv /tmp/walrus . /test/walrus 2 To move a directory: $ mv. /test /tmp mv is recursive by default © Copyright IBM Corporation 2008
Removing Files • With the rm command: $ rm test/walrus 2 $ ls test/walrus 2 ls: rob: No such file or directory If unsure, use -i option $ rm -i test/walrus 2 rm: remove `test/walrus 2´? To remove files and directories recursively: $ rm -ir test/ © Copyright IBM Corporation 2008
Listing File Contents • With the cat (concatenate) command: $ cat file 1 file 2. . . $ cat walrus "The time has come", the walrus said, "To talk of many things: Of shoes - and ships - and sealing wax Of cabbage - and kings And why the sea is boiling hot And whether pigs have wings. " $ © Copyright IBM Corporation 2008
Displaying Files Page by Page • With the more or less commands: $ less walrus "The time has come", the walrus said, "To talk of many things: Of shoes - and ships - and sealing wax Of cabbage - and kings And why the sea is boiling hot And whether pigs have wings. " /tmp/test/walrus 1 -6/6 (END) © Copyright IBM Corporation 2008
Displaying Binary Files • With the od command: $ od /usr/bin/passwd 0000000 042577 043114 000401 000000 0000020 000002 000003 000001 000000 107300 004004 000064 00000040 051430 000000 000064 000040 000006 000050 $ • With the strings command: $ strings /usr/bin/passwd /lib/ld. so. 1 __gmon_start__ __deregister_frame_info __register_frame_info. . . $ © Copyright IBM Corporation 2008
File Managers • Linux also offers different graphical file managers: – Nautilus (GNOME) – Konqueror (KDE) –. . . © Copyright IBM Corporation 2008
Virtual, Unified Filesystem • Linux does not use drive letters (A: , C: , D: ) to identify drives/partitions, but creates a virtual, unified filesystem. • Different drivers/partitions are mounted on a mountpoint. /dev/hda 2 / usr var tmp /dev/hda 2 is the "root filesystem /dev/hda 5 / bin lib sbin /dev/hda 6 / tmp spool log /dev/hda 5 is mounted on /usr /dev/hda 6 is mounted on /var © Copyright IBM Corporation 2008 this directory is now called /var/log
The mount Command • The mount command mounts a filesystem. – Makes it part of the unified filesystem structure mount [-t type] [-o opts] device mountpnt # mount /dev/hda 5 /usr /dev/hda 2 / /usr /var /tmp /dev/hda 5 / /bin /lib /sbin © Copyright IBM Corporation 2008
The umount Command • The umount command unmounts a filesystem. – It takes it out of the unified filesystem structure. – Filesystem should not be busy. umount {device|mountpnt} # umount /dev/hda 5 - OR # umount /usr © Copyright IBM Corporation 2008
The /etc/fstab File • /etc/fstab lists all known filesystems on the system. • Syntax: device mountpoint type options dump fsck • Filesystems with the noauto option are not mounted automatically, but can be used as templates for mount. # cat /dev/hda 1 /dev/hda 2 /dev/hda 5 /dev/hda 6 /dev/cdrom /dev/fd 0 none /etc/fstab /mnt/win. C / /usr /var /media/cdrom /media/floppy /proc /dev/pts vfat ext 3 iso 9660 auto proc devpts defaults noauto, owner, ro noauto, owner defaults gid=5, mode=620 0 1 1 1 0 0 Note: Some distributions use filesystem labels instead of device names! © Copyright IBM Corporation 2008 0 1 2 2 0 0
Mounting and Unmounting Removable Media • Most distributions configure /etc/fstab so that the console user is allowed to mount removable media (floppy, cd) on a predetermined mountpoint and with predetermined options (for security). • Always unmount media before ejecting. • The GUI typically offers icons that perform the mount. $ whoami tux 1 $ mount /media/cdrom $ mount. /dev/cdrom on /media/cdrom type iso 9660 (ro, nosuid, nodev, user=tux 1). $ ls /media/cdrom. $ umount /media/cdrom © Copyright IBM Corporation 2008
Accessing Windows Filesystems Directly • mtools is a collection of tools that can read/write Windows filesystems directly, using drive letters. # mcopy c: autoexec. bat /root/autoexec. bat # mformat a: # mdir a: • Devices must not be mounted! • Drive letters are mapped to physical devices in /etc/mtools. conf: drive a: file="/dev/fd 0" exclusive 1. 44 m mformat_only drive c: file="/dev/hda 1" © Copyright IBM Corporation 2008
Checkpoint 1. True or False? Linux imposes an internal structure on a regular file (not a directory or special file). 2. Which of the following is not a legal filename? a. b. c. d. ~tux 1/mydocs. tar. gz /home/tux 1/mydoc(1) /var/tmp/. secret. doc /home/. . /home/tux 1/one+one 3. What command would you use to copy the file /home/tux 1/mydoc to /tmp, and rename it at the same time to tempdoc? © Copyright IBM Corporation 2008
Unit Summary • There are three types of files: – Ordinary – Directory – Special • The Linux file system structure is a hierarchical tree. • Files are accessed using either full or relative path names. A full pathname always begins with a /. • The following commands can be used with directories: pwd, cd, mkdir, rmdir, touch, and ls. • The following commands can be used with files: cat, more, less, cp, mv, rm, touch, od, and strings. © Copyright IBM Corporation 2008