Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Find & move files sorted by date [solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 7:25 am    Post subject: Find & move files sorted by date [solved] Reply with quote

I would like to use the find command to sort by date a directory with many files. How can I?

Thanks.


Last edited by chrooted on Wed Oct 15, 2008 10:55 am; edited 3 times in total
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Wed Oct 15, 2008 7:33 am    Post subject: Reply with quote

Something like this
Code:
find . -type f -maxdepth 1 | xargs ls -lt
?
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 7:47 am    Post subject: Reply with quote

thanks, but in the reversed order, can we? :)
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Wed Oct 15, 2008 7:52 am    Post subject: Reply with quote

chrooted wrote:
thanks, but in the reversed order, can we? :)
Yes we can :-)
Use "-r" too, meaning "ls -ltr"
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 7:53 am    Post subject: Reply with quote

OK thanks, but how can I combine find with ls ?

Like this? find . -type f -maxdepth 1 | xargs ls -ltr
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Wed Oct 15, 2008 8:59 am    Post subject: Reply with quote

chrooted wrote:
OK thanks, but how can I combine find with ls ?

Like this? find . -type f -maxdepth 1 | xargs ls -ltr
yes
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 9:07 am    Post subject: Reply with quote

toralf wrote:
chrooted wrote:
OK thanks, but how can I combine find with ls ?

Like this? find . -type f -maxdepth 1 | xargs ls -ltr
yes

toralf,

can you help me with this one, please?

I would like to move the first 1000 files in a directory sorted by date.

find . -type f -maxdepth 1 | xargs ls -latrh | head -n 1000 -exec mv {} /test \;

head: invalid option -- e

Try `head --help' for more information.

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

xargs: ls: terminated by signal 13


What's the problem? :(

I mean, I would like to put the move command after find . -type f -maxdepth 1 | xargs ls -lt
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Wed Oct 15, 2008 9:20 am    Post subject: Reply with quote

Hhm, you should really try to use "man head" "man find" ;) - but nevertheless here's a simple example :
Code:
find . -type f -maxdepth 1 | xargs ls -latrh | head -n 1000 | awk ' { print $9 } ' | while read line; do echo mv $line /test; done
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 9:30 am    Post subject: Reply with quote

toralf wrote:
Hhm, you should really try to use "man head" "man find" ;) - but nevertheless here's a simple example :
Code:
find . -type f -maxdepth 1 | xargs ls -latrh | head -n 1000 | awk ' { print $9 } ' | while read line; do echo mv $line /test; done


Used the man, but with no luck.

find . -type f -maxdepth 1 | xargs ls -latrh | head -n 10 | awk ' { print $9 } ' | while read line; do echo mv $line /test; done

find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).

Please specify options before other arguments.

mv /test
mv /test
mv /test
mv /test
mv /test
mv /test
mv /test
mv /test
mv /test
mv /test

ls -l /test
total 0

This is just not working :(
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Wed Oct 15, 2008 9:42 am    Post subject: Reply with quote

To avoidan error mesg from find command, prefer
Code:
find . -maxdepth 1 -type f
and replace (obviously) the "." by that directory where you expect the 1000 files
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 10:06 am    Post subject: Reply with quote

toralf wrote:
To avoidan error mesg from find command, prefer
Code:
find . -maxdepth 1 -type f
and replace (obviously) the "." by that directory where you expect the 1000 files


Thanks. Did that, the point is that the destination folder : /test is blank :(
Back to top
View user's profile Send private message
neysx
Retired Dev
Retired Dev


Joined: 27 Jan 2003
Posts: 795

PostPosted: Wed Oct 15, 2008 10:45 am    Post subject: Reply with quote

1. You should read the man page of each command to understand what it does and understand what you're doing.
2. To list files that are going to be moved, use
Code:
find /path/to/dir/ -maxdepth 1 -type f -print0 | xargs -0 ls -Qltr | head -n 1000 | less

find selects all file entries in specified directory and passes the list to ls via xargs (find's -print0 and xargs's -0 are used in case you have file names with spaces). ls sorts the list on mtime and pipes it into head which selects the first 1000.
If you what you see matches the 1000 files you want to move,
3. Move'em with mv in one go:
Code:
find /path/to/dir/ -maxdepth 1 -type f -print0 | xargs -0 ls -Q1tr | head -n 1000 | xargs mv -iv -t /path/to/dest/

Important: the first ls command uses -l (lower case L) to list all details, the second one uses -1 (digit one) to display the file name alone so there's no need to cut it out with awk or cut.
Replace /path/to/dir with the path to the directory that contains all the files and /path/to/dest with the directory you want to move the files to.

Hth
Back to top
View user's profile Send private message
chrooted
n00b
n00b


Joined: 06 Jun 2008
Posts: 55

PostPosted: Wed Oct 15, 2008 10:52 am    Post subject: Reply with quote

neysx wrote:
1. You should read the man page of each command to understand what it does and understand what you're doing.
2. To list files that are going to be moved, use
Code:
find /path/to/dir/ -maxdepth 1 -type f -print0 | xargs -0 ls -Qltr | head -n 1000 | less

find selects all file entries in specified directory and passes the list to ls via xargs (find's -print0 and xargs's -0 are used in case you have file names with spaces). ls sorts the list on mtime and pipes it into head which selects the first 1000.
If you what you see matches the 1000 files you want to move,
3. Move'em with mv in one go:
Code:
find /path/to/dir/ -maxdepth 1 -type f -print0 | xargs -0 ls -Q1tr | head -n 1000 | xargs mv -iv -t /path/to/dest/

Important: the first ls command uses -l (lower case L) to list all details, the second one uses -1 (digit one) to display the file name alone so there's no need to cut it out with awk or cut.
Replace /path/to/dir with the path to the directory that contains all the files and /path/to/dest with the directory you want to move the files to.

Hth


Thanks, this is working!

Thanks a lot, neysx.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum