Access the oldest file in a dir


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Access the oldest file in a dir
# 1  
Old 10-14-2010
Access the oldest file in a dir

I have to move files (one by one) from one dir to another, in such a way that the oldest file should be moved first followed by the latest file. The source dir (from where I am moving files) may contains a minimum of 20K files at any point of time. I am not able to use "ls -ltr" as it throws error - Arg list is too long.Smilie
Kindly help me in solving this.
# 2  
Old 10-14-2010
1) What platform?
2) Why one by one?
3) We have no clue to help you : What is the delta between oldest and newest?
# 3  
Old 10-14-2010
Quote:
that the oldest file should be moved first followed by the latest file
Are you moving just two files, or are you moving 20k files?

Are you moving files in the unix sense or copying?


You cannot get the error message "Arg list is too long" by typing "ls -ltr" . What is in your script?
# 4  
Old 10-15-2010
Hi Vbe & methyl,

The platform is UNIX Solaris. I will be keep on receiving files from another server to the source dir. I want to move files one by one to the destination dir becoz, there is a daemon application which will be always pooling this destination dir for any files and if a file is found, it starts processing it. This application is taking more time to start when the no: of files in its pooling dir (destin dir) is more. So, in my script I check if the no: of files in the destin dir is < 50 and if so, I have to start moving files (the file with oldest timestamp first, and so on, and file with latest timestamp at last).
At present, I use the following code:
Code:
while [ 1 ];
do
for i in `ls -t $sourceDir`
do
  if [ `find $DestinDir -name "*.txt" | wc -l` -lt 50 ]
     mv $sourceDir/*.txt $DestinDir
  fi
done
done

But this is failing with the error- Arg list too long @ for i in `ls -t $sourceDir`. Please suggest me ways to handle this.
And the delta between the oldest and newest file may be 2 or 3 days, as in worst case we may be having many backlog files in the sourceDir when the daemon application is kept down for maintenance.

---------- Post updated at 07:38 PM ---------- Previous update was at 07:35 PM ----------

small mistake in the code....

mv $sourceDir/$i $DestinDir

Last edited by vbe; 10-15-2010 at 12:01 PM.. Reason: code tags please
# 5  
Old 10-15-2010
Can you do a:
Code:
ls -ltr >$HOME/list_2mv.txt

?
As for
Quote:
Arg list too long @ for i in `ls -t $sourceDir`.
It depends on how many... you could use xargs
# 6  
Old 10-15-2010
I'd love to know where this "for i in open-ended-list" construct comes from.

Anyway, we can turn the script from a "for" loop to a "while" loop.
I'd suggest inserting some "sleep" statements in this script because it will loop at a ridiculous rate.
As an idea, try getting out of the inner loop with a "break" command when the destination directory has reached the limit. If you don't there is a good chance that you will process your files list out of order.
There is no code to deal with embryonic files. I hope that they arrive in $sourceDir under a name other than *.txt, then get renamed?

Code:
while true
do
    ls -t "${sourceDir}"/*.txt 2>/dev/null | while read filename
    do
        if [ `find "${DestinDir}" -name "*.txt" | wc -l` -lt 50 ]
        then
            if [ -f "${sourceDir}/${filename}" ]
            then
                mv "${sourceDir}/${filename}" "${DestinDir}"
            fi
        else
            # Stop processing the list if the destination directory is full
            break
        fi
        sleep 1
    done
    sleep 30
done

# 7  
Old 10-17-2010
Something like

Code:
while :
do
let CurrentNum=`find "$DestinDir" -name "*.txt" | wc -l`
while [ $CurrentNum -lt 50 ]
do
{ read filename
mv $filename $DestinDir/${filename##*/}
let CurrentNum++
done } < `ls -rt | egrep -e "txt$"`
done

I have just some doubt about
- where the read should be placed
- where { } should be added
- where the < `ls -rt | egrep -e "txt$"` should be placed

Maybe the read should be moved at the while condition level..
while [ $CurrentNum -lt 50 ] && read filename
...
done < `ls -rt | egrep -e "txt$"`

if you have many files, the wildcard expansion may failed with
Code:
ls -t "${sourceDir}"/*.txt

so i went for a
Code:
`ls -rt | egrep -e "txt$"`

instead.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Access Issue - dir and script

Hi All, I got access in UNIX box from our client. I am able to create folder, files, script etc.. in that folder. here is the issue: my login/base folder is = /home/ace I have created 2 folders within it Script ScriptLog in Script folder i have created 2 .ksh file ... (1 Reply)
Discussion started by: ace_friends22
1 Replies

2. HP-UX

NFS Client unable to access shared DIR's

I ve a client : <clientname> and a server: <servername> Both of them running: HPUX B.11.31 SERVER outputs: I ve shared a folder in <servername> called /test and the O/P for # exportfs - /test root=<clientname>,rw=<clientname>,ro "" # showmount -e export list for... (6 Replies)
Discussion started by: Amit Kulkarni
6 Replies

3. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

4. Linux

SFTP user access restriction to his home dir

Hi Friends, I have installed a FTP Server on my Linux machine (Fedora 11). I want the ftp users to be restricted to their own home dir using sftp. But the said condition is met when the user logs in using ftp over port 21 and when the user logs in using sftp i.e. protocol 22, he/she has... (4 Replies)
Discussion started by: pashy
4 Replies

5. UNIX for Dummies Questions & Answers

Will i see a file/dir I don't have read access to

Hi all, if I have a dir with a mixture of files and directories in it and one of the directories *only* has read permission for the owner and I am not the owner - will I see it with an 'ls -la'. I do not have access to a unix box at present to try this out. Any thoughts gratefully received (1 Reply)
Discussion started by: ajcannon
1 Replies

6. UNIX for Dummies Questions & Answers

Restrict user access to their home dir

Hi! i'm using FreeBSD 6.2 and hosting my pc to frens in particular of sensitive information being saved to the PC, i would like to know is it possible for me to restrict user access to their /home dir. only? and also, i wanted to restrict them listing files under /etc thanks all! (10 Replies)
Discussion started by: rdns
10 Replies

7. UNIX for Advanced & Expert Users

user has access only to one dir

Hello i want to ask how can i let a newly created user to access only one directory and not any other directory at all.: (1 Reply)
Discussion started by: learn82
1 Replies

8. Shell Programming and Scripting

User dir access using ~ in sh

I am writing code to copy file if user dir exists. Code snippet : #!/bin/sh if then cp ~user1/file file else cp ~user2/file file fi This code works if shell is ksh but not if shell is sh. Can anyone suggest how this can work in sh script? Thanks, Ashish (1 Reply)
Discussion started by: Ashishp
1 Replies

9. UNIX for Dummies Questions & Answers

Setting up FTP access to my Tomcat Dir

Hi Guys, Im trying to set up FTP to my Apache Tomcat dir on my server so my web designer can FTP in and look at things in a live environment. However at first I couldn't write to the dir, so i change some permissions, and now I cant access it at all! Here is my LS -all output: ... (4 Replies)
Discussion started by: boarderstu
4 Replies

10. UNIX for Dummies Questions & Answers

dir access

How do I give specific user the ablity to create, delete, rename, and alter in a specific directory. I am using redhad 7.2 and would like to give a specific user the rights to a dirc in the main www directory. (1 Reply)
Discussion started by: macdonto
1 Replies
Login or Register to Ask a Question