bash script issue

Peter plpeter2006-/E1597aS9LQAvxtiuMwx3w at public.gmane.org
Mon Aug 18 06:57:26 UTC 2014


William Muriithi <william.muriithi at ...> writes:
> '004378858 (152).jpg'

One tip: bash has POSIX conventions and one of those is: "any number that
starts with 0 is octal". Yes, OCTAL. Therefore, AVOID using such filenames
most of the time. You can use a prefix notation and all will be fine like
'tel:004378858 (152).jpg' . The space is usually not a problem but one way
to get rid of its parsing complexities in shell scripts is to url escape it.
Space is %20. A simple way to avoid the problems with leading zeros is to
format all numbers to equal length using leading zeros (printf %010d will do
that), then prepend a '1' to each. Now 0001.jpg will become 1000000001.jpg
and can be sorted nicely without being interpreted as octal. At the end,
remove the leading 1. You many have to get more creative than that to
preserve number length and so on.

So one trick when working with crazy file names which may have leading zeros
is to delete leading zeros from the name. You can add them back at the end
in a bulk rename using for example printf %9f $old_name_numeric

Another trick is to escape any shell offending characters like () into
something less offending like digraphs and more. Again, URL escaping will
work fine but will mess up sorting.

Another trick is to reduce all names to a "canonical form" before
processing, which means making them equal length and whatever else is needed
to make them processable and sortable in a way which can be undone later.

There are numerous bash resources on working with such problems at
http://www.shelldorado.com/freenode/BashFaq.html (see in particular the
entries on how can I read a file line by line and so on).

-- Peter (been there, done that, have the marks to show)

--
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





More information about the Legacy mailing list