linuxgrepfinddatestamp

Find empty files, if found update files with


Can someone with more Linux knowledge answer this correctly for me. On our web server, we host and run ALOT of web scripts. we control these via Datestamp files, So the script is not over ran, or ran more than once.

A lot of files are all 0 KB. I wanted to know if there is a quick way in Linux to locate the files and update them.

I have located the files using:

find /var/www/vhosts/DOMAINNAME.co.uk/httpdocs -name "datestamp.*" -type f -empty

I have a long list of files, Can i update these with a simple datestamp format: i.e. 20150923114046


Solution

  • You can use the -exec option of find:

    find /var/www/vhosts/DOMAINNAME.co.uk/httpdocs -name "datestamp.*" -type f -empty \
        -exec bash -c 'echo 20150923114046 > {}' \;
    

    To get the timestamp dynamically, use date:

    bash -c 'echo $(date +%Y%m%d%H%M%S) > {}'
    

    To use the last modified timestamp, use the -r option:

    bash -c 'echo $(date +%Y%m%d%H%M%S -r {}) > {}'