blazingeagle
blazingeagle

Reputation: 59

moving files with xargs

I want to pipe the output of ls into head and pipe it into mv.

I used the following command on terminal but it isn't working properly.

ls -t Downloads/ | head -7 | xargs -i mv {} ~/cso/

Please do rectify the error. Thanks in advance!

Upvotes: 0

Views: 629

Answers (2)

anubhava
anubhava

Reputation: 785128

It is well documented that parsing ls output is not recommended. You can use this safe approach using find + sort + cut + head + xargs pipeline:

find . -maxdepth 1 -type f -printf '%T@\t%p\0' |
sort -z -rnk1 |
cut -z -f2 |
head -z -n 7 |
xargs -0 -I {} mv {} ~/cso/

Upvotes: 3

Exception_al
Exception_al

Reputation: 1079

Use -I like here :

ls -t Downloads/* | head -7 | xargs -I '{}' mv '{}' ~/cso/

Upvotes: 1

Related Questions