Hi

I have searched for a way to replace odd characters in a FOLDER NAME. All search-and-replace issues I have seen, only involves how to make search-and-replace on a FILE och with TEXT INSIDE a FILE. My problem is with the FOLDER NAME.

My case is this:
I have a couple of persons that every now and then uploads files that are contained in a folder. Sometimes the foldernames get screwed up resulting in odd characters such as ^,§,√,≈ and so forth, i.e.

G^ran_F_080122

I would like to replace that "^" to an "o" and get

Goran_F_080122

How do I accomplish that?

-Arndorff

With bash,ksh93 and zsh:

$ ls -l
total 12K
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G^ran_F_080122/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G§ran_F_08023/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G√ran_F_08024/
$ for d in *;do [ -d "$d" ]&&[ ! -d "${d//[§√^]/_}" ]&&mv "$d" "${d//[§√^]/_}";done
$ ls -l
total 12K
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_080122/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_08023/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_08024/

With bash,ksh93 and zsh:

$ ls -l
total 12K
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G^ran_F_080122/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G§ran_F_08023/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G√ran_F_08024/
$ for d in *;do [ -d "$d" ]&&[ ! -d "${d//[§√^]/_}" ]&&mv "$d" "${d//[§√^]/_}";done
$ ls -l
total 12K
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_080122/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_08023/
drwxr-xr-x 2 radoulov radoulov 4.0K 2008-01-29 15:00 G_ran_F_08024/

Thank you very much. That did the trick. One more question. How do I make this script to work in subfolders aswell?

find . -depth -type d -exec sh -c 'p="${1%/*}"
d="${1##*/}"
[ ! -d "$p/${d//[§√^]/_}" ]&&mv "$p/$d" "$p/${d//[§√^]/_}"' - {} \;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.