[Gllug] Finding filenames with special characters (was Mass renaming of files)

Tethys tet at accucard.com
Sun May 11 11:00:54 UTC 2003


Simon Morris writes:

>I've also come across some other problem filenames I need to find. 
>
>How do you find filenames containing the following characters
>
>* " | ?

The same way you did before -- just nodify the expression you pass
to find. Assuming you want to rename those characters to something
innoccuous as well, a minor modification to the arguments to find
and tr is all that's needed:

	#!/bin/sh

	tmpfile=/tmp/foo.$$

	trap '/bin/rm "$tmpfile"' 0 INT HUP QUIT TERM

	find "${1:-.}" -name '*[*:"?|]*' -print > "$tmpfile"

	while read file
	do
		newfile=`echo "$file" | tr '[*:"?|]' -`
		mv "$file" "$newfile"
	done < "$tmpfile"

Or if you prefer it on a single line like Rich's perl version:

	find /path/to/dir -name '*[*:"?|]*' -print | while read file; do newfile=`echo "$file" | tr '[*:"?|]' -`; mv "$file" "$newfile"; done

>There are also some directorys and files with no names! How am I
>supposed to find them???

I doubt that. What makes you think that's the case? It's possible
that someone's created filenames consisting solely of whitespace,
but every file has to have a name[1].

Tet

PS. Yes, I know, not strictly true, but for the purposes of this
    discussion it is...


-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list