SysAdmin Tips

Renaming multiple files

for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done
for i in *.phtml; do mv $i ${i%.phtml}.php; done

Deleting files matching a pattern

find opt/Products -name "*.pyc" -exec rm '{}' \;

Sed: Stream Editor


sed 's/old/new/g' input.txt > output.txt


sed '/old/ s/old/new/g' input.txt > output.txt

Do on a folder full of files:

#! /bin/sh
# Source files are saved as "filename.txt.bak" in case of error
# The '&&' after cp is an additional safety feature
for file in *.txt
cp $file $file.bak &&
sed 's/foo/bar/g' $file.bak >$file

Insert paragraph:

  • e.g. search for lines `#include <termios.h>' and then write:
    #ifdef SYSV
    #include <termios.h>
    #include <sgtty.h>

Now, for writing the same script on one line, the -e mechanism is needed... what follows each -e can be considered as an input line from a sed script file, so nothing kept us from doing:

sed -e '/#include <termios\.h>/{' \
-e 'i\' \
-e '#ifdef SYSV' \
-e 'a\' \
-e '#else\' \
-e '#include <sgtty.h>\' \
-e '#endif' \
-e '}'



Collected resources:

Adding a swapfile

If you have a live system which you need to give extra swapspace too without a reboot, then can add a swapfile.

Note that a swapfile is much slower than a swap partition, which is -of course- slower than memory. The reason for this is to save the ship when the boat's going down, not increase preformance. The right way to fix an OOM issue is to fix the leaky program.

Create file:

dd if=/dev/zero of=/swapfile count=[size of swap, in 512B blocks]

Format it:

mkswap /swapfile

Tell the kernel to use it at a lower priority (i.e. only use when swap partition is full):

swapon -p -2 /swapfile 

Changing editors

In ubuntu (or any shared system really), you will often find that you don't have the option to use your favourite editor. Ubuntu offers a farirly sophisticated way to dictate which editor you wish to use. Try:

man update-alternatives


update-alternatives --config editor


Last modified 10 years ago Last modified on 09/11/10 08:35:39

Attachments (2)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.