Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 06-02-2005, 07:56 PM   #1
LQ Newbie
Registered: Jun 2005
Posts: 2

Rep: Reputation: 0
Question find and move most recent files in dir

1. What I'd like to be able to do is to look thought a directory containing files like:


2. using the name of the file, be able to move files one at a time, starting with the oldest (foo.123), and working it's way through all the files until all are processed.

So, this is what I have so far which is moving the first file fine. I'd like to know how to make it run until it has processed all the files in the directory...

#! /bin/csh
set lastfile = NULL
foreach i (`ls -t foo*`)
if ($lastfile == NULL) then
set datafile = $i
set lastfile = $i
if ($lastfile != $datafile) then
mv $lastfile testdir

any help is much appreciated.


- GD
Old 06-02-2005, 08:19 PM   #2
Registered: Sep 2004
Location: western massachusetts
Distribution: fedora core 3, Suse 10
Posts: 877

Rep: Reputation: 35
you can look into using the find command with the options -mtime -atime and -ctime. Here are the descriptions for each as stated in Sams Teach Yourself Shell Programming in 24 Hours.

-mtime - fines files last modified more than, exactly, or fewer than n days ago

-atime - find files last accessed more than, exactly, or fewer than n days ago.

-ctime - find files that were last changed more than, exctly, or fewer thatn n days ago.

you could probably do something like find -{c/m/a}time expression -exec mv foo new destination in a loop to accomplish this. You could also look into xarg to execute the command after you get the list of file names to move. something like find (options and parameters) | xarg mv (options and parameters) check out man for find and xarg for more info. And by the way you should have posted this in the programming section and might have gotten more reponses; however don't double post. If a moderator sees this they will probably move it to programming forum anyways.
Old 06-14-2005, 01:03 PM   #3
LQ Newbie
Registered: Jun 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you for your help. I ended up doing the following:
foreach i (`ls abc*`)
set datafile = $i
echo $datafile
mv $datafile ../post

foreach i (`ls xyz*`)
set datafile = $i
echo $datafile
mv $datafile ../post


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
cannot using FTP move command to move files adrianmak Linux - Networking 4 04-21-2009 01:01 PM
Script to find all picture files on a HDD and move them scoops98 Linux - General 8 02-27-2007 03:40 AM
deleting recent files Santas Linux - General 2 01-19-2004 07:26 AM
recent debian distro/base files/ net install ? mvbv-linux Debian 1 01-08-2004 11:15 AM
When I try to move a file to a dir it says No Space Left on Device. Help KrazyKid Linux - Software 1 06-16-2002 10:06 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:20 AM.

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