Random Linux Stuff

Swap caps lock and Escape on the console

If you run Debian, add the following line to /etc/console-tools/remap:

  s/keycode 58 = Caps_Lock/keycode 58 = Escape/;

Then either reboot or, more conveniently, as root type:

  # /etc/init.d/console-screen.sh

On other distributions like Slackware, the following works great. Create the following file, call it /etc/keymap:

  keycode 58 = Escape

Then in /etc/rc.d/rc.local, add the following line:

  /bin/loadkeys /etc/keymap

Differences in directories

Recursively check differences below two entire directories:

  $ diff -rq dir1 dir2

I need wav's from these mp3's

No problem, lame is your friend. Just hop into the directory and type:

  $ for i in *mp3; do lame --decode "$i" "`basename "$i" "mp3"`wav"; done

I want to rip a CD

Use cdda2wav, which is very fast. Running as root (perhaps with sudo) can give a higher quality:

  $ cdda2wav -B

This leaves you with a bunch of files called audio_01.wav for instance. To put the number in front of the file, do something like:

  $ for i in *wav; do mv $i `echo $i|sed -e "s/audio_\([0-9][0-9]\)/\1_cdname/"`;done

If that doesn't work, install cdparanoia and check what's on the CD:

  $ cdparanoia -Q

Then rip all content to ogg format with:

  $ oggenc *wav

Or to mp3 format (boooh!!) with lame:

  $ for i in *wav; do \
      lame $i `basename $i .wav`.mp3; \

If you're ripping an audiobook, add --preset voice as an option.

Rename files with a number in front of them

Some MP3 players don't play your files as you see them in Explorer or whatever you view files in. Use the following bash script to put a sequential number in front of your files. I use ls in this example since it automatically sorts the files alphabetically.

 FILES=`ls *mp3`
 for i in $FILES; do
   mv $i "${n}_${i}"

This renumbers all files in the current directory.

Converting mp3 to ogg

Generally, it's a bad idea to convert one lossy format to another, but sometimes you have no choice:

  $ mpg321 input.mp3 -w - | oggenc -o output.ogg -

Or for batch conversion:

  $ for i in *mp3; do mpg321 $i -w - | oggenc -o `basename $i .mp3`.ogg -;done

My box stops booting

See also http://www.kernelhacking.org/docs/kernelhacking-HOWTO/indexs09.html

Export man pages to HTML

 $ man -Hcat uubp  > uubp.html

Mounting shares

To mount a Windows share, create a directory share in your home directory and mount it as follows (parameters between square brackets are optional):

  smbmount //server/sharename ~/share -o username=yourusername[,password=xxxxxx]

To mount a Netware share, install NCPFS and mount as follows:

  ncpmount -S server -A server ~/share -C -b -U username

Headless servers

If you run a headless server in a lab environment, it's useful for people to know when it finished booting. Some servers have an LCD display for this usage, but the poor man's solution is to beep at the end of the boot sequence. Edit /etc/rc.d/rc.local and add the following lines:

  echo -e "\a"
  sleep 1
  echo -e "\a"
  sleep 1
  echo -e "\a"

This will emit three beeps when the booting is done.

Removing a bunch of files

Sometimes, you need to operate on a large amount of files; normal shell file globbing (asterisk and/or question marks) won't work, because there are too much of them:

  $ rm -fr plot-9_X*
  -bash: /usr/bin/rm: Argument list too long

You'll need xargs then:

 $ find . -name 'plot-9_X*' -print0 | xargs -0 rm

The -print0 and the -0 flags are there so that spaces in filenames are not used as separators.

Burning a CD or a DVD

You'll probably need to be root. First check available devices:

 # cdrecord -scanbus
         0,0,0     0) 'ATA     ' 'WDC WD1600JS-60M' '10.0' Disk
         0,1,0     1) *
         ... (output removed)
         1,0,0   100) 'HL-DT-ST' 'DVD+-RW GSA-H31L' 'W616' Removable CD-ROM
         ... (output removed)

Watch for the writer and note the series of numbers, in this case 1,0,0. Use as follows:

 # cdrecord -v -eject dev=scsibus1:1,0,0 CentOS-4.4-i386-bin1of4.iso