1

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

2 Answers 2

0

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.

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .