Bash for file processing [duplicate]
Clash 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?
bash variable basename dirname
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.
add a comment |Â
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?
bash variable basename dirname
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")
andfname=$(basename "$fline")
â Jesse_b
Feb 25 at 21:25
add a comment |Â
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?
bash variable basename dirname
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
bash variable basename dirname
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")
andfname=$(basename "$fline")
â Jesse_b
Feb 25 at 21:25
add a comment |Â
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")
andfname=$(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
add a comment |Â
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 )
add a comment |Â
up vote
0
down vote
Where are you using $img_fil
in your script? Shouldn't the line done
be done < "$img_fil"
?
Might becat img_dump.txt | script.sh
â user unknown
Feb 26 at 3:23
add a comment |Â
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 )
add a comment |Â
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 )
add a comment |Â
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 )
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 )
answered Feb 25 at 21:46
ilkkachu
49.3k672136
49.3k672136
add a comment |Â
add a comment |Â
up vote
0
down vote
Where are you using $img_fil
in your script? Shouldn't the line done
be done < "$img_fil"
?
Might becat img_dump.txt | script.sh
â user unknown
Feb 26 at 3:23
add a comment |Â
up vote
0
down vote
Where are you using $img_fil
in your script? Shouldn't the line done
be done < "$img_fil"
?
Might becat img_dump.txt | script.sh
â user unknown
Feb 26 at 3:23
add a comment |Â
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"
?
Where are you using $img_fil
in your script? Shouldn't the line done
be done < "$img_fil"
?
edited Feb 25 at 21:31
Jesse_b
10.4k22658
10.4k22658
answered Feb 25 at 21:30
user1404316
2,314520
2,314520
Might becat img_dump.txt | script.sh
â user unknown
Feb 26 at 3:23
add a comment |Â
Might becat 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
add a comment |Â
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")
andfname=$(basename "$fline")
â Jesse_b
Feb 25 at 21:25