Previous Table of Contents Next


Module 138
touch

DESCRIPTION

The external touch command is used to update the access and modification times of a file. If the file given as an argument does not exist, touch will attempt to create it with a length of zero bytes.


BSD (Berkeley)
The BSD version of touch only updates the modification time of a file.

COMMAND FORMAT

Following is the general format of the touch command.

     touch [ -acm ] [ mmddhhmm[yy] ] file_list

BSD (Berkeley)
touch [ -cf ] file_list

Options

The following list describes the options used to control how touch functions.

-a Only the access time is updated. By default both access and modification times are changed.
-c If the file does not exist do not create it. By default the file is created if it does not already exist.
-m Only the modification time is updated.

BSD (Berkeley)
The BSD version of touch does not support the -a option since it does not make access time modifications. Nor does it support the -m option.
-c Same as SV.
-f Attempts to force a touch regardless of read and write permissions of the file.

Arguments

The following list describes the arguments that may be passed to the touch command.

mmddHHMM[yy] The date and time to be used for the new access and modification times. The mm is the month (01-12), dd is the day of the month (01-31), HH is the hours (00-24), MM is the minutes (00-59), and yy is the optional last two digits of the year (00- 99). If you do not provide a year then the year is left untouched.
The current date and time are used if you do not specify them.
file_list One or more files touch is to update or create.

FURTHER DISCUSSION

If only files are given for arguments, touch will use the current time to update the access and modification times of the files. If a file does not exist, it will be created with no data (the size will be zero).

If you need a file for shell script locking or date/time comparison, use the touch command to create it, since no disk space is used other than the name and inode. If you use echo or some other command, at least one block of data will be used when the file is created.

The access and modification times of a file are stored in the file's inode. The inode is the file information or header node. It is discussed in Module 2.

RELATED COMMANDS

Refer to the date command described in Module 30 and the ls command described in Module 84.

RETURN CODES

The return code for touch is equal to the number of files that could not be updated or created.

APPLICATIONS

You can use the touch command to update old files to prevent them from being removed by a cleanup program. The system administrator may have a cleanup program run on the system that saves and removes all files older than 90 days. If you do not want a file to be removed because of its age, you can use touch to update its access time.

Often touch is used to update program files when using the programmer's make utility. The make program compares modification times of source and object files to determine if a module needs to be recompiled. By using touch you can force make to recompile a specific file as you wish.

It may also be used to create date and time comparison files. For example, if you need to back up files that are newer than the last backup, you could type "touch .backup" and then use the "find . -newer .backup -print | cpio -oB > /dev/rmt/0m" command to save the files to magnetic tape. The next time you want to perform your backup, just use the find command and all files with a newer date than the .backup file will be saved.

TYPICAL OPERATION

In this activity you use the touch command to create a file with an old date and time instead of the current date and time (SV only). Begin at the shell prompt.

1.  Type touch 0101120071 myfile and press Return. The file myfile is created with the date of Jan 1, 1971 and a time of 12:00.

BSD (Berkeley)
Type touch myfile and press Return. You cannot specify a date for the file or the -a option.

2.  Use the ls -l myfile command to view the information about the new file. The following display shows the information:
     cj> ls -l myfile
     -rwxr-xr--  1  mylogin    tech       0 Jan  1 1971  myfile

Notice in the fifth field the length of the file is 0.

BSD (Berkeley)
The file will have the current date. Turn to the next module.

3.  Type touch -a myfile and press Return. The access time is updated but the modification time remains unchanged.
4.  Type ls -lu myfile and press Return to list the new access time as shown in the following display:
      cj> ls -lu myfile
      -rwxr-xr--  1  mylogin    tech       0 Jun  7 11:39  myfile
5.  To view the modification time type ls -l myfile and press Return. Notice the date and time revealed is the original Jan 1 1971.
6.  Turn to Module 53 to continue the learning sequence.


Previous Table of Contents Next