Chris
Chris

Reputation: 7310

Getting duplicate output

I'm trying to write a bash script that looks for a file in one directory and replaces it with one of the same name from a source directory. When I run the find command, it seems to be setting my $path twice

 for f in build-res/$1/*.png; do
     file="$(basename "$f")"
     echo "Looking for $file in $TMP"
     path="$(find $TMP -type f -name "$file")"
     if [[ -z $path ]]; then
             echo "Could not find $file in $TMP"
      else
             echo "Replacing file at $path with $file"
             echo "__path__"
             echo $path
             echo "---"
      fi
 done

Running one iteration of this loop outputs something like

Replacing file at tmp/trx//images/background/background_iphone5.png
tmp/trx//images/background_iphone5.png with background_iphone5.png
__path__
tmp/trx//images/background/background_iphone5.png tmp/trx//images/background_iphone5.png
---

Notice how path repeats itself with a space between. Why would this be happening?

Another note, why is it coming back with // in the path as well? This doesn't seem to be an issue, more so just curious.

Upvotes: 0

Views: 607

Answers (1)

unconditional
unconditional

Reputation: 7666

If you look closely this is not the same path:

tmp/trx//images/background/background_iphone5.png tmp/trx//images/background_iphone5.png

-->

tmp/trx//images/background/background_iphone5.png 
tmp/trx//images/background_iphone5.png

This is the result output of find which finds 2 files with the same name in different subdirectories of /tmp.

Just FYI, if you want to control how deep find can descend into subdirs, there's an option for that:

-maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

Or if you want just a single result you can use

-quit

Exit immediately. No child processes will be left running, but no more paths specified on the command line will be processed. For example, find /tmp/foo /tmp/bar -print -quit will print only /tmp/foo.

Upvotes: 4

Related Questions