Tips & Tricks


Touch command in association with find command can be used to create customized backups.

Suppose you want to create a backup of files that you created or changed in a directory between 9am and 5pm. For this, the very first step is to create two files temp1 and temp2 with timestamps as 9am and 5pm respectively.

$ touch -d “9am” temp1
$ touch -d “5pm” temp2

These commands will create two files temp1 and temp2 with access and modification timestamps as 9am and 5pm respectively.

Let’s cross check these by using stat command:

$ stat temp1
File: `temp1′
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 806h/2054d Inode: 528534 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/YourName) Gid: ( 1000/YourName)
Access: 2013-04-28 09:00:00.000000000 +0530
Modify: 2013-04-28 09:00:00.000000000 +0530
Change: 2013-04-28 19:06:05.982909491 +0530
Birth: –

$ stat temp2
File: `temp2′
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 806h/2054d Inode: 529476 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/YourName) Gid: ( 1000/YourName)
Access: 2013-04-28 17:00:00.000000000 +0530
Modify: 2013-04-28 17:00:00.000000000 +0530
Change: 2013-04-28 19:06:12.090939793 +0530
Birth: –

So we see that timestamps was as expected. Now move to the directory where you want to create the backup of files. Here are the contents of the directory in my case :

$ ls
bfrovrflw.c Cfile.c env.c firstPYProgram.py macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
bufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c

Now, I create a directory named ‘bkup’ and run the following command :

$ find . -newer ../temp1 ! -newer ../temp2 -exec cp ‘{}’ ./bkup/ ‘;’

The -newer and ! -newer options in command above will first find all the files with modification time between 9am and 5pm. Then the -exec option makes sure that the cp command is run for every result (‘{}’) of find command and the file is copied to ./bkup/ folder. The terminating ‘;‘ is the indication that cp command terminates here.

Now, if you see the ‘bkup’ directroy, you’ll find all the backed up files there. Here is what I saw in my case :

$ cd bkup/

$ ls
bfrovrflw.c Cfile.c env.c firstPYProgram.py macro.c my_fopen.c new_printf.c stacksmash.c virtual_func.c
bufrovrflw.c cmd.c file.c helloworld.c main.c my_printf.c prog.c test_strace.c

As all the files were created between 9am and 5pm so all of them were backed up.

Go Back to Menu