Using find to compress new or modified files after a particular date

One of our customers had a unique challenge of moving web servers. Their site was huge, with one directory having over 200GB of images. They opted to do a partial migration, copying over the website as is first before the final switch over.

The final switch over required to copy over only the latest files created or modified after a particular date.

The most efficient way would have been the rsync utility. Unfortunately, this was not an option as we did not have SSH access on the new service, so we had to find an alternate way.

The objective was simple;

  • Find the files
  • Archive/compress them

There are two commands that required to be run, the first one was to search for new or modified files after a particular date and the second one was to create a tar file.

The dry run command looked like so:

find /path/to/folder -type f -newermt '2017-04-01T00:00:00' -print0

Let’s break this down:

  • find /path/to/folder
    • This defines where to search
  • -type f 
    • We’ll be looking for files only, recursively.
  • -newermt ‘2017-04-01T00:00:00’ 
    • The date from where we want to search from
  • -print0
    • This outputs the files so it can be piped into the tar command

For the second objective, we piped in the tar command to accept the output from the first. This would be appended to the original command.

  • | tar -czvf /backup/archive-name.tar -T –
    • Begin piping into the tar
    • The tar will compress and output the progress (verbose)
    • -T – takes in the files to archive from the previous output

Here is the final command:

find /path/to/folder -type f -newermt '2017-04-01T00:00:00' -print0 | tar -czvf /backup/archive-name.tar -T -

I hope this will be useful for anyone with a similar requirement.

SSH Commands to find files on Linux Server

If you have a dedicated server or a VPS – or any Linux machine with SSH access, the following commands may be useful to you if you are looking to find some files quickly. PuTTY is a good telnet/SSH client if you don’t already have one. Let’s get started.

#1: If you are looking for a particular file in /home directory

find /home -name filename

#2: If you are looking for files whose name is starting with “filename” in public_html directories only, you can use the following command:-

find /home/*/public_html -name filename* 

#3: Instead of viewing your search results in command line, you can write it to file by using this:

find /home/*/public_html -name filename* > /home/filename_search.txt

#4: Alternatively, you can send it via e-mail directly from server using the command below. Don’t forget to replace “Subject Here” with your email subject, and “[email protected]” with your email ID.

find /home/*/public_html -name filename* |mail -s "Subject Here" [email protected]

I hope this was useful.