Linux find command

kitty99

New Member
Joined
May 30, 2020
Messages
2
Reaction score
0
Credits
20
Hi Guys

I am new to Linux, but really enjoying it.
I am trying to find a method to find all files in a directory and all its sub-directory which were created more than a year ago and copy them over to another folder in the same structure. Eg#

file found is /mail/john/cur must be moved to /backup/john/cur -- > with the directory being created on the fly if they don't exist in the target directory.

Any help will be grateful.

Thanks
Kitty.
 


Kitty,

Instead of using find you might take a look at rsync. I use it to backup my files. In your case you might try this:

cd /mail/john
rsync --delete -avh cur /backup/john

The "-delete" option says if there is a file in /backup/john/cur that is not in /mail/john/cur then delete it. The result is that /mail/john/cur and /backup/john/cur are identical. Also if you want to try this out without actually moving or deleting anything you can add the "--dry-run" option like this:

rsync --dry-run --delete -avh cur /backup/john

Note here that dry-run and delete are preceeded by two hyphens even though here they may look like one.

gwood55
 
Kitty,

I just noticed your "more than a year ago" requirement. The rsync command I gave acts on everything in the directory.

gwood55
 
Kitty,

Here's a find command that may do the job for you:

find /mail/john/ -mtime +365 -exec cp -r {} /backup/john \;

/mail/john/ (with the trailing slash) is the source directory, "-mtime +365" says to look for files older than 365 days and "-exec" says to execute the following command which is "cp" to copy "-r" recursively "{}" each file that the find locates to /backup/john. The "\;" at the end just terminates the command.

gwood55
 
Kitty,

Here's a find command that may do the job for you:

find /mail/john/ -mtime +365 -exec cp -r {} /backup/john \;

/mail/john/ (with the trailing slash) is the source directory, "-mtime +365" says to look for files older than 365 days and "-exec" says to execute the following command which is "cp" to copy "-r" recursively "{}" each file that the find locates to /backup/john. The "\;" at the end just terminates the command.

gwood55


This script is doing well for me in part, but it does not maintain the directory structure from the source to the target. All the files from the find result are put into one directory.
 
Hi all and welcome @kitty99 :)

I am moving this Thread to Command Line where it will attract qualified response, and help others interested.

Participants and Members who are following will be notified.

Cheers

Chris Turner
wizardfromoz
 
Kitty99,

Actually, the command I gave you ends up copying everything twice. As you saw you get all the files in one directory but you also get them in their directories. Not good. The other thing I noticed in working with this is that all the files are copied to the backup directory with today's date and time. I think that's actually worse than getting two copies of everything if you're backing up and archiving files.

So let me ask if this would fit your situation: Would it work to tar all the old files, move the tar file to the backup directory and then extract the files. tar combines files into a single file with a suffix of ".tar" along with their directory attributes such as permissions and dates.

You start with find as before but you pipe the list of files into tar like this:

cd /mail/john
find . -mtime +365 -print0 | tar cvf backup.tar -T -

The "-print0" tells find to display the list of files (called writing the list to stdout). If you ran everything up to but not including the '|' you'd see a list of all the old files. The "|" is called a "pipe" and tells linux to send the list of files to the tar program. The "cvf backup.tar" tells tar to archive all those files into a single file called "backup.tar" The "-T -" (that's hyphen, T, space, hyphen) tells tar to take the list of files from the output of find. In linux jargon we say that the output of find is piped into tar.

If you want to see the list of files in backup.tar use the command "tar tvf backup.tar"

The last step would be to copy the tar file (backup.tar) to the backup directory and extract its contents with "tar xvf backup.tar".

This will give you the same directory structure and all the same permissions and dates.

If there is anything here that isn't clear, please ask.
 

Members online


Top