![]() |
| |||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Suppose I wish to list all files in my directory that have an extension of '.txt'. I want to tell 'rm' to remove files where the first part is any number of any characters, followed by '.txt' The symbol '*' in Unix means just that: any number including zero of any characters. It is termed a wildcard For example: % ls -a . .. .txt f1.txt f2.txt txt txt.f3 txt.f4 % rm *.txt % ls -a . .. .txt txt txt.f3 txt.f4 % rm txt.* % ls -a . .. .txt txt And: % rm *will remove all files. Remember that you can use the '-i' option with 'rm', causing 'rm' to prompt for each file to be removed and hopefully avoiding accidentally removing all files from the wrong directory. The character '?' matches any one character. rm test?will remove 'test1', 'testa', and 'test$'. It will not remove 'test' or 'test12'. Enclose characters in '[ ]' to match any one of a specific list of characters. % ls b[aeiou]gwill list 'bag', 'beg', 'big', 'bog', and 'bug'; but not 'bfg' or 'bags'. To match any one of a range of characters, separate a pair of characters with '-'. [a-z] matches any lowercase letter. |
Tell Me More...
|
|
Dot Files Notice that 'rm *.txt' does not remove '.txt', although it should do. There is a special rule that says leading dots in filenames must be matched explicitly. % rm .*will remove all hidden files. More Complex Globbing [a-z0-9]matches any lowercase letter or digit. Glob patterns may be combined as in: % ls [mt]*day[0-9]to match filenames that start with 'm' or 't', have any number of intervening characters, and end in 'day' followed by one digit. Filename Order When a glob pattern is expanded into a number of filenames, they are presented in alphanumeric order. |
Recursion is when a command is applied not only to all files in the specified directory, but to all files in each nested directory too. The command works the entire directory structure rooting out every last file.
Compare:
% ls ~with:
% ls -R ~Many commands can operate recursively, usually by including option '-R' or '-r'.
|
We can now make use of a recursive 'cp' to copy directories. The last time we tried we got this: % mkdir new-dir % cp new-dir new-dir-2 cp: new-dir is a directory (not copied). Now we try again with a recursive copy: % cp -r new-dir new-dir-2This worked, and made a copy of the directory. It doesn't matter that new-dir is empty; we still must use a recursive copy. If new-dir were to contain many files and deeply nested directories, the entire directory structure and all files would be reproduced in new-dir-2. Previously we used 'rmdir' to remove a directory, but learnt that the directory had to be empty. Clearly a directory hierarchy containing many files and nested directories is going to be difficult to remove. The task is made easy using wildcards and recursion. % rm -i *will remove all files in the current directory. I have included the '-i' interactive option as a safety net - remember it prompts you for each file removal. To delete a directory completely, use: % rm -ri new-dirZap! The whole directory, including all sub-directories, was removed. Note that we used the 'rm' command rather than rmdir. 'rm' applied recursively will remove directories as it empties them. Without option '-r' rm will not remove directories. You may omit option '-i', but beware that a command such as: % rm -r *issued at the top level of your home directory will delete every file in every directory that you own! Be warned! |
Tell Me More...
|
|
The 'cp' command, like all pure Unix commands, does not understand Mac resource forks. Hence, 'cp' applied to files or recursively to directories will copy only the data fork, and strip resource forks from the copied files. Ditto 'ditto' is a copy command that does understand resource forks. It copies files and directories, recursively, optionally preserving resource forks. The copy is a carbon copy of the original, including owners, permissions and time-stamps. For example, to create a backup of /Users/you to a disc called 'Backup', one could use: % ditto -rsrc /Users/you/ /Volumes/Backup/Users/yourm -rf * The option '-f' says to force remove - i.e. don't issue any warning for files that are not writeable - just delete them. Silent but deadly! |
|
|
To report on how much disc space a directory occupies use the disc usage command 'du'. To query your home directory use: % du ~This displays the size of every directory from your home directory down. The size of a directory includes the size of all directories within it. The last line displayed will be the total size of your home directory. The size is reported in file-system blocks. Each block is 512 bytes or 1/2k. Take the answer you get and divide by 2 to get the size in k-bytes. To report on how much disc space a directory occupies, but without displaying that information for every sub-directory, use the '-s' or summary option. % du -sk ~Will print a single line displaying the total size of your home directory, and in k-bytes too (requested by the '-k' option). To display how full each disc and partition is, use the 'df' command: % df -kOption '-k' causes sizes to be displayed in k-bytes. |
More 'ls' Tricks % ls -aFwill display each file with a trailing character to indicate the file type. / means directory will also display file sizes in k-bytes. 'file' The command 'file' attempts to determine the type of a file - like: executable |
Command Synopsises
Key:
[ ] - optional part
directory... - 1 or more directories
In Part 4
In the next part to this column I will cover Unix file permissions, multiple users, passwords, and Mr root.
In the meantime, try this:
% lastEnjoy :-)
Discuss this article in the Learning Center forum
|
|
Part 3 - Playing With Directories (page 2 of 2) |
|
| Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved. | ||
| Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users. | ||
| Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc. |