Piping find output to xargs - problem with spaces in filenames

I have a folder with many subfolders and files in them and need to extract only some of those documents (based on part of the filename).

So I am thinking something like find /location -name *string* | xargs do_something would be the sort of command I need.

however the file where not created with such a situation in mind. File- and foldernames are full of spaces.

$ find /path/to/folder -name *FISI*
/path/to/folder/2012 Sommer/AP S2012 IT GA1 FISI Löser.pdf
/path/to/folder/2012 Sommer/AP S2012 IT GA1 FISI.pdf
/path/to/folder2013_14 Winter/AP W2013 IT GA1 FISI Löser.pdf
...
...

so any command I pipe this through to will not be able to work because it can not identify those files.

Is there any way around this? I.e. If you could batch-replace the spaces in file/foldernames with underscores or such. The list is much longer then what ist visible above, so I need some sort of automation.

Hi @vrms,

How about:

find <location> -name <string> -exec <command> \;

Where:

  • <location> is obvious;
  • <string> is also obvious; and
  • <command> is a command to execute for every result, so could be used to strip spaces using sed and appending the result to a text file, which can, in turn, be read by another script.

IMPORTANT NOTE:

In the <command> to execute, {} is used as placeholder for the complete filename and path. For example:

$ find /tmp -name '*mir*' -exec echo {} \;                                                                                                                                                                                                           

/tmp/keepassxc-mirdarthos.socket
/tmp/keepassxc-mirdarthos.lock
/tmp/kdeconnect_mirdarthos

Won’t something like that work?

true, I haven’t though of that.

$ find /path/to/folder -name *FISI* -exec cp {} /somewhere \;

does this quite nicely

1 Like

In addition to the above, when using xargs with find use the -print0 option on find and --null on xargs.

Best articles on the net about processing linux filenames:

1 Like

great, thx for this.

  1. find -name '*some string*'-print0 | xargs --null do_something (tested with file) works
  2. good food for further reading
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.