Open Directory Site  Advanced Unix Tutorials

Home | Windows 98 | Windows Me | Windows 2000 Windows XP | Unix | Advanced Unix

Home >> Operating Systems >> Advanced Unix >> Directory Maintenance

Directory Maintenance

Wildcards

Selecting a subset of files within a directory to perform a command can be accomplished using reular expressions and wildcards. Common wildcards include the asterisk (*) and question mark (?). The asterisk can represent zero or more characters. The question mark represents exactly one character. Examples of these are as follows:

 

            C*                                           Any file that begins with C.

            ?C*                                          Any file with a second letter as C.

            prog*.C                                  Any file that begin with the letters prog and ends with a .C.

help*.d??                                Any file that begin the word help, and ends with .d and any two other characters.

            *angle*                                   Any file that has the word angle in it.

 

Other infrequently used wildcards include the square brackets ( [ ] ) and the curly braces ( { } ). The square brackets are used to specify a range of characters such as the lowercase letters a through z with [a-z], or the numbers 0 through 9 with [0-9]. The curly braces select files that could contain one of a group of possible words such as either the word doc or txt with {doc,txt}. Examples follow:

 

            [0-9]*                                      Any file that begins with a number.

            *.{doc,txt,asc}                        Any file with a doc, txt, or asc.

            *[A-Z][A-Z]*                         Any file that contains two uppercase letters.

            {cat,dog}*.fm                         Any file that begins the word cat or dog, and ends with .fm.

 

grep

Grep is a utility to search through one or more files for a string of text. Grep supports some aspect of regular expressions, but in its simplest form the syntax is as follows: grep “string to search for” filename(s). The output of grep is the lines that contain the string. You may use wildcards for files. If a string has spaces or other characters that may be interpreted by the shell, you will need to put quotes around it. Also, if no file is specified grep will search through stdin, standard input. Now try out a few commands using grep:

 

grep animal *                        Search through all files in the present directory for the word animal.

grep “Chapter 2” *.doc        Search through all files that end in .doc for the string Chapter 2.

            cat summer.txt | grep rose Display the lines in the file summer.txt that contain

                                                            the word rose.

ps -elf | grep uniqname       List only those processes that the user with the loginID uniqname has running.

 

find

The find command can be used to locate these files recursively in a directory structure. The find command has numerous options, but -name, -exec, and -print are the most common. With the find options, you can search for files by file names, file types, modification times and so forth.

 

The syntax of the find command is as follows: find directoryname options. To display the results of the find command, you need to include the -print option. Without this option, find will still function, but all found files will never be displayed to the screen. The -name option followed by a wildcard sequence or specific file name will instruct find to locate files that fit that file space. If you do use wildcards with the -name option, you must backslash them. If you do not backslash them, they will be interpreted by the shell and not passed on to find. The -exec switch is useful to perform a command on every found file (for example, -exec command {} command_options \; ). The curly braces indicate where find is to insert the actual file name found, the \; tells find where to terminate the command, and the text between them is the actual command that will be executed on each found file.

 

Examples of the find command follow:

 

find . -name cisco -print       Search through the current directory as well as all directories under the current for files named cisco and print the results to the screen.

find . -name \?\?e\* -print  Search for any file that matches the wildcard ??e* or all files that have e as their third character and print the  results.

find . -name \*.txt -exec cat {} \;    Search for all files that end in .txt and cat those files that match.

 

 

tar

Originally for tape archive, tar is now mainly used to consolidate a group of files and directories into one file, or to extract files from a previously tarred file. The way to consolidate a series of files in the current directory into a single file called cons.tar would be to use the command tar -cvf cons.tar *. Note that tar does not compress these files, it merely stores them into this one file. The switches at the front of the command are short for create/verbose/file. When you consolidate a series of files into a single tarred file, the originals are not deleted.

 

To extract the files from within the tarred file games.tar issue the command tar -xvf games.tar. All of the files and directories stored within games.tar would be extracted into the current directory. Tar will create directories while it is extracting so that the original hierarchy of files and directories are preserved upon extraction.

 

gzip/gunzip

These two utilities are responsible for compressing and expanding large files to save disk space; the syntax is very straightforward: gzip options filename or gunzip filename. Gzip compresses the given file. One option frequently used is the speed/size compression switch. This switch is a number -1 through -9. The -9 switch will compress the file into as small a file as the algorithm will support taking as much time as is necessary. The -1 switch will compress the file as quickly as possible at the cost of a little bit looser compression. Gzip will add a .gz extension to the original file, and upon completion, it will erase the original file.

 

A good candidate for compression is a tarred file. Frequently, you will see a file ending in .tar.gz or .tgz. This indicates a tarred file that also has been gzipped. Gunzip will uncompress a gzipped file. You do not need to specify any options here as the decompression will be calculated from the base file.

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting