Bash for file processing [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



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?







share|improve this question














marked as duplicate by Jesse_b, Fox, meuh, Kiwy, Community♦ Feb 26 at 18:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
    – nohillside
    Feb 25 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 at 21:25















up vote
0
down vote

favorite













This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



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?







share|improve this question














marked as duplicate by Jesse_b, Fox, meuh, Kiwy, Community♦ Feb 26 at 18:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
    – nohillside
    Feb 25 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 at 21:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



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?







share|improve this question















This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers



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?





This question already has an answer here:



  • Why does my shell script choke on whitespace or other special characters?

    4 answers









share|improve this question













share|improve this question




share|improve this question








edited Feb 25 at 21:28









Jesse_b

10.4k22658




10.4k22658










asked Feb 25 at 21:17









OldManRiver

11




11




marked as duplicate by Jesse_b, Fox, meuh, Kiwy, Community♦ Feb 26 at 18:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jesse_b, Fox, meuh, Kiwy, Community♦ Feb 26 at 18:05


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
    – nohillside
    Feb 25 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 at 21:25













  • 1




    locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
    – nohillside
    Feb 25 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 at 21:25








1




1




locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
– nohillside
Feb 25 at 21:24




locate -0 .jpg .jpeg | xargs -0 ... might help here. Also, find ~ ... might be more accurate.
– nohillside
Feb 25 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 at 21:25





Have you tried quoting the variables inside your command substitutions? i.e. fpath=$(dirname "$fline") and fname=$(basename "$fline")
– Jesse_b
Feb 25 at 21:25











2 Answers
2






active

oldest

votes

















up vote
2
down vote













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 )






share|improve this answer



























    up vote
    0
    down vote













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






    share|improve this answer






















    • Might be cat img_dump.txt | script.sh
      – user unknown
      Feb 26 at 3:23

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    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 )






    share|improve this answer
























      up vote
      2
      down vote













      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 )






      share|improve this answer






















        up vote
        2
        down vote










        up vote
        2
        down vote









        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 )






        share|improve this answer












        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 )







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 25 at 21:46









        ilkkachu

        49.3k672136




        49.3k672136






















            up vote
            0
            down vote













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






            share|improve this answer






















            • Might be cat img_dump.txt | script.sh
              – user unknown
              Feb 26 at 3:23














            up vote
            0
            down vote













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






            share|improve this answer






















            • Might be cat img_dump.txt | script.sh
              – user unknown
              Feb 26 at 3:23












            up vote
            0
            down vote










            up vote
            0
            down vote









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






            share|improve this answer














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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 25 at 21:31









            Jesse_b

            10.4k22658




            10.4k22658










            answered Feb 25 at 21:30









            user1404316

            2,314520




            2,314520











            • Might be cat img_dump.txt | script.sh
              – user unknown
              Feb 26 at 3:23
















            • Might be cat img_dump.txt | script.sh
              – user unknown
              Feb 26 at 3:23















            Might be cat img_dump.txt | script.sh
            – user unknown
            Feb 26 at 3:23




            Might be cat img_dump.txt | script.sh
            – user unknown
            Feb 26 at 3:23


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay