LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-11-2016, 08:50 PM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Rep: Reputation: 46
How to move oldest file in one directory to another directory


I have a lot of data I need to move into a processing queue, but I want to do it slowly, like one file a minute. I could cron a command to do this, but I don't know the command.

What I want to do is find the oldest file in the source directory, then move that into the destination directory. Then i'll let cron take it from there.

Anyone know of a one-liner that will do that, that i could paste into a crontab?

Thanks in advance.
 
Old 06-11-2016, 10:17 PM   #2
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: None (src & compile)
Posts: 297

Rep: Reputation: 49
You may wish to be more specific about "oldest file", as if you continue to move files, even one just accessed/written to a second ago will be technically the "oldest file" if it's the only one left. If you're doing something like removing old logs or backup, you might find "find"'s tests more helpful. I wouldn't use cron for this: try a "for" loop with a sleep timer. Something like (for Zsh):
Code:
for filez ( `find $SOURCE_DIR -atime +1 | xargs` ); do
  echo "Moving $filez accessed at least two days ago and waiting 60 seconds..."  
  mv $filez $DEST_DIR
  sleep 60
done
If you want the oldest file in a directory, and only that one file, then 'ls -t | tail -n 1' in that directory will get you that file.

See 'man find', specifically the section on "TESTS", if the above example needs changing to suit your needs. If you were going to perform this removal on a schedule, such as daily or weekly, then the above code could fit well into a cron job, or a script called via a cron job. How to do it will depend on which cron daemon you have. Personally, I prefer fcron.
 
Old 06-11-2016, 10:34 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,832

Rep: Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820Reputation: 1820
Nothing is ever as easy as it seems. Any special characters possible (\f, \n, [[:space:]], ...) ?. Even the following may not be true if (sub-)directories exist.
Quote:
Originally Posted by jayjwa View Post
If you want the oldest file in a directory, and only that one file, then 'ls -t | tail -n 1' in that directory will get you that file.
 
1 members found this post helpful.
Old 06-12-2016, 07:17 AM   #4
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,668

Original Poster
Rep: Reputation: 46
Luckily I already have something scrubbing the names and it's all one directory. The only thing I could come up with on my own was

mv "`ls -t /Source/*.pdf | tail -1`" /destination/

Wasn't sure if there was a better way or what.
 
Old 06-12-2016, 02:59 PM   #5
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 304

Rep: Reputation: 121Reputation: 121
Quote:
Originally Posted by rjo98 View Post
mv "`ls -t /Source/*.pdf | tail -1`" /destination/
Edit: Looking a little closer at your example, I see you are restricting the search to pdf files. In that case, your command is fine, since it is unlikely that you would have a directory with ".pdf" at the end of its name.

Last edited by Beryllos; 06-12-2016 at 03:39 PM. Reason: answered a different question
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script for deleting the oldest directory within a directory kauuttt Linux - Newbie 6 07-14-2013 06:11 AM
Trying to remove oldest file in directory Shimdidly Linux - Newbie 2 07-10-2013 11:11 PM
[ncftp] how to delete oldest directory and file using ncftp commands booyeeka Linux - Newbie 3 08-17-2010 12:23 PM
Move old file to other directory elainelaw Linux - Newbie 1 02-02-2010 03:25 AM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 04:04 AM


All times are GMT -5. The time now is 03:19 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration