Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

2 Answers 2

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
1  
@patrix Rewritten, now it doesn't re-traverse, and works :) –  George Garside Jan 20 at 20:46
    
For the while at the end, will this work: while read dname; fname; do work? –  Erick Jan 20 at 21:19
    
while read fame; do ... done. Might be a good idea to google for "Shell Scripting Primer" –  patrix Jan 20 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
    fi
    if [[ -f "$thing" ]]; then
        # do whatever you want to do with the file
    fi
done
share|improve this answer

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.