find - Recursively replace colons with underscores in Linux -


first of all, first post here , must specify i'm total linux newb.

we have bought qnap nas box office, on box have large amount of data copied off old mac xserve machine. lot of files , folders had forward slashes in name (hfs+ should never have allowed in first place), when copied nas replaced colon.

i want rename colons underscores, , have found following commands in thread here: pitfalls in renaming files in bash

however, flavour of linux on box not understand rename command, i'm having use mv instead. have tried using code below, work files in current folder, there way can change include subfolders?

for f in *.*; mv -- "$f" "${f//:/_}"; done 

i have found can find al files , folders in question using find command follows

files:

find . -type f -name "*:*"  

folders:

find . -type d -name "*:*" 

i have been able export list of results above using

find . -type f -name "*:*" > files.txt 

i tried using command below i'm getting error message find saying doesn't understand exec switch, there way pipe 1 command, or somehow use files exported previously?

find . -depth -name "*:*" -exec bash -c 'dir=${1%/*} base=${1##*/}; mv "$1" "$dir/${base//:/_}"' _ {} \; 

thank you! vincent

so loop code works, in current dir. also, able use find build file files : in filename.

so, you've done this, loop on each line of file, , perform same mv command.

something this:

for f in `cat files.txt`; mv $f "${f//:/_}"; done 

edit:

as pointed out tripleee, using while loop better solution

eg

while read -r f; mv "$f" "${f//:/_}"; done <files.txt 

hope helps.

will


Comments