Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two similar find commands that I basically have the same code for both once the while loop begins. I want to combine them so I only need one find to search through files and directories, instead of one to search directories and one to search files. Here are the two that I want to combine into one line:

find "$ORIG_DIR" -name "*" -type d | while read dname

find "$ORIG_DIR" -name "*" -type f | while read fname
share|improve this question
up vote 0 down vote accepted

You can provide multiple -type options with -o, such as -type d -o -type f in a single command.

find "$ORIG_DIR" -name "*" -type d -o -name "*" -type f | while read file

-o matches all parameters, so the -name is provided twice in the above command.

share|improve this answer
@patrix Rewritten, now it doesn't re-traverse, and works :) – grgarside Jan 20 '14 at 20:46
For the while at the end, will this work: while read dname; fname; do work? – Erick Jan 20 '14 at 21:19
while read fame; do ... done. Might be a good idea to google for "Shell Scripting Primer" – patrix Jan 20 '14 at 21:30

You can use the test command to see whether it is a file or a folder:

find "$ORIG_DIR" | while read thing; do
    if [[ -d "$thing" ]]; then
        # do whatever you want to do with the directory
    if [[ -f "$thing" ]]; then
        # do whatever you want to do with the file
share|improve this answer

Your Answer


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.