Posted: July 22nd, 2009 | Author: Troy | Filed under: Linux, Solaris | No Comments »
The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.
touch’s syntax is
touch [option] file_name(s)
When used without any options, touch creates new files for any file names that are provided as arguments (i.e., input data) if files with such names do not already exist. Touch can create any number of files simultaneously.
Thus, for example, the following command would create three new, empty files named file1, file2 and file3:
touch file1 file2 file3
A nice feature of touch is that, in contrast to some commands such as cp (which is used to copy files and directories) and mv (which is used to move or rename files and directories), it does not automatically overwrite (i.e., erase the contents of) existing files with the same name. Rather, it merely changes the last access times for such files to the current time.
Several of touch’s options are specifically designed to allow the user to change the timestamps for files. For example, the -a option changes only the access time, while the -m option changes only the modification time. The use of both of these options together changes both the access and modification times to the current time, for example:
touch -am file3
The -r (i.e., reference) option followed directly by a space and then by a file name tells touch to use that file’s time stamps instead of current time. For example, the following would tell it to use the times of file4 for file5:
touch -r file4 file5
The -B option modifies the timestamps by going back the specified number of seconds, and the -F option modifies the time by going forward the specified number of seconds. For example, the following command would make file7 30 seconds older than file6.
touch -r file6 -B 30 file7
The -d and -t options allow the user to add a specific last access time. The former is followed by a string (i.e., sequence of characters) in the date, month, year, minute:second format, and the latter uses a [[CC]YY]MMDDhhmm[.ss] format. For example, to change the last access time of file8 to 10:22 a.m. May 1, 2005, 1 May 2005 10:22 would be enclosed in single quotes and used as follows, i.e.,:
touch -d '1 May 2005 10:22' file8
Partial date-time strings can be used. For example, only the date need be provided, as shown for file9 below (in which case the time is automatically set to 0:00):
touch -d '14 May' file9
Just providing the time, as shown below, automatically changes the date to the current date:
touch -d '14:24' file9
The most commonly used way to view the last modification date for files is to use the ls command with its -l option. For example, in the case of a file named file10 this would be
ls -l file10
The complete timestamps for any file or directory can be viewed by using the stat command. For example, the following would show the timestamps for a file named file11:
stat file11
The –help option displays a basic list of options, and the –version option returns the version of the currently installed touch program.
Posted: July 15th, 2009 | Author: Troy | Filed under: Solaris | Tags: examples | No Comments »
Searching for Files (find)
The find command searches for files that meet conditions you specify, starting from a directory you name. For example, you might search for file names that match a certain pattern or that have been modified within a specified time frame.
Unlike most commands, find options are several characters long. You must specify the starting directory before your desired options.
In the previous example, directory is the name of the starting directory and options represents the options for the find command.
Each option describes a criterion for selecting a file. A file must meet all criteria to be selected. The more options you apply, the narrower the field becomes. The -print option indicates that you want the system to display the results.
The -name filename option tells find to select files that match filename. Here filename is taken to be the rightmost component of a file’s full path name. For example, the rightmost component of the file /usr/bin/calendar is calendar. This portion of a file’s name is often called the base name.
For example, to see which files within the current directory and its subdirectories end in s, type the following command.
$ find . -name '*s' -print
./programs
./programs/graphics
./programs/graphics/gks
./src/gks
$
|
The following table describes other options of the find command.
Table 3–1 find Options
| Option |
Description |
| -name filename |
Selects files with a rightmost component that matches filename. Surround filename with single quotes if it includes filename substitution patterns. |
| -user userid |
Selects files that are owned by userid. userid can be either a login name or user ID number. |
| -group group |
Selects files that belong to group. |
| -m -time n |
Selects files that have been modified within n days. |
| -newer checkfile |
Selects files that have been modified more recently than checkfile. |
You can specify an order of options by combining options within escaped parentheses (for example, \(options\) ). Within escaped parentheses, you can use the -o flag between options to indicate that find should select files that qualify under either category, rather than just those files that qualify under both categories.
$ find . \( -name AAA -o -name BBB \) -print
./AAA
./BBB
|
In the previous example, the find command searches in the . directory for all files that are named AAA, then looks for all files named BBB. find then displays the results of both searches.
You can invert the sense of an option by including an escaped exclamation point before the option. find then selects files for which the option does not apply:
$ find . \!-name BBB -print
./AAA
|
You can also use find to apply commands to the files it selects with the following options.
-exec command ‘{}’ \;
You terminate this option with an escaped semicolon (\;). The quoted braces are replaced with the file names that find selects.
You can use find to automatically remove temporary work files. If you name your temporary files consistently, you can use find to search for and remove these files. For example, if you name your temporary files junk or dummy, this command finds and removes the files.
$ find . \( -name junk -o -name dummy \) -exec rm '{}' \;