Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 1 of 44 Copyright ©2008 by Pearson Education, Inc. Upper.

Slides:



Advertisements
Similar presentations
Learning Unix/Linux Bioinformatics Orientation 2008 Eric Bishop.
Advertisements

Jump to first page Unix Commands Monica Stoica Jump to first page Introduction to Unix n Unix was born in 1969 at Bell Laboratories, a research subdivision.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
Working with Files How to create, view, copy, rename and print files.
Lab6 CPCS361 I.Mona Alshehri. Working with Files and Directories Creating files Create a file with the cat command type the command cat > name_of_file.
Cosc 4750 Getting Started in UNIX Don’t be afraid of the prompt, in linux it can be your best friend. In some cases, the only way to do certain things.
Introducing the Command Line CMSC 121 Introduction to UNIX Much of the material in these slides was taken from Dan Hood’s CMSC 121 Lecture Notes.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Linux+ Guide to Linux Certification, Second Edition
Linux+ Guide to Linux Certification, Second Edition
Chapter 5 Editing Text Files
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
UNIX Filters.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Chapter 4: UNIX File Processing Input and Output.
"Two of the most famous products of Berkeley are LSD and Unix. I don't think that this is a coincidence.” Anonymous.
Advanced File Processing
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 9 Part II Linux Command Line Access to Linux Authenticated login using a Linux account is required to access a Linux system. The Linux prompt will.
Working with Files Chapter 5. Display a Calendar Display a calendar for a specific month – cal Display a calendar for a specific year – cal 2000.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
3 File Processing Mauro Jaskelioff. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash.
Introduction to Shell Script Programming
UNIX Unbounded 5th Edition Amir Afzal Chapter 3 Getting Started
Copyright ©2008 by Pearson Education, Inc
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
Unix programming Term: III B.Tech II semester Unit-II PPT Slides Text Books: (1)unix the ultimate guide by Sumitabha Das (2)Advanced programming.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
LIN Unix Lecture 3 Hana Filip. LIN UNIX Resources UNIX Tutorials UNIX help for.
Linux+ Guide to Linux Certification, Second Edition
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
Jozef Goetz, expanded by Jozef Goetz, 2009 Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
BIF713 Basic Unix/Linux Commands Getting Help with Commands.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
1 of 47 Chapter 4: The vi Editor – First Look Copyright ©2008 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved.
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: whereis, which, whoami, finger, passwd, cal, date Working with Files: cat, more, less.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
PTA Linux Series Copyright Professional Training Academy, CSIS, University of Limerick, 2006 © Workshop III - Part B Shell Commands Professional Training.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 6: The vi Editor – Last Look 1 of 55 Copyright ©2008 by Pearson Education, Inc. Upper Saddle.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Learning Unix/Linux Based on slides from: Eric Bishop.
Lesson 5-Exploring Utilities
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
Some Linux Commands.
Vi Editor.
Tutorial of Unix Command & shell scriptS 5027
Tutorial of Unix Command & shell scriptS 5027
Guide To UNIX Using Linux Third Edition
Tutorial of Unix Command & shell scriptS 5027
The Unix File System.
Presentation transcript:

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 1 of 44 Copyright ©2008 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 2 of 44 This is the second chapter that discusses the UNIX file system and its associated commands; it complements the material discussed in Chapter 5. Chapter 8 presents more commands for manipulating files, including commands for copying files, moving files, and looking at the contents of a file. The chapter also explains the shell input/output redirection operators and file substitution metacharacters.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 3 of FILE READING Assume that your working directory is david and you have a file (say, 20 pages long) called large_file in it. To read large_file using the vi editor, you type  vi -R large_file [Return] This opens large_file for read only. Contents of large_file are displayed on the screen, and you can use vi commands to view the other pages. To read large_file using the cat command, you type  cat large_file [Return] The contents of large_file are displayed and scroll before your eyes. You can use [Ctrl-s] and [Ctrl-q] to stop and resume scrolling.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 4 of The vi Editor Read-Only Version: The view Command Viewing a file using the view command prevents you from saving your editing or changes to the file. You can use it only to read a file. Refer to Chapter 6 for usage examples. To read large_file using the view command, you type  view large_file [Return] This opens large_file for read only. Contents of large_file are displayed on the screen, and you can use vi commands to view the other pages Reading Files: The pg command pg is used to view files one screen at a time. A prompt sign (:) is produced at the bottom of the screen. Press [Return] to continue viewing the rest of the file. pg shows EOF (end of the file) on the last line of the screen when it reaches the end of your file. Press [Return] at this point to get to the $ prompt.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 5 of 44 Unlike other commands’ options, some pg options start with the plus sign (+). pg is not available on all systems. You can check its availability by typing pg, or man pg (to look at the manual pages about the pg commands)

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 6 of 44 Assuming there is a file called large_file in the working directory, Type:  pg large_file [Return] This is a simple way of looking at large_file one screen at a time. Type:  pg -p Next +45 large_file [Return] This displays large_file starting from line 45 and shows the prompt Next instead of the normal prompt : (colon). Two options are used: -p to change the default prompt from : to Next, and +45 to start viewing from line 45. Type:  pg -s +/hello large_file [Return] This shows prompt and other messages in reverse video, and starts viewing from the first line that contains the word hello. Two options are used: -s to show the prompt and other messages in reverse video, and +/hello to search for first occurrence of the word hello.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 7 of 44 You can give commands to move forward or backward a specified number of pages or lines to view different parts of the text. Table 8.2 summarizes some of these commands.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 8 of SHELL REDIRECTION Among the most useful facilities that the shell provides are the shell redirection operators. The shell redirection operators allow you to do the following: Save the output of a process in a file. Use a file as input to a process. 1.A process is any executable program. This could be an appropriate shell command, 2.an application program, or a program you have written. 3.The redirection operators are instructions to the shell and are not part of the command syntax. Accordingly, they can appear anywhere on the command line. 4.Redirection is temporary and effective only with the command using it Output Redirection Output redirection allows you to store the output of a process in a file. Then you can edit it, print it, or use it as input to another process. The shell recognizes the greater than sign (>) and double greater than sign (>>) as output redirection operators. The format is as follows: command > filename or command >> filename

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 9 of 44 For example, Type:  ls > mydir.list [Return] This redirects ls output to mydir.list. The double greater than sign (>>) redirection operator works the same as the greater than sign (>) operator, except for the fact that it appends the output to the Specified file. Type:  ls >> mydir.list [Return] This adds the filenames in the working directory to the end of the file called mydir.list. 1.If the specified filename already exists, then it is written over, and the contents of the existing file are lost. 2.If the specified filename does not exist, then the shell creates a new file. 1.If the specified file does not exist, then the shell creates a new file to save the output in it. 2.If the specified filename does exist, then the shell adds the output to the end of the file, and the previous contents of the file remain intact.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 10 of Input Redirection The shell recognizes the less than sign (<) and the double less than sign (<<) as the input redirection operators. The format is as follows: command < filename or command << word For example  cat < mydir.list [Return] This displays the contents of mydir.list. The redirection operator (<<) is used mostly in script files (shell programs) to provide standard input to other commands.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 11 of The cat Command Revisited To refresh your memory, let’s look at the following command lines: $ cat myfirst [Return] This displays myfirst. $ cat -n myfirst [Return] This displays myfirst with line numbers.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 12 of 44 Creating Files: The cat Command Using the cat command with the output redirection operator (>), to create a file. For example: $ cat > myfirst [Return]... Create a file called myfirst. _ Type the following text. I wish there were a better way to learn UNIX. Something like having a daily UNIX pill. [Ctrl-d] End your typing. $_ Ready for the next command. $ cat myfirst [Return]... Check to see if myfirst has been I wish there were a better way to learn UNIX. Something like having a daily UNIX pill. $_ Ready for the next command. 1.If myfirst does not exist in your working directory, then cat creates it. 2.If myfirst already exists in your working directory, then cat overwrites it. The contents of the old myfirst are lost. 3.If you do not want to overwrite a file, use the (>>) operator.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 13 of 44 Appending Files: The cat Command Using the cat command with the output redirection operator (>>), to append a file. For example: $ cat >> myfirst [Return] Append to a file called myfirst. _ Type the following text. However, for now, we have to suffer and read all these boring UNIX books. [Ctrl-d] Signal the end of the input text. $_ Back to the prompt. $ cat myfirst [Return] Display the contents of myfirst. I wish there were a better way to learn UNIX. Something like having a daily UNIX pill. However, for now, we have to suffer and read all these boring UNIX books. 1.If myfirst does not exist in your working directory, then cat creates the file. 2.If myfirst exists, then cat appends the input text to the end of the existing file. In this example, your one line of input was appended to myfirst. Thus three lines of text are displayed.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 14 of 44 Copying Files: The cat Command Using the cat command with the output redirection operator (>), to copy files. For example: $ cd [Return] This make sure you are in your HOME directory. $ cat myfirst > myfirst.copy [Return] This copies myfirst to myfirst.copy. The input to the cat command is myfirst file, and the output from the cat command (the contents of the myfirst) is saved in myfirst.copy.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 15 of ENHANCED FILE PRINTING Use the pr command to format a file before printing or viewing it. pr with no options formats the specified file into pages 66 lines long. pr puts a five-line heading at the top of the page that consists of: two blank lines, one line of information about the specified file, and two more blank lines. The information line includes the current date and time, the name of the specified file, and the page number. pr produces five blank lines at the end of each page. For Example:  pr myfirst [Return] This formats a file called myfirst. (See Figure 8.1.)

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 16 of 44 pr Options Table 8.3 summarizes the pr command options. 1.The -m or -columns is used to produce multicolumn output. 2.The -a option can only be used with the -columns option and not -m.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 17 of 44

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 18 of Practicing Linux Alternative Command Options Using the Linux alternative options for the pr command: $ pr --pages=2 large_file [Return] This displays large_file starting from page 2. $ pr --columns=2 myfirst [Return] This displays myfirst in two columns. $ pr --double-space myfirst [Return] This displays myfirst in double space. $ pr --omit-header --columns=2 --across names [Return] This displays names across two columns with no header. $ pr --omit-header --columns=2 names [Return] This displays names in two columns separated sign. $ pr --help [Return] This displays the help page.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 19 of FILE MANIPULATION COMMANDS Continuation of the file manipulation commands that were discussed in Chapter Copying Files: The cp Command The cp (copy) command is used to create a copy (duplicate) of a file. You can copy files from one directory to another or make a backup copy of a file. Figure 8.2 shows your directory structure before and after application of the cp command. If the target file already exists, then its contents are destroyed.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 20 of 44 The following command sequences show how the cp command works: $ ls [Return] List the current directory files. memos REPORT $ cp REPORT REPORT.COPY [Return] This copies REPORT to REPORT.COPY. $ ls [Return] See the list of files REPORT.COPY is in the list. memos REPORT REPORT.COPY $ cp REPORT REPORT [Return]... Source and target filenames are the same. File cannot be copied onto itself 0 file(s) copied $_ Ready for the next command.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 21 of 44 Copy a file from your current directory to another directory. Figure 8.3 shows your directory structure before and after the cp command. $ cp REPORT memos [Return] This creates a copy of REPORT in memos. $ ls memos [Return] List files in memos directory, and REPORT is there, as you expected. REPORT When the target file is a directory name, then the source file is copied in the specified directory with the same filename as the source filename.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 22 of 44 Suppose you have files called names and scores under your current directory (david) and you want to copy them to the memos directory. Figure 8.4 shows your directory structure before and after the cp command. $ cp names scores memos [Return] This copies names, scores in current directory to the memos directory. 1. The files called names and scores are in your current directory. 2. The filenames on the command line are separated by at least one space. 3. The last filename must be a directory name. In this case, memos is a directory name.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 23 of 44 cp Options Table 8.4 summarizes the cp command options.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 24 of 44 Using cp Option: -b Move REPORT to memos and create a backup of REPORT if it already exits in memos. Figure 8.5 shows your directory structure before and after the cp -b command. $ cp -b REPORT memos [Return] $ ls memos [Return] List files under memos. REPORT REPORT~ Notice that REPORT is backed up and the backup filename is REPORT~.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 25 of 44 Using cp Option: -i The -i option protects you from overwriting an existing file. It asks for confirmation if the target file already exists. If your reply is yes, it copies the source file, overwriting the existing file. If your answer is no, then it quits, and your existing file remains intact. $ cp -i REPORT memos [Return]..... Make a copy of REPORT under memos. Target file already exists overwrite?.... Shows confirmation prompt; Press [y] [Return] for yes or [n] [Return] for no. Using cp Option: -r Copy files and subdirectories in david to another directory called david.bak using the -r option. You can use cp with the -r option to copy directories and all their contents into a new directory. $ cp -r./memos./david.bak [Return] This copies memos directory and all the files in it to david.bak.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 26 of 44 1.If david.bak exists in your current directory, then files and directories in memos are copied into david.bak. 2.If david.bak does not exist in your current directory, then it is created and all the files and directories including memos itself are copied into david.bak. Now the pathname for files in memos under david.bak is./david.bak/memos.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 27 of 44 Practicing Linux Alternative cp Options $ cp --interactive REPORT memos [Return] (Same as cp -i REPORT memos.) $ cp --recursive./memos./david.bak [Return] (Same as cp -r /memos/david.bak.) $ cp --help [return] Display the help page. $ cp -v names scores memos [Return]..... Copy names and scores in the current directory to the memos directory. names -> memos/names scores -> memos/scores $_ Ready for the next command. Notice the feedback from the -v option that shows what file is copied and where it is copied.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 28 of Moving Files: The mv Command Use mv to move a file from one place to another or to change the name of a file or a directory. $ mv REPORT REPORT.OLD [Return] Change REPORT in the current directory to REPORT.OLD. Figure 8.7 shows your directory structure before and after application of the mv command to rename REPORT.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 29 of 44 Move REPORT to the memos directory: $ mv REPORT memos [Return] Move REPORT to memos. Figure 8.8 shows your directory structure after application of the mv command to move REPORT.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 30 of 44 mv Options Table 8.5 shows some of the mv command options. The following command lines show the usage of the mv command options.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 31 of 44 Using mv Option: -b (backup) $ mv -b REPORT memos [Return] Move REPORT to memos and create a backup of REPORT if it already exits in memos. $ ls memos [Return] Check files under memos. REPORT REPORT~ $_ Ready for the next command. Notice that REPORT is backed up and the file name is REPORT~. $ mv -v REPORT memos [Return] Move REPORT to memos. REPORT -> memos/REPORT.. $_ Ready for the next command.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 32 of 44 Linux Alternative mv Options $ mv --backup REPORT memos [Return].... Same as mv -b REPORT memos. $ mv --interactive REPORT memos [Return].. Same as mv -i REPORT memos. $ mv --verbose REPORT memos [Return].... Same as mv -v REPORT memos. $ mv --version [Return] Display version information. $ cp --help [Return] Display the help page. Read the help page and familiarize yourself with other options available for the mv command.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 33 of Linking Files: The ln Command You can use the ln command to create additional names for an existing file and refer to the same file with different names. For example: $ ln REPORT RP [Return] This creates a file named RP in your current directory and links that name to REPORT. Now, REPORT and RP are two names for a single file. Figure 8.9 shows your directory structure before and after the ln command application. At first glance, this looks like the cp command, but it is not!

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 34 of 44 cp physically copies the file into another place, and you have two separate files. Whatever changes you make in one are not reflected in the other. ln just creates another filename for the same file; no new file is created. If you change anything in any of the linked files, the changes are there in the file regardless of the name you use to refer to it. To experiment with the ln command, try the following command sequence: $ cat > xxx [Return] This creates a file called xxx. Now type the following line in the file: Line 1:aaaaaa $ [Ctrl-d] This signals end of the input. $ ln xxx yyy [Return] This links yyy to xxx. $ cat yyy [Return] This displays the contents of the xxx, using the filename yyy. The output is, as you expected, the contents of xxx: Line 1:aaaaaa

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 35 of 44 Figure 8.10 shows your directory structure before and after application of the ln command. When you create a file, you also establish a link between the directory and the file. Thus, the link count for every file is at least one, and subsequent use of ln adds to the number of links.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 36 of 44 Linux Alternative Options for the ln Command Use the Linux alternative options for the ln command, as suggested in the following command lines: $ ln --version [Return] This displays version information. $ ln --help [Return] This display the help page. Read the help page and familiarize yourself with other options available for the ln command. Final Words The three commands cp, mv, and ln all affect the filenames and work in a similar manner, but they are different commands and are used for different purposes: cp creates a new file. mv changes the filename or moves files from one place to another. ln creates additional names (links) for an existing file.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 37 of Counting Words: The wc Command wc is used to find out the number of lines, words, or characters in a file or a list of specified files. $ cat myfirst [Return] This displays the contents of myfirst. I wish there were a better way to learn UNIX. Something like having a daily UNIX pill. However, for now, we have to suffer and read all these boring UNIX books. $ wc myfirst [Return] This counts the number of lines, words, and characters in myfirst myfirst $_ Ready for the next command. The first column shows the number of lines, the second column shows the number of words, and the third column shows the number of characters.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 38 of FILENAME SUBSTITUTION Most file manipulation commands require filenames as arguments. The shell supports file substitution, which allows you to select those filenames that match a specified pattern. These patterns are created by specifying filenames that contain certain characters that have a special meaning to the shell. These special characters are called metacharacters (or wild cards). Table 8.7 summarizes the wild cards that can stand for one or more characters in a file name.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 39 of The ? Metacharacter The question mark (?) is a special character that the shell interprets as a single character substitution and expands the filename accordingly. $ ls -C [Return] This lists the filenames in your working directory. report report1 report2 areport breport report32 $ ls -C report? [Return] This uses a single question mark in the filename. report1 report2 shell expands the filename report? to the filename report followed by exactly one character, any character. Thus, the filenames report1 and report2 are the only two files that match the pattern. $ ls report?? [Return] This uses two question marks as special characters. report32 shell expands the filename report?? to the filename report followed by exactly two characters, any characters. Thus, the filename report32 is the only file that matches the pattern.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 40 of The * Metacharacter The asterisk (*) is a special character that the shell interprets as any number of characters (including zero characters) of substitution in a filename, and expands the filename accordingly. $ ls -C [Return] This lists the filenames in your working directory. You have the following files: report report1 report2 areport breport report32 $ ls -C report* [Return] This lists all filenames that begin with the word report: report report1 report2 report32 shell expands the filename report* to the filenames report followed by any number of characters, any characters. $ ls -C *report [Return] This lists all the filenames that end with the word report: report areport breport.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 41 of The [ ] Metacharacters The open and close brackets are special characters that surround a string of characters. The shell interprets this string of characters as filenames that contain the specified characters, and expands the filenames accordingly. Using the ! before the specified string of characters (but within the brackets) causes the shell to display the filenames that do not contain the characters in the string at the specified position. ❏ To list all the filenames that start with a or b: $ ls -C [ab]* [Return] areport breport shell expands the filename [ab]* to the filename a or b followed by any number of characters, any characters. Thus, areport and breport are the only two filenames that match the specified pattern. ❏ To list all the filenames that do not start with a or b: $ ls -C [!ab]* [Return] report report1 report2 report32

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 42 of 44 The [ ] Metacharacters You can use the [ ] metacharacters to specify a range of characters or digits. For example, [5-9] means the digits 5, 6, 7, 8, or 9; and [a-z] means all the lowercase letters of the alphabet. ❏ List all the filenames that end with the digits1 to 32: $ ls *[1-32] [Return] report1 report2 report32 ❏ Show all the single-capital-letter file names, assuming you have some single- letter filenames. $ 1s -C [A-Z] [Return] A B D W

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 43 of Metacharacters and Hidden Files To use the metacharacters for displaying hidden files, filenames starting with. (dot) you must explicitly have the. (dot) as part of the specified pattern. ❏ List all the invisible (hidden) files: $ ls -C.* [Return].exrc.profile shell expands the filename.* to the filename. (dot) followed by any number of characters, any characters. Thus, only the hidden files are displayed. The pattern is.* and there is no space between the dot and the asterisk. Wild cards are not limited in use to only the ls command. You can use wild cards with other commands that need filename arguments. ❏ Examples of filename substitution: $ rm *.* [Return] This deletes all the files with filenames that contain at least one dot. $ rm report? [Return] This delete all filenames that begin with report and end with only one character, any character.

Amir Afzal UNIX Unbounded, 5th Edition Copyright ©2008 Chapter 8: The UNIX File System Continued 44 of 44 Metacharacters and Hidden Files ❏ More examples of filename substitution: $ cp * backup [Return] This copies all files from the current directory to the backup directory. $ mv file[1-4] memos [Return] This move file1, file2, file3, and file4, indicated by the range [1–4], to the memos directory. $ rm report* [Return] This deletes all the filenames that start with the string report. There is no space between the string report and the asterisk wild card. $ rm report * [Return] This deletes all the files. In the preceding example, there is a space between the string report and the asterisk wild card. This space can have disastrous consequences. The command rm report * is interpreted as “delete a file called report, and then delete all the other files.’’ In other words, all files in the current directory are deleted.