bash script issue

Giles Orr gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Mon Aug 18 13:30:44 UTC 2014


On 17 August 2014 13:53, William Muriithi <william.muriithi-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org>
wrote:

> I have a list of files that have horrible names and it is forcing me
> to improve the intelligence of a script I have been using to clean up
> the system after the images have been processed. Sample names are as
> below
>
> '004378858 (152).jpg'
> '004384040.jpg'
> '004382728.jpg'
> '004383192.jpg'
> '004375871.jpg'
> '004378858 (179).jpg'
> '004378858 (155).jpg'
> '004378858 (187).jpg'
>
>
> If i run either of the below commands on the console, it list them
> properly as above.
>
> #LIST=`find /home/wmuriithi/images/   -type f | cut -d"/" -f4| sed 's@
> @\\ @g' | sed 's@[(]@\\(@' | sed 's@[)]@\\)@'`
> #LIST=`find /home/wmuriithi/images/   -type f | cut -d"/" -f4|sed -e
> "s/.*/'&'/"`
>
> If I run it on a loop, it breaks the file name into two if the name has a
> space
>
> for f in $LIST;
>   do
>
>      mv /home/wmuriithi/images/"$f" /home/wmuriithi/archive/images/"$DATE"/
> #    echo "rm $f" >> processed.list
>      echo "rm $f" >> processed.list2
>
>   done
>
> I get this error:
>
> mv: cannot stat `/home/wmuriithi/images/\'004378858': No such file or
> directory
> mv: cannot stat `/home/wmuriithi/images/(38).jpg\'': No such file or
> directory
> mv: cannot stat `/home/wmuriithi/images/\'004378858': No such file or
> directory
> mv: cannot stat `/home/wmuriithi/images/(53).jpg\'': No such file or
> directory
>
> Now, I am curious, why do I get different behaviours above?  What's a
> better way of handling such a file name?
>
> Thanks in advance
>
> William
>

Others have made some cogent points about the use of parentheses and spaces
in file names that I won't repeat, except to say I agree.  Another thing
I'd strongly suggest is using the "basename" command instead of "cut": your
use of cut relies on knowing how deep in the directory structure you are,
and that makes the script very fragile.  basename just grabs the terminal
filename and discards all the directories regardless of how deep you are in
the directory tree.

-- 
Giles
http://www.gilesorr.com/
gilesorr-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gtalug.org/pipermail/legacy/attachments/20140818/3351bfb8/attachment.html>


More information about the Legacy mailing list