0

How do you replace the phrase "Full Tilt" with "FullTilt" in all files in a directory and subdirectories under that directory (and all sub-directories under those subdirectories and so on)?

  • Paste some part of code you have at least and describe where you have a problem. – Grzegorz Krukowski Sep 13 '13 at 15:47
  • 1
    with the great tool `find`. – urzeit Sep 13 '13 at 15:52
  • possible duplicate of [Rename file names in current directory and all subdirectories](http://stackoverflow.com/questions/4593620/rename-file-names-in-current-directory-and-all-subdirectories) – DanteTheEgregore Sep 13 '13 at 16:17
  • Duplicate of http://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string – iamauser Sep 13 '13 at 16:18
  • Are you renaming files or editing the contents of the files? Your title suggests renaming; your question suggests editing ('in all files in a directory'). If you said 'in all file names in a directory', your question would be consistent with your title. – Jonathan Leffler Sep 13 '13 at 16:26

4 Answers4

1

Haven't tested it, but you should be able to do it more or less like this:

First, create a script called 'rename.sh', make it executable and save it with the following content:

#! /bin/bash
folder=`dirname $1`
oldname=`basename $1`
newname=`echo "$oldname" | sed 's/Full Tilt/FullTilt/g'`
mv "$folder/$oldname" "$folder/$newname"

Then, run the following command from inside your directory:

find -name "*Full Tilt*" -type file -exec "rename.sh" "{}" ";"

It can probably be done with a one-liner on find (instead of using a script), but it won't be readable and it will contain too many escapes to follow...

Barak Itkin
  • 4,872
  • 1
  • 22
  • 29
0

Something like:

find . -name '*xx*' -print | sed -e 's/^\(.*\)xx\(.*\)$/\1xx\2 \1yy\2 /g' | xargs -n2 mv

Where 'xx' ist the search string for the file names. sed replaces the xx with yy and pipes that to xargs xargs takes now (-n2) two parameters (old and new name) and execute mv with both parms.

That is only the start point, try it in a safe environment :-)

You have first rename the files ( -type=f ) after that you rename the path ( -type=d ). If you change this order the mv will fail while the directory is renamed while changing the next file name :-)

If there are spaces in the file names, you have to use print0 instead of print. But then you also have to change the sed part... I think the rest is your homework :-)

Feel free to ask if something will not work at all.

Klaus
  • 24,205
  • 7
  • 58
  • 113
0

You can simply do this with find and sed

 find /path/to/your/dir -type f -exec sed -i 's/Full Tilt/FullTilt/g' "{}" \;
bartimar
  • 3,374
  • 3
  • 30
  • 51
0

Boy, was it tricky! First you have to reverse the output of find to start from the deepest files and directories. Then to make it work with filenames containing spaces. And then, not all rename commands are equal. You need the one from Larry Wall, which takes a Perl regex to define the renaming. Check man rename first.

find foo | tac | while read i; do echo $i; rename 's/(.*)Full Tilt/$1FullTilt/' "$i"; done
SzG
  • 12,333
  • 4
  • 28
  • 41