Name globbing and recursive directories under windows 7
See the man page for details. You have already seen how to create a directory with mkdir. Now, let's look further at mkdir and introduce rmdir , its analog for removing directories. Suppose you are in our lpi directory and you want to create subdirectories dir1 and dir2. Fortunately, mkdir has a -p option that allows it to create any required parent directories, as shown in Listing 7.
Removing directories using the rmdir command is the opposite of creating them. Again, there is a -p option to remove parents as well. You can remove a directory with rmdir only if it is empty as there is no option to force removal. You'll see another way to accomplish that particular trick when you look at recursive manipulation. Once you learn this, you will probably seldom use rmdir on the command line, but it is still good to know about it. You then used rmdir to remove all the directories you just created with mkdir.
As you can see, d1 and d2 were not removed because d2 was not empty. The other directories were removed. Once you remove the copy of text1 from d2, you can remove d1 and d2 with a single invocation of rmdir -p.
Up to now, the commands you have used have operated on a single file or perhaps a few individually named files. For the rest of this tutorial, you look at various operations for handling multiple files, recursively manipulating part of a directory tree, and saving or restoring multiple files or directories. The ls command has a -R note uppercase "R" option for listing a directory and all its subdirectories. The recursive option applies only to directory names; it does not find all the files called 'text1', for example, in a directory tree.
You can use other options that you have seen already along with -R. A recursive listing of our lpi directory, including inode numbers, is shown in Listing 9. You can use the -r or -R or --recursive option to cause the cp command to descend into source directories and copy the contents recursively. To prevent an infinite recursion, you cannot copy the source directory itself.
Listing 10 shows how to copy everything in your lpi directory to a copy1 subdirectory. You use ls -R to show the resulting directory tree. I mentioned earlier that rmdir only removes empty directories. You can use the -r or -R or --recursive option to cause the rm command to remove both files and directories as shown in Listing 11, where you remove the copy1 directory that you just created, along with its contents, including the backup subdirectory and its contents.
If you have files that are not writable by you, you might need to add the -f option to force removal. This is often done by the root user when cleaning up, but be warned that you can lose valuable data if you are not careful. Often, you might need to perform a single operation on many filesystem objects, without operating on the entire tree as you just did with recursive operations.
For example, you might want to find the modification times of all the text files you created in lpi, without listing the split files. Although this is easy with our small directory, it is much harder in a large filesystem.
To solve this problem, use the wildcard support that is built in to the bash shell. A string containing any of the characters '? Globbing is the process by which the shell or possibly another program expands these patterns into a list of pathnames matching the pattern. The matching is done as follows:.
Wildcard patterns and regular expression patterns share some characteristics, but they are not the same. Globbing is applied separately to each component of a path name. You can use it anywhere that you might specify multiple file or directory names, for example in the ls , cp , mv , or rm commands. In Listing 12, you first create a couple of oddly named files and then use the ls and rm commands with wildcard patterns. Remember that any wildcard characters in a command are liable to be expanded by the shell, which can lead to unexpected results.
Furthermore, if you specify a pattern that does not match any filesystem objects, then POSIX requires that the original pattern string be passed to the command. Some earlier implementations passed a null list to the command, so you might run into old scripts that give unusual behavior. Listing 13 illustrates these points. For more information on globbing, look at man 7 glob. You need the section number, as there is also glob information in section 3.
The best way to understand all the various shell interactions is by practice, so try these wildcards out whenever you have a chance. Remember to try ls to check your wildcard pattern before allowing cp , mv , or worse, rm to do something unexpectedly.
Now, let's look at the touch command, which can update file access and modification times or create empty files. In the next part, you can see how to use this information for finding files and directories. You continue using the lpi directory for the examples. You also look at the various ways you can specify time stamps.
The touch command with no options takes one or more filenames as parameters and updates the modification time of the files. This is the same time stamp normally displayed with a long directory listing. Listing 14 uses our old friend echo to create a small file called f1, and then uses a long directory listing to display the modification time or mtime. In this case, it happens also to be the time the file was created.
It then uses the sleep command to wait for 60 seconds and runs ls again. Note that the time stamp for the file has changed by a minute. If you specify a filename for a file that does not exist, then touch normally creates an empty file for you, unless you specify the -c or --no-create option. Listing 15 illustrates both these commands. Note that only f2 is created. The touch command can also set a file's modification time also known as mtime to a specific date and time using either the -d or -t options.
The -d is very flexible in the date and time formats that it accepts, while the -t option needs at least an MMDDhhmm time with optional year and seconds values.
Listing 16 shows some examples. If you're not sure what date a date expression might resolve to, you can use the date command to find out. Use the -d option with a date string to resolve the same kind of date formats that touch can. Note the different date formats in the listing for dates from a prior year or dates in the future. You can use the -r or --reference option along with a reference filename to indicate that touch or date should use the time stamp of an existing file.
Listing 17 shows some examples. A Linux system records both a file modification time and a file access time. These are also known respectively as the mtime and atime. Both time stamps are set to the same value when a file is created, and both are reset when it is modified. If a file is accessed at all, then the access time is updated, even if the file is not modified. For our last example with touch , you look at file access times. Listing 18 uses the cat command to access the f1 file and display its contents.
You then use ls -l and ls -lu to display the modification and access times respectively for f1 and f1a, which you created using f1 as a reference file. You then reset the access time of f1 to that of f1a using touch -a and verify that it was reset. For more complete information on the many allowable time and date specifications, see the man or info pages for the touch and date commands.
Now that we've covered the file and directory topic with the big recursive hammer that hits everything, and the globbing hammer that hits more selectively, let's look at the find command, which can be more like a surgeon's knife.
The find command is used to find files in one or more directory trees, based on criteria such as name, time stamp, or size. Again, you will use the lpi directory. The find command searches for files or directories using all or part of the name, or by other search criteria, such as size, type, file owner, creation date, or last access date.
The most basic find is a search by name or part of a name. Listing 19 shows an example from your lpi directory where you first search for all files that have either a '1' or a 'k' in their name, then perform some path searches that are explained in the following notes. In the first example above, you found both files and a directory. Use the -type parameter along with one-letter type to restrict the search.
Use 'f' for regular files, 'd' for directories, and 'l' for symbolic links. See the man page for find for other possible types. By using both upper and lower size bounds, you can find files whose size is within a given range. By default, the -size option of find assumes a unit of 'b' for byte blocks.
Among other choices, specify 'c' for bytes, or 'k' for kilobytes. In Listing 21, you first find all files with size 0, and then all with size of either 24 or 25 bytes. Note that specifying -empty instead of -size 0 also finds empty files. The second example in Listing 21 introduces the -print option, which simply prints the output to stdout. This an example of an action that can be taken on the results returned by the search. In the bash shell -print is the default action if no action is specified.
On some systems and some shells, an action is required; otherwise, there is no output. Other actions include -ls , which prints file information equivalent to that from the ls -lids command, and -exec , which executes a command for each file. The -exec has to be terminated by a semicolon, which must be escaped to avoid the shell interpreting it first. Remember that curly braces also have meaning to the shell and must be escaped or quoted.
Listing 22 shows how the -ls and the -exec options can be used to list file information. Notice that the second form does not list the inode information. You can use the -exec option for as many purposes as your imagination can dream up.
For our final examples of find , you use the time stamps described with the touch command to locate files having particular time stamps whose names start with 't'. You start by setting the time stamp of text2 to the day before yesterday, so that you can see differences in the output.
Listing 23 shows three examples:. The man pages for the find command can help you learn the extensive range of options that this brief introduction cannot cover. File names often have a suffix such as gif, jpeg, or html that gives a hint of what the file might contain.
Linux does not require such suffixes and generally does not use them to identify a file type. Knowing what type of file you are dealing with helps you know what program to use to display or manipulate it. The file command tells you something about the type of data in one or more files.
Listing 24 shows some examples of using the file command. The file command attempts to classify each file using three types of test. Filesystem tests use the results of the stat command to determine whether a file is empty or a directory, for example. So-called magic tests check a file for specific contents that identify it. These signatures are also known as magic numbers.
The first type that is found is reported unless the -k or --keep-going option is specified. The file command has many options that you can learn about using the man pages.
Listing 25 shows how to use the -i or --mime option to display the file type as a MIME string instead of the normal human-readable output. The magic number files are also managed by the file command. Again, see the man pages for more information.
The identify command, which is part of the ImageMagick package, is an additional tool that provides more detail when identifying image file types. Listing 26 shows an example. When you are backing up, archiving, or transmitting files, it is common to compress the files.
In a Linux environment, three popular compression programs are gzip , bzip2 , and xz. All three are lossless compression tools. Compression generally works well on text files. Many image formats already compress the data, so compression might not work well on these or other binary files.
Note that the compressed file has the same time stamp and has a. You decompress a gzipped file using the -d option of gzip or, more commonly, using the gunzip command. Listing 28 shows the first of these choices. Note that the uncompressed file now has the original file name and time stamp. The bzip2 command operates in a similar manner to gzip as shown in Listing The xz command's native format. See Resources for more information. Not surprisingly, the xz command operates in a similar manner to gzip and bzip2 as shown in Listing By design, many of the bzip2 and xz options are the same as those of gzip , but the commands do not all have identical options.
You might have noted that in both our examples, the uncompressed file had the same name and time stamp as the original. However, renaming or touching the compressed file can change this behavior. The gzip command has the -N or --name option to force the name and time stamp to be preserved, but bzip2 and xz do not. The gzip and xz commands also have a -l option to display information about the compressed file, including the name that will be used when it is decompressed.
Listing 31 illustrates some of these differences between the commands. For all three commands, the -v or --verbose provides information about the compression. All three compressors accept input from stdin using redirection the - parameter in a pipeline.
All support the -c or --stdout option to direct output to stdout. If input is from stdin, then output goes to stdout by default. Listing 32 shows some examples. The man pages can help you learn more about the other options of gzip , bzip2 , and xz. These provide cross-platform compression functions that are available on a wide range of hardware and operating systems.
Be aware that not all operating systems support the same file attributes or filesystem capabilities. If you download a zipped product file and unzip it on a Windows system and then transfer the resulting files to a CD or DVD for installation on Linux, you might experience problems installing because, for example, the Windows system does not support the symbolic links that were part of the original uncompressed file set. For more information on these or other compression programs, see their respective man pages.
The tar , cpio , and dd commands are commonly used for backing up groups of files or even whole partitions, either for archiving or for transmission to another user or site. Exam , which is part of the LPIC Linux Network Professional Certification, focuses on backup considerations in greater detail. These commands, along with the other commands you have learned about in this tutorial, give you the tools to perform any of these backup tasks.
The tar originally from Tape ARchive creates an archive file, or tarfile or tarball , from a set of input files or directories; it also restores files from such an archive. If a directory is given as input to tar , all files and subdirectories are automatically included, which makes tar very convenient for archiving subtrees of your directory structure.
Output can be to a file, a device such as tape or diskette, or stdout. The output location is specified with the -f option. Other common options are -c to create an archive, -x to extract an archive, -v for verbose output, which lists the files being processed, -z to use gzip compression, and -j to use bzip2 compression. Most tar options have a short form using a single hyphen and a long form using a pair of hyphens.
The short forms are illustrated here. See the man pages for the long form and for additional options. Listing 33 shows how to create a backup of the lpi directory using tar. Usually you want to compress archive files to save space or reduce transmission time. Btw, interesting, that if I'm adding a slash after an asterisk: Thanks a lot anyway. The reason the slash after asterisk didn't work is probably because the asterisk should have been escaped, see the edit to my answer. I don't know the full path, only partial, so "-wholename" won't work.