The Find command is a powerful tool in the UNIX operating system. In addition to being able to locate files, there are many other useful uses for this command. If you can use it to back up files, delete files interactively, enumerate file lists, and so on. As long as the flexible application of these features, then the day-to-day work of system engineers can play a multiplier effect.
First, use the Find command to backup files.
For example, there is a user Oracle in the UNIX operating system. The system will automatically create a subdirectory Oracle for this user in the home directory. Users now want to be able to automatically back up files in their directories to prevent loss of files. As shown in the following illustration, the system engineer is now ready to back up the files under the user directory to the backup directory and rename the file names to backup. This function can be achieved? The answer is certainly yes. You can use the Find command to combine the CP command to achieve this function.
1. The last modification time of the file.
In the file properties of Unix system, there are very important three time truncation, which is the last modification time of file, the last access time of file, and the last modification time of inode node. Once a user has made a change to a file, the last modification time in the file Inode node is updated. It should be noted that directories are also treated as one of the files in the UNIX operating system, and that the modification of the directory, such as creating a directory or changing the name of the directory, is also handled as a modification to the file. When a file is modified, only changes to the file's modification time are made, and the last modification time of the directory is not affected. So if you have a file change in one directory, you only need to back up the changed file at the time of the backup without having to back up any other files that are not changed in this directory.
For backup efficiency, only the files that were modified that day are backed up at the time of backup. The first thing to do during a backup is to find these recently changed files. The idea of this solution is to start with the last modification time of this file. A parameter in the Find command is Mtime, which indicates that the modification time is less than x days. If we set this parameter to 1, the Find command can identify the files that have been modified in the last day. To do this, you only need to back up the files that you found.
2, rename the file.
After the files that need to be backed up are found, the next thing to do is to use the CP command to copy the files to another directory. But one thing you need to do here is to rename the file after you copy it, that is, add. Backup to remind the user that these are backup files and that they cannot be deleted or modified. To implement this feature, you need to use a placeholder for the filename. If you use the CP command to copy files and then bulk change the name of the file, you only need to use CP {} {}.backup. The {} symbol here is the placeholder symbol for the filename. CP {} {}.backup the function of this command is to copy the files to a specific directory, and then rename each file in the previous copy, followed by the. Backup content.
To achieve this, you need to use the command only for the above file backup
Find/home/oracle–mtime 1–exec cp {}/backup/{}.backup \;
After executing this command, the system searches the user's home directory for all files that have been modified for less than 1 days and copies the found files to the/backup directory. and add an extension. backup after each original file. The final \; is a fixed format that must be available. The system engineer then only needs to add this command to cron and let the operating system periodically execute the command at noon or other idle time every day. In this way, the operating system automatically backs up the user's files to prevent accidental damage to the files or damage caused by modifications.
Also note that the Find command in the UNIX operating system finds only those files that have been modified in the last day. And in the Linux operating system, there are mmin parameters, this parameter is in minutes, such as can be found in the last 4 hours or 1 hours of modified files, so as to improve the frequency of backup. This system engineer can adjust the frequency of file backups according to the needs of the users. However, the author believes that in general, file backup once a day can be.