Replacing substrings in file names and contents in Unix/Linux shell script
Today I’d like to share two useful code snippets to deal with string replacement in file names and contents. Each of those two operations can be done in a single pipe, without intermediate files and variables and thus fast and without side effects.
Suppose you want to replace some $STRING_ONE with $STRING_TWO in files and folders’ names (rename them) and in all files’ contents for some given directory $BASEDIR.
cd $BASEDIR
To rename all files and folders by replacing $STRING_ONE to $STRING_TWO:
find . -name "*$STRING_ONE*" -print \ | sed "h; s/$STRING_ONE\([^/]*\)$/$STRING_TWO\1/; H; g" \ | sed -n 'N; 1! G; $ p; h' | xargs -L 2 mv
To replace string $STRING_ONE with $STRING_TWO in all files:
grep -lr "$STRING_ONE" . \ | xargs sed -i '' -e "s/$STRING_ONE/$STRING_TWO/g"
The second operation uses grep to select files that contain string to be replaced before actual replacement. This makes renaming more effective when there are a lot of files in the directory and only some of them contain $STRING_ONE, but there are a lot of such substrings to be replaced.
Code snippets were tested on Mac OS X 10.6.5.
Typical .hgignore file for LaTeX project
When working on TeX documents, version control can help very much. I use Mercurial.
When compiling a final document, typesetting engine (e.g. pdflatex) produces a lot of intermediate files, which should not be version controlled. One way to exclude such files from version control is Mercurial’s .hgignore file that should be placed in repository root.
Here’s such an example file that tells hg to ignore TeX intermediate stuff:
syntax: glob *.aux *.bbl *.blg *.brf *.log *.out *.synctex.gz *.thm *.toc
Коды УДК
Список некоторых кодов универсальной десятичной классификации.
- Классификатор УДК на русском языке: http://teacode.com/online/udc.
- Сервис ВИНИТИ для расшифровки формул УДК: http://scs.viniti.ru/udc.

