2

I'm seeing the weirdest results here and was hoping somebody can explain this to me.

So, I'm using a find command to locate all files of type .log, and piping the results of that command to a python script. ONLY the first result of the find command is being piped to xargs, or xargs is receiving all results and passing them as a string to the python script.

Example:

# Find returns 3 .log files
find . -name "*.log"
    file1.log
    file2.log
    file3.log

# xargs only runs the python script for first found file (or all 3 are being piped to script as a single string, and only first result is read in)
find . -name "*.log" | xargs python myscript.py -infile 
    Success: parsed file1.log

What I want to happen is the python script to run for all 3 files found.

find . -name "*.log" | xargs python myscript.py -infile 
        Success: parsed file1.log
        Success: parsed file1.log
        Success: parsed file1.log
user797963
  • 2,907
  • 9
  • 47
  • 88

1 Answers1

0

A safer way to do this is as follows:

find . -name "*.log" -print0 | \
    xargs -0 -I {} python myscript.py -infile {}

When passing file names from find, it is very important to use the -0 or -d option to set the separator to \0 (null). Filenames can not contain / or \0 characters, so it guarantees a safe use of the filename.

With xargs, you must supply -0 to inform of the use of \0 separators. You also need:

  • "-L 1" if you just need the filename as the last argument.
  • "-I {}" to pass one or more to the command anywhere in the command.
James Risner
  • 5,451
  • 11
  • 25
  • 47