find and move

Hello I am new to this board.

I was looking for a one line command to find the oldest 10,000 files in a directory and move them.

I tried such things as:

find . -type f | ls -ltr | head -n 10000 -exec mv {} directory/ \;

find . -type f | ls -ltr | head -n 50 | xargs mv directory/ \;

I could create a list and find the 10001 st file and use ! -newer or a for loop but there must be a one line solution.

Thanks in advance

Natasha

I think your logic is flawed with your find example.

find . -type f | ls -ltr | head -n 10000 -exec mv {} directory/ \;

This runs the 'ls -ltr' command on ONE FILE then it runs 'head -n 10000' on that ONE FILE. And since you've passed the rest of your find command arguments (the '-exec mv {} directory/ \;' part) are being passed as arguments to the head command you're getting nowhere fast.

I suggest you pick a cutoff date for old files in that directory. Then use the find command to move those.

Something like this:

/usr/bin/find . -mtime +130 -type f -xdev -exec mv {} /some/other/dir/  \;

So here we've moved all files that haven't been modified in at least 130 days to /some/other/dir/.

The '-xdev' prevents us from searching other filesystems within the current mountpoint. If you wish to search those filesystems then simply remove the '-xdev'.

Please test your command with a different variation of the find example above before you run this on your live system to make sure it does what you expect.

Something like this perhaps:

/usr/bin/find . -mtime +130 -type f -xdev -ls

Simply shows you which files meet the criteria you specified in the find command.

And then run 'man find' and read all that.

Good Luck.

I'm using this following command but its erroring out !!
find /home/xxx/yyy/ . -mtime -7 -type f -exec mv {} /home/xxx/yyy/zzz/

" find: 0652-018 An expression term lacks a required parameter. " -- This is the error i get... My basic requirement here is to move files which are 7 days old !!...

I have tried at the prompt also giving same command but same error is popping up. please suggest what might be the problem here..

don't forget the \; at the end of the exec command. It always errors out unless it is there. You also have an extra . in the command.

find /home/xxx/yyy/ -mtime -7 -type f -exec mv {} /home/xxx/yyy/zzz/ \;

Nopes still erroring out !!!!...
find: 0652-018 An expression term lacks a required parameter.

I might be wrong, but I don't think /usr/bin/find is needed to accomplish what OP wanted:

ls -lrt (or, alternatively "-1rt")

gives a list of files sorted ascendingly by modification date, therefore:

ls -1rt | head -10000 | while read file ; do mv $file /somewhere/else ; done

should do the trick. If there are subdirectories and these should be excluded things get a bit more complicated. Replace the "ls -1rt" above with:

ls -lrt | grep "^-" | sed 's/[<space><tab>][<space><tab>]*/<space>/g' | cut -d' ' -f9

I concede, the second is a bit of a stretch of a "one-liner". Replace "<space>" and "<tab>" with literal tabs and spaces respectively.

bakunin

You can try to add \ to the end of the line. I got it from the following thread in forum.

e.g. /usr/bin/find . -mtime +130 -type f -exec mv {} /some/other/dir/ \\\;

http://nixdoc.net/files/forum/about48417.html

I have a directory tree on my ubuntu which consists of 15000 files and 8Gig of space.

At the command line, I put in:

time find "*.gz"

and it comes back with a response in less than 0.1 seconds.

I do the same thing on my cygwin system and it takes much longer.

My personal machine is much faster. Quad core machine, etc...

Unless I am fooling myself, I am seeing 2000 to 10000 times faster.

any thoughts would be appreciated.