Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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 http://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.

share|improve this answer
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
1  
@Bentley4: Sorry, I can't reproduce that. -type d lists only directories for me... –  mpy May 29 '13 at 13:03
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
1  
@Bentley4: Can you please try, if lpr accepts more than one file? E.g. lpr file1 file2. This (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
show 8 more comments

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

share|improve this answer
    
This returns find: missing argument to -exec'`. –  Bentley4 May 29 '13 at 13:42
1  
\n should read \; –  mpy May 29 '13 at 13:43
add comment

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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