660 likes | 882 Views
CSCI 330 The UNIX System. Unit II The file system. The Unix file system. hierarchical organization of files contains directories and files always single tree basic commands to list and manipulate files independent of physical file system organization typical Unix file system types
E N D
CSCI 330The UNIX System Unit II The file system
The Unix file system • hierarchical organization of files • contains directories and files • always single tree • basic commands to list and manipulate files • independent of physical file system organization • typical Unix file system types • ext4 (formerly ext2, ext3) • reiserfs • also:vfat, ntfs CSCI 330 - The UNIX System
Unix file system Layout root (/) bin home boot dev etc … users’ home directories … lib CSCI 330 - The UNIX System media X11 opt mnt opt var proc usr root sbin srv tmp lib bin include
Common Unix directories bin Essential command binaries boot Static files of the boot loader dev Device files etc Host-specific system configuration lib Essential shared libraries and kernel modules media Mount point for removable media mnt Mount point for temporary file systems opt Add-on application software packages proc data on running system root home directory for system administrator sbin Essential system binaries srv Data for services provided by this system tmp Temporary files usr Secondary hierarchy var Variable data CSCI 330 - The UNIX System
Directory content • regular files • text, data • binaries, executables • links to other files or directories • system files • device files: character or block special • networking endpoints: socket, pipe • directories CSCI 330 - The UNIX System
Directory terminology • Root Directory: / • top-most directory in any UNIX file structure • Home Directory: ~ • directory owned by a user • default location when user logs in • Current Directory: . • default location for working with files • Parent Directory: .. • directory immediately above the current directory CSCI 330 - The UNIX System
File and directory names • Use the following characters: • Uppercase letters (A-Z) • Lowercase letters (a-z) • Numbers (0-9) • Underscore ( _ ) • Period/dot ( . ) • avoid the following characters: CSCI 330 - The UNIX System
Wildcards in filenames • Shell allows special characters in filename pattern to help with file selection * matches zero or more characters ? matches any single character [] matches a set of characters • These special characters are called “wildcards” CSCI 330 - The UNIX System
File names • Unix file name does not require file extension • no consideration for extension when treating files • However, some extensions are commonly used • Program source code: .c .h .cc .cpp .f .f77 .f95 • Compiled object code: .o .a .so .sa • Compressed files: .gz .zip .bz2 • Archive files: .tar .tgz • Web site source code: .html .css .shtml .php • Text files that will be moved to Windows: .txt • Executable files typically have no extension CSCI 330 - The UNIX System
Path • path: list of names separated by “/” • Absolute Path • Traces a path from root to a file or a directory • Always begins with the root (/) directory Example: /home/student/Desktop/assign1.txt • Relative Path • Traces a path from the current directory • No initial forward slash (/) • dot (.) refers to current directory • two dots (..) refers to one level up in directory hierarchy Example: Desktop/assign1.txt CSCI 330 - The UNIX System
Path to file3 CSCI 330 - The UNIX System Absolute Path: /usr/staff/joan/file3
File system commands CSCI 330 - The UNIX System
Current Directory’s Path • To determine the full path of the current working directory, use the command named “pwd” • pwd stands for print working directory • Example: to show path of current working directory % pwd /home/student/ CSCI 330 - The UNIX System
List directory content • most frequently used file system command: “ls” • displays files in directory Syntax: ls [options] [path] • displays files in current directory if path is missing CSCI 330 - The UNIX System
List directory content • common options: -a show all files -l show long version of listing -t show files sorted by time stamp -S show files sorted by file size -r show files in reverse sorted order CSCI 330 - The UNIX System
Long List Option CSCI 330 - The UNIX System
.is current dir. ..is parent dir. dot (.) names are hidden files directories List everything in directory List contents of the current directory in long format % ls -al total 126 drwxr-xr-x 13 egecsci 1024 Apr 26 15:49 . drwxr-xr-x 15 root root 512 Apr 24 15:18 .. -rwx------ 1 egecsci 1120 Apr 12 13:11 .config -rwxr--r-- 1 egecsci 885 Dec 2 13:07 .login -rw-r--r-- 1 egecsci 141 Mar 14 13:42 .logout -rwx------ 1 egecsci 436 Apr 12 11:59 .profile drwx------ 7 egecsci 512 May 17 14:11 330 drwx------ 3 egecsci 512 Mar 19 13:31 467 drwx------ 2 egecsci 512 Mar 31 10:16 Data -rw-r--r-- 1 egecsci 80 Feb 27 12:23 quiz.txt CSCI 330 - The UNIX System plain file
List all in a specific directory % ls -l unix/grades total 10 -rwxr-xr-x 3 egecsci 72 Jan 19 19:12 330assign-graderun -rwxr-xr-x 1 egecsci 70 Jan 19 19:13 330exam-graderun -rwxr-xr-x 2 egecsci 70 Jan 19 19:12 330quiz-graderun -r-x------ 1 egecsci 468 Feb 1 11:55 test-330grade -r-x------ 1 egecsci 664 Feb 1 11:55 test-330scores CSCI 330 - The UNIX System
Creating a New Directory Syntax: mkdir [ -p ] directory-list CSCI 330 - The UNIX System
mkdir examples % mkdir csci330 % mkdir test-data % mkdirdirOnedirTwo % mkdir /home/student/unix/demo % mkdir –p /home/student/unix/demo CSCI 330 - The UNIX System directories in path must already exist
Example: Create a Directory to create a directory Data under csci330: • Absolute Path: • Relative Path: dev etc usr home local tty null student … bin Desktop You are here csci330 unix demo Temp CSCI 330 - The UNIX System Data mkdir/home/student/Desktop/csci330/Data mkdir csci330/Data
Changing Directory CSCI 330 - The UNIX System
Changing Directory from the Data directory, change to home directory: • Absolute Path: • Relative Path: also: dev etc usr home local tty null student … bin Desktop csci330 unix demo Temp You are here CSCI 330 - The UNIX System Data cd /home/student cd ../../.. cd~ cd
Remove Directories • If empty, use “rmdir” Example: To remove an empty directory called “test” % rmdir test • if non-empty, use “rm -r” Example: To remove non-empty directory “old-data” % rm -r old-data CSCI 330 - The UNIX System
File System Commands CSCI 330 - The UNIX System
Copying Files Syntax: cp source target • source is one or more items to copy • target is either name of copied item, or directory • Commonly used options: -i if “target” exists, the command cp prompts for confirmation before overwriting -r recursively copy entire directories -p preserve access times and permission modes CSCI 330 - The UNIX System
Examples: Copying a file • Make a copy of a file % cp assign1.txt assign1.save • Copy “assign1.txt” to a different directory with name “assign1.save” % cp assign1.txt ~/archive/assign1.save • Copy “assign1.txt” to a different directory % cp assign1.txt ~/archive/ CSCI 330 - The UNIX System
Copying multiple Files Syntax: cp source-files destination-directory % cp assign1.txt assign2.txt ~/archive % cp assign?.txt ~/archive • Files will have same name in destination directory CSCI 330 - The UNIX System
Moving Files • To move files from one directory to another directory, or to re-name a file, use: “mv” CSCI 330 - The UNIX System
Moving a file • Move “assign1.txt” a different directory • If the destination file exists, “mv” will not overwrite existing file: % mv assign1.txt ~/archive • Move “assign1.txt” a different directory and rename it to “assign1.save” % mv assign1.txt ~/archive/assign1.save CSCI 330 - The UNIX System
Moving multiple Files Syntax:mv source-files destination-directory % mv assign1.txt assign2.txt ~/archive % mv assign?.txt ~/archive • Files will have same name in destination directory CSCI 330 - The UNIX System
Renaming files or directories • use “mv” Example: rename file “unix” to “csci330” % mvunix csci330 Caveat: what happens if “csci330” exists and is a directory ? CSCI 330 - The UNIX System
Deleting files or directories Syntax:rm path-list • Commonly used options: -f force remove regardless of permissions -i prompt for confirmation before removing -r removes everything under the indicated directory Example: remove file “old-assign” % rmunix/assign/old-assign CSCI 330 - The UNIX System
Linking Files • Allows one file to be known by different names • A link is: • A reference to a file stored elsewhere on the system • A way to establish a connection to a file to be shared • 2 types: • Hard link • Symbolic link (a.k.a. “soft link”) CSCI 330 - The UNIX System
The ln command • hard link: ln shared-file link-name • symbolic link: ln –s shared-file link-name CSCI 330 - The UNIX System
home student dir1 dir2 bb aa dir3 Link illustration • create entry “bb” in “dir3” as link to file “aa” in “dir1” CSCI 330 - The UNIX System
File System Layout • file consists of: • data blocks • identified by block id • file meta information: inode • contains: • which blocks make up file • permissions, etc. • stored in inode table • index into table is inode number • directory is table of: • file name -> inode number CSCI 330 - The UNIX System
. 2406 home 2407 2408 . student . dir1 dir2 bb file data aa dir3 Hard Link example Contents of dir1 inode table CSCI 330 - The UNIX System disk blocks Contents of dir3
. 2598 home 2599 2600 . student . dir1 dir2 bb aa dir3 Symbolic Link example Contents of dir1 inode table CSCI 330 - The UNIX System disk blocks Contents of dir3 /home/student/ dir1/aa
Link type comparison CSCI 330 - The UNIX System
Finding Files • The command named “find” can be used to locate a file or a directory Syntax: find path-list expression(s) • “find” recursively descends through directories in path-list and applies expression to every file CSCI 330 - The UNIX System
The find command • path-list specifies where to look for files Ex.: find /tmp ~ • expressions specifies what to do with files that were found, can be test or action CSCI 330 - The UNIX System
find expressions • simple tests: -name -type -size -empty -executable Examples: • find . -name “*.txt” • find ~ -type d • find /tmp -empty CSCI 330 - The UNIX System
find expressions • simple actions: -print (default) -delete -exec Examples: • find . -name “*.txt” • find ~ -name “*.txt” -exec lpr • find /tmp -empty -delete CSCI 330 - The UNIX System
Create Edit (will be discussed in separate course unit) Display Contents Print Others Operations Unique to Regular Files CSCI 330 - The UNIX System
Creating New Files • simplest way to create a new empty file: • touch command • originally meant to update access time stamp • side effect: if file does not exist, it will be created as an empty file • Example: % touch newfile • other ways to create new files: • editor • redirect output from command CSCI 330 - The UNIX System
Display Contents of Text Files Display Text File contents cat more less pg head tail CSCI 330 - The UNIX System
Viewing Contents of Text Files • command “cat” can be used to display/concatenate one or more files, displaying the output all at once Example: Display the contents of file “assign1.txt” % cat assign1.txt CSCI 330 - The UNIX System
Viewing Contents of Text Files • “more”, “less” or "pg" display the contents of one or more files one page at a time • Space bar – to advance to next page • b – to go back a page • Enter Key – to advance to next line Example: Display the contents of file “assign1.txt” one page at a time % more assign1.txt CSCI 330 - The UNIX System
Viewing Contents of Text Files • “head” displays the beginning portion of indicated file(s); the default head size is 10 lines. Example: Display first 20 lines of file “assign1.txt” % head -20 assign1.txt CSCI 330 - The UNIX System