Moving Files with Xargs

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!

4

2 Answers

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/

Use -I like here :

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.