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.
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 executeslpr
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
.
With just the find
command, use its -exec
option:
find . -type f ! -name ".*" -exec lpr '{}' \;
which passes every matching file name to lpr (the '{}'
is expanded to the each file name in turn).
lpr
can take multiple files, so
lpr ./*
...will print all files in the current directory. For recursiveness, if you have bash 4+, you can use
shopt -s globstar
lpr ./**
If sending directories to lpr
causes problems, you can always use a for loop with a test (the second, recursive one requires globstar to be set):
for f in ./*; do [[ -f "$f" ]] && lpr "$f"; done
for f in ./**; do [[ -f "$f" ]] && lpr "$f"; done