Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash scripting challenge

Tags:

bash

scripting

I need to write a bash script that will iterate through the contents of a directory (including subdirectories) and perform the following replacements:

  • replace 'foo' in any file names with 'bar'
  • replace 'foo' in the contents of any files with 'bar'

So far all I've got is

find . -name '*' -exec {} \;

:-)

like image 515
Dónal Avatar asked Jan 22 '10 18:01

Dónal


2 Answers

With RH rename:

find -f \( -exec sed -i s/foo/bar/g \; , -name \*foo\* -exec rename foo bar {} \; \)
like image 100
Ignacio Vazquez-Abrams Avatar answered Oct 01 '22 14:10

Ignacio Vazquez-Abrams


find "$@" -depth -exec sed -i -e s/foo/bar/g {} \; , -name '*foo*' -print0 |
while read -d '' file; do
    base=$(basename "$file")
    mv "$file" "$(dirname "$file")/${base//foo/bar}"
done
like image 36
ephemient Avatar answered Oct 01 '22 15:10

ephemient