2

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.

Bentley4
  • 1,908
  • 8
  • 23
  • 37

3 Answers3

4

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.

mpy
  • 27,002
  • 7
  • 85
  • 97
  • The command `find . -type f ! -name ".*" -print0 | xargs -0 lpr` fails, I get `lpr: No file in print request.`. And using this command without the `lpr` part lists out dotfiles as well so it doesn't seem to be the right command. – Bentley4 May 29 '13 at 12:35
  • 1
    @Bentley4: The command I posted works here as intended with bash, tcsh and zsh. What's stange about your edited question: You are using `-type d` but find lists files not directories. Are you using GNU Linux? – mpy May 29 '13 at 12:44
  • I am using Ubuntu. I got that command from [this thread](http://stackoverflow.com/questions/4916588/how-do-i-filter-dotfiles-out-recursively-using-find-in-bash) btw. Apparently it works for those other pple as well. – Bentley4 May 29 '13 at 12:49
  • 1
    @Bentley4: Sorry, I can't reproduce that. `-type d` lists only directories for me... – mpy May 29 '13 at 13:03
  • Oh, I forgot I adapted that command. I'm such a dummy sometimes, sorry! You just need to replace d with f so you get this: `find . -type f | grep -v '/\.'`. – Bentley4 May 29 '13 at 13:07
  • 1
    @Bentley4: Ok, that makes sense now. Back to my command. Are you sure `find . -type f ! -name ".*"` lists dotfiles? What it does is to list non-dotfiles in dotdirectories. But obviously you want to omit dotdirs, too. That's my fault, although it's not explicitely stated in the question it should be clear from the `grep -v` part... I'll edit my answer. – mpy May 29 '13 at 13:17
  • 1
    Indeed, I meant also to omit dot directories. Sorry for not being clear enough on this. – Bentley4 May 29 '13 at 13:19
  • @Bentley4: Hope, it works now. – mpy May 29 '13 at 13:24
  • `find . -type f ! -regex ".*/\..*" ! -name ".*"` lists all non-hidden files and directories just like you said. However `find . -type f ! -regex ".*/\..*" ! -name ".*" -print0 | xargs -0 lpr` still doesn't print and returns `lpr: No file in print request.` – Bentley4 May 29 '13 at 13:27
  • 1
    @Bentley4: Can you please try, if `lpr` accepts more than one file? E.g. `lpr file1 file2`. This (http://unix.stackexchange.com/q/73469/33390) hints that it can also have to do something with _empty_ files... – mpy May 29 '13 at 13:32
  • It does accept multiple files. – Bentley4 May 29 '13 at 13:36
  • @Bentley4: Ok, my last try (edited answer), to omit also empty files, as they seem to make troubles on some systems. – mpy May 29 '13 at 13:42
  • Nice catch on the empty files! Thanks so much. I'll take a look at `a2ps` since the output is quite ugly. – Bentley4 May 29 '13 at 13:50
1

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).

manyon
  • 33
  • 5
1

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
evilsoup
  • 13,097
  • 3
  • 59
  • 80