I want to find some files and then move them.

I can find the file with:

$ find /tmp/ -ctime -1 -name x*

I tried to move them to my ~/play directory with:

$ find /tmp/ -ctime -1 -name x* | xargs mv ~/play/

but that didn't work. Obviously mv needs two arguments.
Not sure if (or how) to reference the xargs 'current item' in the mv command?


5 Answers 5


Look at Stephane's answer for the best method, take a look at my answer for reasons not to use the more obvious solutions (and reasons why they are not the most efficient).

You can use the -I option of xargs:

find /tmp/ -ctime -1 -name "x*" | xargs -I '{}' mv '{}' ~/play/

Which works in a similar mechanism to find and {}. I would also quote your -name argument (because a file starting with x in the present directory would be file-globed and passed as an argument to find - which will not give the expected behavior!).

However, as pointed out by manatwork, as detailed in the xargs man page:

   -I replace-str
          Replace occurrences of replace-str in the initial-arguments with
          names read from standard input.  Also, unquoted  blanks  do  not
          terminate  input  items;  instead  the  separator is the newline
          character.  Implies -x and -L 1.

The important thing to note is that -L 1 means that only one line of output from find will be processed at a time. This means that's syntactically the same as:

find /tmp/ -ctime -1 -name "x*" -exec mv '{}' ~/play/

(which executes a single mv operation for each file).

Even using the GNU -0 xargs argument and the find -print0 argument causes exactly the same behavior of -I - this is to clone() a process for each file mv:

find . -name "x*" -print0 | strace xargs -0 -I '{}' mv '{}' /tmp/other

read(0, "./foobar1/xorgslsala11\0./foobar1"..., 4096) = 870
mmap(NULL, 135168, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =     0x7fbb82fad000
open("/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26066, ...}) = 0
mmap(NULL, 26066, PROT_READ, MAP_SHARED, 3, 0) = 0x7fbb82fa6000
close(3)                                = 0
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,         child_tidptr=0x7fbb835af9d0) = 661
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 661
--- SIGCHLD (Child exited) @ 0 (0) ---
clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,         child_tidptr=0x7fbb835af9d0) = 662
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 662
--- SIGCHLD (Child exited) @ 0 (0) ---
  • 1
    That assumes file names don't contain newline, single quote, double quote or backslash characters. Commented Sep 16, 2013 at 15:31

Perhaps this command is possible now and wasn't back in 2013, but this works perfectly for me:

ls pattern* | xargs mv -t DESTINATION/

The -t key puts the destination folder first, freeing up mv command to have all last arguments as just the files to be moved.

  • 2
    this was the simplest solution that worked for me
    – ptetteh227
    Commented Aug 22, 2019 at 10:08
  • But that fails if any of the files that match pattern* is of type directory, or the names contain whitespace or single quote, double quote, or backslash characters. You'll notice that I gave a correct answer using mv -t already in 2013. Commented Aug 3, 2020 at 12:15
  • mywiki.wooledge.org/ParsingLs
    – CervEd
    Commented Apr 24, 2021 at 15:49
  • mv: illegal option -- t Commented Sep 23, 2023 at 8:09

With GNU tools:

find /tmp/ -ctime -1 -name 'x*' -print0 |
  xargs -r0 mv -t ~/play/

The -t (--target) option is GNU specific. -print0, -r, -0, while non-standard and originating in GNU are also found in some other implementations like on some BSDs.


find /tmp/ -ctime -1 -name 'x*' -exec sh -c '
  exec mv "$@" ~/play/' sh {} +

Both run as few mv commands as necessary and work whatever characters the file names may contain. The GNU one may have the advantage that find keeps looking for files while mv starts moving the first batch.

Beware that all the files and directories will end up in one directory, beware of clashes if several files in different directories have the same name.

  • This solution is much better performing, since it calls mv once for all arguments (or for all of -L or -n, if supplied). Otherwise, calling mv for each file will get old (and slow) fast.
    – r2evans
    Commented Feb 12, 2018 at 21:25
  • I recommend using the -n flag
    – CervEd
    Commented Apr 24, 2021 at 15:51

I would also like to suggest some alternate tools. Of course find has been around for a very long time so it is a robust tool but I found fd to be a lot easier to work with:

fd -d1 -epdf -S+2m -x mv {} books/

This command would move all 2MB+ pdf files in the current directory to a subfolder called 'books'.

I like fd because it complains a lot less where other tools like find+xargs have more edge cases you need more knowledge to be aware of. For example if the file has a single quote in the name then xargs will have potentially unexpected behavior.


  • To nitpick, doesn't answer the question since xargs isn't used.
    – user598527
    Commented Feb 29 at 9:05

You can try with below command and tested and its worked fine

find /tmp/ -ctime -1 -type f -name "x*" -exec mv -t ~/play/ {} \;
  • No point using the GNU-specific mv -t if you're using \; instead of +. Commented Aug 3, 2020 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .