LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2018, 10:23 PM   #1
AZFXDC
LQ Newbie
 
Registered: Dec 2018
Posts: 7

Rep: Reputation: Disabled
Question Find and mv problem - Centos7


OK,
Sorry to have to ask...
I am trying to move files and directories older than 'x' days from source to destination


For instance, if I have files and directories in:
/home/username/temp/
that are older than 20 days

and want to mv them to:
/home/username/OK

I am trying:

Code:
find /home/username/temp -maxdepth 4 -mtime +20 -exec mv "{}" /home/username/OK/ \;
When I run the above line, /home/username/temp no longer exists...

What am I doing wrong?
Thanks in advance, I apprecaite any help.
-AZFXDC

Last edited by AZFXDC; 12-07-2018 at 10:24 PM.
 
Old 12-07-2018, 10:46 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
Welcome to LQ.

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. Your other thread is being marked for closure.

I would guess that find is finding the directory /home/username/temp and moving it.

If you change mv to cp what does it do?

Last edited by astrogeek; 12-07-2018 at 10:50 PM.
 
Old 12-07-2018, 11:07 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,781

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
That's going to move the /home/username/temp directory itself, and that's the very first thing it will do. You have to think very carefully about the consequences of possibly moving directories and then trying to recurse into those directories. You probably need to use "-mindepth 1" to avoid moving the /home/username/temp directory, and also using "-prune" to avoid trying to recurse into any directory you just moved. You'll still have to deal with the problem of moving any directory that already exists under /home/username/OK. Perhaps this(untested):
Code:
find /home/username/temp -mindepth 1 -maxdepth 4 -mtime +20 -exec mv {} /home/username/OK/ \; -prune
I think that should work. For an item that is not a directory, the "-prune" will do nothing. For a directory that is successfully moved, the "-prune" will prevent trying to recurse into the now nonexistent source directory. For a directory that already exists at the destination, the mv will fail and the "-prune" will not be executed, thus allowing recursion the consider any files within that directory.

Or perhaps this is totally wrong, and you can let me know.

Last edited by rknichols; 12-07-2018 at 11:08 PM.
 
1 members found this post helpful.
Old 12-07-2018, 11:43 PM   #4
AZFXDC
LQ Newbie
 
Registered: Dec 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks to both of you.

@asttrogeek -- Sorry about the double post... I got thrown a major curveball with a lot of pressure and was supposed to take care of a critical system in a very short timeframe and I'm still in a panic so when I noticed the general thread I thought that it would be a better place to ask this question.

To answer your question, replacing with 'mv' with 'cp -r' seemed to produce the desired results in my tests...except, of course, that it was a cp -r and the problem is a lack of space at the source.

Quote:
Originally Posted by rknichols View Post
That's going to move the /home/username/temp directory itself, and that's the very first thing it will do. You have to think very carefully about the consequences of possibly moving directories and then trying to recurse into those directories. You probably need to use "-mindepth 1" to avoid moving the /home/username/temp directory, and also using "-prune" to avoid trying to recurse into any directory you just moved. You'll still have to deal with the problem of moving any directory that already exists under /home/username/OK. Perhaps this(untested):
Code:
find /home/username/temp -mindepth 1 -maxdepth 4 -mtime +20 -exec mv {} /home/username/OK/ \; -prune
I think that should work. For an item that is not a directory, the "-prune" will do nothing. For a directory that is successfully moved, the "-prune" will prevent trying to recurse into the now nonexistent source directory. For a directory that already exists at the destination, the mv will fail and the "-prune" will not be executed, thus allowing recursion the consider any files within that directory.

Or perhaps this is totally wrong, and you can let me know.
@rknichols, this still left the originals in their source and performed what appeared to be a cp-r result...

Right now I've got to sleep on this. I've missed the deadline and there isn't any use in stressing out any more tonight... I'll be back on it in the morning.

Thanks again for your suggestions. I'll keep my eyes on the thread for more options and maybe in the AM I'll have a better idea of what is going on when I'm not 18 hours into the work day.
 
