Archive for the ‘bash’ Category

h1

SVN add all waiting files

September 7, 2011

If you would like to add all pending files to svn via an svn add command, the following works quite nicely:


svn status | grep ? | sed 's/^? \(.*\)/\1/g' | xargs svn add

And the same for removing missing files:


svn status | grep ! | sed 's/^! \(.*\)/\1/g' | xargs svn rm

h1

Rename files in directory with some regex magiks

April 12, 2011

When using drupal I often want to clone a module as a starting point for a new module. I then have to rename all the files in the directory to the new structure. This can be painful if there is more than one file. All files in drupal follow the file naming convention of :
modulename.type
So the following bash script will niftily go through a directory renaming all the files with module1 and replace with module2…


for f in *; do mv $f `echo $f | sed 's/module1\(.*\)/module2\1/g'`; done

h1

Find and replace with sed and find

March 7, 2011

To find all instances of a string in a file you can use grep like so

grep -n findstring somefile.txt

To replace all instances of a string with another string in a file you can use sed like so

sed -i 's/find/replace/g' somefile.txt

To find all instances of a string in all files in a directory you can use grep like so

grep -n findstring /some/dir/*

To replace all instances of a string in all files in a directory you can combine sed and find like so

find /some/dir -type f -exec sed -i 's/find/replace/g' {} \;

-n on grep makes it print off the line numbers of where it found the search term

-i on sed means inplace and means it will do the replacement in that file and save it. You can do -iold if you like and every altered file will then be backed up to .old first

-type on find means only regular files are returned.

-exec on find means that the command following will be executed for each found file. The {} items puts the filename into the command. All args after -exec are assumed to be arguments of the command until a semi-colon is found. The semi colon has to be escaped otherwise it will be seen as a command line end.

Simples!