Skip to main content

Use xargs to copy files that have spaces in their filenames



Copy files with spaces on its filenames in Linux?

Use xargs parameter to copy files that have spaces on its filenames.

In Linux to copy a file which has a space on the filename must be enclosed in a parenthesis.

But if copying multiple files with has a space on their filenames is another story.

It is still an easy task to do with the use of xargs parameter.

Below is an example on how to use xargs to copy files with spaces on their filenames.

Command tested on a Red Hat Linux operating system and works okay.

Here's the command:

find . -iname "*.txt" -print0 | xargs -0 -I  {} cp {} /home/backup/txt_2015

Do not omit -0 and -I parameters or else system will have this error:
Above command will search for files with ".txt" extension and copy to /home/backup/txt_2015 folder. Replace the cp with mv if need to move files instead of copying.

xargs: Warning: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
xargs: {}: No such file or directory

Text below from: man xargs

 -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.

  -0     Input items are terminated by a null  character  instead  of  by
              whitespace,  and the quotes and backslash are not special (every
              character is taken literally).  Disables the end of file string,
              which  is  treated  like  any other argument.  Useful when input
              items might contain white space, quote  marks,  or  backslashes.
              The  GNU  find  -print0  option produces input suitable for this
              mode.

Cheers.. till next time..

=====================================




Comments