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?


You could use sponge from MoreUtils:

find . -type f | sponge listoffiles.txt



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
  • upvote ... the OP posted an XY problem – jsotola Feb 2 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.