4
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?

Keith Thompson
  • 21,782
  • 6
  • 48
  • 55
Komal-SkyNET
  • 143
  • 1
  • 4
  • 2
    You should consider using `find` with a suitable time predicate (`-mtime`, `-newerXY` etc.) for this task, rather than parsing the output of `ls -lr` – steeldriver Jul 06 '16 at 20:08
  • 1
    what @steeldriver said but more emphatically: [Don't Parse ls](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) – cas Jul 07 '16 at 06:44
  • The `-t` option for `mv` is not in the [POSIX standard](https://www.unix.com/man-page/posix/1p/mv/), and not all versions of `mv` have it — for example, macOS doesn't. – gidds Jun 05 '22 at 10:57

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
Zachary Brady
  • 4,200
  • 2
  • 17
  • 40