Search code examples
bashxargscommand-substitutionprocess-substitution

How to perform multiple commands on a list of files in bash (xargs?)


I have a file that contains a list of files, and I want to perform two commands on each file.

Contents of files.txt:

file1
file2
file3

The commands I want to perform on each file are (for example) ls -s and du.

I want the output to end up being like this:

<ls size> <du size> file1
<ls size> <du size> file2
<ls size> <du size> file3

I know how to do this using a bash script, but is there a way to easily do this on one line, using process substitution or something?

I thought I could do something like this, but it didn't work:

cat files.txt | tr '\n' '\0' | xargs -0 -I{} cat <(du {} ) <(ls -s {})

That came up with these errors:

du: cannot access `{}': No such file or directory
ls: cannot access {}: No such file or directory

Is there a way to do this?


Solution

  • You can run a sh with xargs, in which you can put multiple commands, for example:

    xargs -0 -I{} sh -c 'du {}; ls -s {}'