0

I am writing a bash script to move all images into a central file.

I create the list of image files with:

img_fil='/home/files/img_dump.txt'
locate -i image | grep \.jpg > "$img_fil"
locate -i image | grep \.jpeg >> "$img_fil"
locate -i image | grep \.gif >> "$img_fil"
locate -i image | grep \.tif >> "$img_fil"
locate -i image | grep \.png >> "$img_fil"

But when I start processing the dump file for this, most of the paths contain blanks so this does not work:

while read -r fline
do
   if [ ! -e "$fline" ]; then
      echo "F=> $fline"
      mv "$fline" "$img_dir"
   else
      fpath="$(dirname $fline)"
      fname="$(basename $fline)"
      echo "F=> $fname P=> $fpath"
   fi 
done

The dirname and basename always parse at the blanks so will not process right.

How do I get this to work?

jesse_b
  • 37,005
  • 1
    locate -0 \.jpg \.jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate. – nohillside Feb 25 '18 at 21:24
  • Have you tried quoting the variables inside your command substitutions? i.e. fpath=$(dirname "$fline") and fname=$(basename "$fline") – jesse_b Feb 25 '18 at 21:25

2 Answers2

2
fpath="$(dirname $fline)"
fname="$(basename $fline)"

Here, you need to quote $fline inside the command substitution. (Outside doesn't matter since it's in an assignment.) So:

fpath=$(dirname -- "$fline")

or

fpath=${fline%/*}

(Though note the minor differences between dirname/basename and the parameter expansions, see: dirname and basename vs parameter expansion )

ilkkachu
  • 138,973
0

Where are you using $img_fil in your script? Shouldn't the line done be done < "$img_fil"?

jesse_b
  • 37,005
user1404316
  • 3,078