Re: : bash script issue
"Chris F.A. Johnson" <chris-E7bvbYbpR6jSUeElwK9/[email protected]> Wed, 3 Sep 2014 20:03:03 -0400 (EDT)
| Newsgroups | gmane.org.user-groups.linux.tolug |
|---|---|
| Message-ID | <[email protected]> |
This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-1785765450-1409789003=:4216 Content-Type: TEXT/PLAIN; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable On Mon, 18 Aug 2014, Giles Orr wrote: > On 17 August 2014 13:53, William Muriithi <[email protected]> = 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 clea= n 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' >=20 > > If i run either of the below commands on the console, it list the= m > properly as above. > > #LIST=3D`find /home/wmuriithi/images/=C2=A0 =C2=A0-type f | cut -= d"/" -f4| sed 's@ > @\\ @g' | sed 's@[(]@\\(@' | sed 's@[)]@\\)@'` > #LIST=3D`find /home/wmuriithi/images/=C2=A0 =C2=A0-type f | cut -= d"/" -f4|sed -e > "s/.*/'&'/"` > > If I run it on a loop, it breaks the file name into two if the na= me has a space > > for f in $LIST; > =C2=A0 do > > =C2=A0 =C2=A0 =C2=A0mv /home/wmuriithi/images/"$f" /home/wmuriith= i/archive/images/"$DATE"/ > #=C2=A0 =C2=A0 echo "rm $f" >> processed.list > =C2=A0 =C2=A0 =C2=A0echo "rm $f" >> processed.list2 > > =C2=A0 done > > I get this error: > > mv: cannot stat `/home/wmuriithi/images/\'004378858': No such fil= e or directory > mv: cannot stat `/home/wmuriithi/images/(38).jpg\'': No such file= or directory > mv: cannot stat `/home/wmuriithi/images/\'004378858': No such fil= e 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?=C2=A0= What's a > better way of handling such a file name? > > Thanks in advance > > William >=20 >=20 > 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.=C2=A0 > 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.=C2=A0 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=3D/path/to/file echo "${path##*/} --=20 Chris F.A. Johnson, <http://cfajohnson.com> --8323329-1785765450-1409789003=:4216-- -- The Toronto Linux Users Group. Meetings: http://gtalug.org/ TLUG requests: Linux topics, No HTML, wrap text below 80 columns How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists