0

So for some reason when I do something like

find $PWD -type f > listoffiles.txt

To create a list of files in current directory and save it to a list, The file "listoffiles.txt" itself is somehow included in that, which tells me that it's instantiated prior to STDOUT of my pipe. How exactly can I get it to not do that?

Tom
  • 153

2 Answers2

2

You could use sponge from MoreUtils:

find . -type f | sponge listoffiles.txt

Related:

steeldriver
  • 81,074
1

The output file is created before find runs. You could create it somewhere else, say:

$ find $PWD -type f > /tmp/listoffiles.txt

or grep it away:

$ find $PWD -type f | grep -v "^..listoffiles.txt" > /tmp/listoffiles.txt

or ask find to ignore it:

$ find $PWD -type f -not -name listoffiles.txt > /tmp/listoffiles.txt