Download presentation
Presentation is loading. Please wait.
Published byAnissa Greer Modified over 9 years ago
1
Chapter 3. The Unix Filesystem
2
Files and Directories (1) u What is a file? –a container for ordered data –persistent (stays around) and accessible by name u Unix files –regular Unix files are pretty simple v essentially a sequence of bytes –Unix files are identified by a name in a directory v this name is actually used to resolve the hard disk name/number, the cylinder number, the track number, the sector, the block number –you see none of this v it allows the file to be accessed
3
Files and Directories (2) u Unix files come in other flavors as well, such as –Directories v a file containing pointers to other files v equivalent of a “folder” on a Mac or Windows –Links v a pointer to another file v used like the file it points to v similar to “shortcuts” in Windows, but better –Devices v access a device (like a soundcard, or mouse, or...) like it is a file
4
Figure 3-3 A Directory Hierarchy
5
Directories (1) u Current Working Directory –the directory you are looking at right now –the shell remembers this for you u To determine the Current Working Directory, use the command pwd (Print Working Directory) Use: mlc104$ pwd Result: print the current working directory /home/tom
6
Directories (2) u Moving about the filesystem –Use the “cd” (Change Directory) command to move between directories and change the current directory Use: mlc104$ cd public_html Result: Makes public_html the current working directory (must be a subdirectory of current directory) –Another Use: “cd..” (two periods) to move up a directory Use: mlc104$ cd.. Result: Now you are back in the directory you started from!
7
Directories (2) u Listing the contents of a directory –Use the “ls” (LiSt directory) command to list the contents of a directory or “ls –F” mlc104$ ls bio gradesF04 public_html –Use the “ls -F” command to list the contents of a directory with a ‘/’ after each directory mlc104 $ ls bio gradesF04/ public_html/
8
Figure 3-17 The mkdir Command
9
Simple Directory Tools u Create a directory with the mkdir command mkdir newdirname u newdirname can be given with pathname mlc104$ pwd /home/tom mlc104$ ls bio gradesF04 public_html mlc104$ mkdir chap3 mlc104$ ls -F bio chap3/ gradesF04/ public_html/
10
Figure 3-19 The rmdir Command
11
Simple Directory Tools (2) u Remove a directory with the rmdir command rmdir dirname –dirname is the directory to remove and can be specified using a pathname –if the directory exists and is empty it will be removed u Examples: mlc104$ ls -F bio chap3/ gradesF04/ public_html/ mlc104$ rmdir chap3/ mlc104$ ls -F bio gradesF04/ public_html/ Assuming chap3 is still empty
12
Figure 3-23 The cp Command
13
Simple Directory Tools (3) u Copy a file from one directory to another mlc104$ ls -F bio chap3/ mlc104$ cp bio chap3 mlc104$ ls chap3 bio u Copying a directory mlc104$ cp chap3 chap3bak cp: chap3: is a directory mlc104$ cp -r chap3 chap3bak mlc104$ ls chap3bak bio Cannot use just cp to copy a directory Must do a recursive copy (cp -r) to copy a directory
14
Figure 3-30 The mv Command
15
Simple Directory Tools (4) u Move a file from one directory to another mlc104$ ls bio foods chap3 mlc104$ ls chap3 bio mlc104$ mv foods chap3 mlc104$ ls chap3 bio foods mlc104$ ls bio chap3 u You can also move a directory the same way - it is just a special file, after all.
16
Figure 3-3 A Directory Hierarchy
17
Navigation u The upside-down tree –the Unix filesystem is organized like an upside-down tree v at the top of the filesystem is the root –write this as a lone slash: / –this is NOT a backslash (opposite of MS-DOS)! v For example, you can change to the root directory: mlc104[21] $ cd / mlc104[22] $ ls -F TT_DB/ dev/ home/ mnt/ sbin/ xfn/ bin/ devices/kernel/ net/ tmp/ cdrom/ etc/ lib opt/ usr/ coreexport/ local/ platform/ var/
18
System Directories u Some standard directories and files in a typical Unix system –/the root –/binBINaries (executables) –/devDEVices (peripherals) –/deviceswhere the DEVICES really live –/etcstartup and control files –/libLIBraries (really in /usr) –/optOPTional software packages –/procaccess to PROCesses –/sbinStandalone BINaries –/tmpplace for TeMPorary files –/home/where home directories are mounted –/home/cst334where your home dirs are
19
Typical System Directory Contents –/usr USeR stuff –/usr/bin BINaries again –/usr/includeinclude files for compilers –/usr/libLIBraries of functions etc. –/usr/locallocal stuff –/usr/local/binlocal BINaries –/usr/local/liblocal LIBraries –/usr/openwinX11 stuff –/usr/sbinsysadmin stuff –/usr/tmpplace for more TeMPorary files –/usr/ucbUCB binaries –/var VARiable stuff –/var/mailthe mail spool
20
Pathnames (1) u A typical Unix file system spans many disks –As a user you don’t know or need to know which physical disk things are on v in fact, you don’t even know which machine they are attached to: disks can be “remote” (eg: your home directory is stored on a disk attached to a server in the machine room) v Look at the df command to see different disks and space used –Inside each directory may be more directories u The Absolute Path –to identify where a file is, string the directories together v separating names with slashes: v e.g. /home/cst334/trebold v this is the absolute path for trebold’s home directory v lists everything from the root down to the directory you want to specify
21
Pathnames (2) u When you first log in, you are in your HOME directory –To see what this is: mlc104$ pwd /home/cst334/trebold –Your home directory is also stored in the environment variable HOME mlc104$ echo My home is $HOME My home is /home/cst334/trebold –You can “Go Home” by typing mlc104$ cd $HOME
22
Pathnames (3) u Some shorthand –In some shells (including tcsh, csh, and bash), $HOME can be abbreviated as ~ (tilde) –Example: mlc104$ cd ~/bin v change to the bin directory under your home directory (equivalent to $HOME/bin) v this is where you usually store your own commands or “executables” –To quickly go home: mlc104$ cd with no parameters, cd changes to your home directory –~user refers to the home directory of user v For me, ~trebold is the same as ~ v ~stude5555 refers to Joe Student’s home directory /home/stude5555)
23
Pathnames (4) u Relative pathnames –You can also specify pathnames relative to the current working directory v This is called a relative pathname –For example mlc104[28] $ pwd /home/cst334 mlc104$ ls bio chap3 mlc104[30] $ cd chap3 mlc104[31] $ pwd /home/tom/chap3 u For most commands which require a file name, you can specify a pathname (relative or absolute)
24
Pathnames (5) u Every directory contains two “special” directories:. and... : another name for the current directory –e.g. cp chap3/bio... : another name for the immediate parent directory of the current directory –use this to cd to your parent: mlc104$ pwd /home/mpc80/mpc80a01 mlc104[33] $ cd.. mlc104[34] $ pwd /home/mpc80/ mlc104[35] $ cd../.. mlc104[36] $ pwd /
25
Pathnames (6) u You can locate a file or directory by this way: –look at the first character of the pathname v / start from the root v. start from the current directory v.. start from the parent directory v ~ start from a home directory v otherwise start from the current directory –going down to the subdirectories in the pathname, until you complete the whole pathname. –if you start in ~tom, the following are equivalent: v /home/tom/chap3/foods v ~/chap3/foods v chap3/foods
26
Searching Directories u What if you need to locate a file, or set of files, in a large directory structure? –Using cd and ls would be very tedious! u The command find is used to search through directories to locate files. –Wildcards can be used, if the exact file name is unknown, or to find multiple files at once. –Can also find files based on size, owner, creation time, type, permissions, and so on. –Can also automatically execute commands on each file found. u Do a “man find” for details and examples!
27
Searching Files for Words and Phrases u What if you need to locate a certain word or phrase in a file, or set of files, in a large directory structure? u The command grep is used to search through directories to locate files containing certain words (grep=Global Regular Exression Print). –Wildcards can be used, if the exact file name is unknown, or to find multiple files at once. u Do a “man grep” or check book for details and examples!
28
System/Control Files u What files do I already have? –Startup files for bash and tcsh (.bash_profile,.bashrc) –Contain commands run after you type your password, but before you get a prompt –Assume you’ve not used your account before mlc104$ ls mlc104$ –Why can’t I see any files? v Files beginning with a ‘dot’ are usually control files in Unix and not generally displayed –Use the –a option to see all files mlc104$ ls -a./../.bash_profile.bashrc
29
Interactive Copy (-i) u OK, let us study some new commands, and variations of some familiar ones mlc104$ ls -a./../.bash_profile.bashrc mlc104$ cp.bashrc my_new_file mlc104$ ls -a./../.bash_profile.bashrc my_new_file mlc104$ cp -i.bash_profile my_new_file cp: overwrite my_new_file (yes/no)? y list all files including those beginning a with. The –i option says to ask when this overwrites existing files.
30
Interactive Remove (-i) mlc104[57] $ rm -i my_new_file rm: remove my_new_file (yes/no)? y mlc104[58] $ ls –a./../.bash_profile.bashrc -i also verifies on the rm command
31
Unix Filenames (1) u Almost any character is valid in a file name –all the punctuation and digits –the one exception is the / (slash) character –the following are not encouraged v ? * [ ] “ ” ’ ( ) & : ; ! –the following are not encouraged as the first character v - ~ –control characters are also allowed, but are not encouraged u UPPER and lower case letters are different –A.txt and a.txt are different files
32
Unix Filenames (2) u No enforced extensions –The following are all legal Unix file names v a v a. v.a v … v a.b.c u Remember files beginning with dot are hidden –ls cannot see them, use ls -a u. and.. are reserved for current and parent directories
33
Unix Filenames (3) u Even though Unix doesn't enforce extensions, –“.” and an extension are still used for clarity v.jpg for JPEG images v.tex for LaTeX files v.sh for shell scripts v.txt for text files v.mp3 for MP3’s –some applications may enforce their own extensions v Compilers look for these extensions by default –.c means a C program file –.C or.cpp or.cc for C++ program files –.h for C or C++ header files –.o means an object file
34
Unix Filenames (4) u Executable files usually have no extensions –cannot execute file a.exe by just typing a –telling executable files from data files can be difficult u “file” command Use:file filename Result:print the type of the file Example: mlc104$ file ~/.bashrc.bashrc: executable bash script u Filenames and pathnames have limits on lengths –1024 characters typically –these are pretty long (much better than MS-DOS days and the 8.3 filenames)
35
Fixing Filename Mistakes u It is very easy to get the wrong stuff into filenames –Say you accidentally typed mlc104$ cp myfile -i –What if you type mlc104$ rm -i v The shell thinks -i is an option, not a file v Getting rid of these files can be painful u There is an easy way to fix this... –You simply type mlc104$ rm -- -i –Many commands use “--” to say there are no more options Creates a file with name -i
36
Unix Quoting (1) u Double Quotes: "...." –Putting text in double quotes "..." stops interpretation of some shell special characters (whitespace mostly) –Examples: mlc104[12] $ echo Here are some words Here are some words mlc104[13] $ echo "Here are some words" Here are some words mlc104[14] $ mkdir "A directory name with spaces! " mlc104[15] $ ls A* A directory name with spaces!/
37
Unix Quoting (2) u Single Quotes '...' –Stops interpretation of even more specials v Stop variable expansion ($HOME, etc.) v Backquotes `...` (execute a command and return result...we’ll get to this later) v Note difference: single quote ( ' ), backquote ( ` ) v Examples: mlc104$ echo "Welcome $HOME" Welcome /gaul/s1/student/1999/csnow mlc104$ echo ‘Welcome $HOME’ Welcome $HOME
38
Unix Quoting (3) u Backslash \ –‘quotes’ the next character –Lets one escape all of the shell special characters mlc104$ mkdir Dir\ name\ with\ spaces\*\* mlc104$ ls Dir\ * Dir name with spaces**/ –Use backslash to escape a newline character mlc104$ echo "This is a long line and\ we want to continue on the next“ This is a long line and we want to continue on the next –Use backslash to escape other shell special chars v Like quote characters mlc104$ echo \"Bartlett\'s Familiar Quotations\" "Bartlett's Familiar Quotations"
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.