Friday, 22 November 2019

printing - How to print out all non-hidden files recursively in a dir using lpr


find . -type f | grep -v '/\.' lists out all non-hidden files in the current dir recursively.


Example of this command given the following file tree


.
├── css
│ ├── base.css
│ └── main.css
├── img
├── index.html
└── js
└── app.js

$ find . -type f | grep -v '/\.'

./index.html
./css/main.css
./css/base.css
./js/app.js

But how do I print all these listed files using lpr?
I tried find . -type f | grep -v '/\.'|lpr but this only prints this list instead of printing each file.



Answer



lpr prints out, what is sent to it via STDIN. So you need to invoke lpr for each file found by find:


find . -type f ! -name ".*" -print0 | xargs -0 lpr


  • -type f searches for files

  • ! is a logical not, hence ! -name ".*" will omit hidden files (with some help from https://superuser.com/a/101012/195224)

  • -print0 separates the individual filesnames with \0 so that this will also work with file names with white spaces in it.

  • xargs finally executes lpr with the list of filesnames it receives (-0 again tells that \0 is used as a delimiter).


This command will list only non-dotfiles, but also those in dotdirs.


If you also want to exclude dotdirs, extend the find command to


find . -type f ! -regex ".*/\..*" ! -name ".*"

And finally, as some versions of lpr have obviously a problem with empty files, omit these also:


find . -type f ! -regex ".*/\..*" ! -name ".*" ! -empty



As a sidenote: To get a nicer layout of your printout (includes file name) you should consider to replace lpr by a2ps.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...