bash script issue

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Sep 4 00:03:03 UTC 2014


On Mon, 18 Aug 2014, Giles Orr wrote:
> 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.

   There's no need for basename, cut, or any external command:

path=/path/to/file
echo "${path##*/}


-- 
Chris F.A. Johnson, <http://cfajohnson.com>


More information about the Legacy mailing list