Old 12-07-2018, 11:48 PM   #5
AZFXDC
LQ Newbie
 
Registered: Dec 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Change the mv to echo "{}" - see the problem ?.
@syg00:
I'm afraid I don't see... when checking the man echo I don't see how...

Thanks for your help.
 
Old 12-08-2018, 12:55 AM   #6
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by AZFXDC View Post
@syg00:
I'm afraid I don't see... when checking the man echo I don't see how...
The suggestion is to display the name of the file that was found, rather than moving it somewhere else. It's good practice, generally, to simulate the consequences of your actions before performing those actions. By displaying the file name, you know what will be moved and if necessary modify the find conditions.

Instead of the echo command, you could also use
Code:
ls -ld {}
This way, not only do you see the file name but also the timestamp and permissions of that file.
 
Old 12-08-2018, 04:09 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
that is interesting: you wanted "to move files and directories older than 'x' days from source to destination". You wrote a solution, executed and worked. And now you missed a directory just I don't know why? It was older than x days therefore it was moved.

So I think you want to skip that dir. probably -mindepth 1 is your friend.
 
Old 12-08-2018, 08:54 AM   #8
AZFXDC
LQ Newbie
 
Registered: Dec 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
that is interesting: you wanted "to move files and directories older than 'x' days from source to destination". You wrote a solution, executed and worked. And now you missed a directory just I don't know why? It was older than x days therefore it was moved.

So I think you want to skip that dir. probably -mindepth 1 is your friend.
I think that you are correct. After so many hours of working I was exhausted... There are unique situations in the directory structure which are going to cause additional issues with -mindepth which I won't go into here in too much depth... The files that are being moved are call recordings from our phone system and end up with directories which have identical names (dates) so if I go to that -mindepth I will end up moving a few dozen with the same name... if I go one level up I move everything inside of it including the newer files and one deeper I think I will end up moving the files which meet the date criteria I am defining, all of which are uniquely named, but they will all be in the same location.

Testing now! Wish me luck and thanks, everyone, for your advice. I'll report back.
 
Old 12-10-2018, 03:55 PM   #9
AZFXDC
LQ Newbie
 
Registered: Dec 2018
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks again everyone, mindepth -1 helped but due to the complexity of the file structure I've had to use a compound of all of the examples above.
Thanks for your help!
 
Old 12-15-2018, 06:03 PM   #10
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 529

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Quote:
Originally Posted by AZFXDC View Post
I am trying to move files and directories older than 'x' days from source to destination
Could you state the requirement more clearly? It is possible to have new files in old directories (for example, if the files were created long ago and then later modified). If you move the old directory, all of its contents, including new files, will move with it. Is that really what you need?

Edit: You can also have a new directory containing an old file. You can't move the old file without creating a new directory. As I said, figure out what you actually need, and we can find a way to accomplish it.

Another edit: If this script is to be run again from time to time, carefully consider how you want to handle the case where an even older file or directory of the same name already exists on the destination.

I'm thinking that if your requirement is to move old files into an identical directory structure, you could use find to generate a file list, and then use rsync to move those files. If you don't want to clobber pre-existing files, rsync can rename or relocate them. rsync can remove files from the source after the transfer, but I don't think it will remove directories from the source. If that is part of your requirement, you would have to find another way to do that.

Last edited by Beryllos; 12-15-2018 at 07:19 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problem if I install CENTOS7 Workstation over CENTOS7 Server? Rich Strebendt Linux - Software 5 05-03-2018 11:05 PM
can not find slapd.conf fine in /etc/openldap under Centos7 gbcbooks Linux - Server 7 02-27-2016 01:42 AM
UEFI and Centos7 and install Datcom001 Linux - Laptop and Netbook 9 05-22-2015 02:36 PM
[SOLVED] Network connection problem with centos7 and virtualbox newbc Linux - Virtualization and Cloud 2 08-25-2014 11:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration