5
ls -lrt |grep 'Jun 30th' | awk '{print $9}' | xargs mv -t /destinationFolder

I'm trying to move files from certain date or pattern or createuser. It doesn't work properly without the -t option.

Could someone please enlighten on xargs -n and -t in move command?

1 Answers1

5

From the mv man page

   -t, --target-directory=DIRECTORY
          move all SOURCE arguments into DIRECTORY

mv's default behavior is to move everything into the last argument so when xargs executes the command it does it like mv /destinationFolder pipedArgs without the -t it would try to move everything into the last arg piped to xargs. With the -t you're explicitly saying to move it HERE.

From the xargs man page

 -n max-args
      Use at most max-args arguments per command line.  Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.

Normally xargs will pass all the args at once to the command. For example

echo 1 2 3 4 |xargs echo
1 2 3 4

executes

echo 1 2 3 4

While

echo 1 2 3 4 |xargs -n 1 echo

executes

echo 1
echo 2
echo 3
echo 4