Version 5 (modified by Fran Boon, 11 years ago) ( diff )


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.

Create file:

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

Format it:

mkswap /swapfile -p 16384

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

swapon -p 16384 /swapfile 


Attachments (2)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.