mario::konrad
programming / C++ / sailing / nerd stuff
Linux - Tips and Tricks
© / Mario Konrad

Command Line Tips

Create a file with zero length

If you like to create a file with zero length, use this command:

    touch filename

A file with length 0 gets created. Use the same trick to set the modification date and time of the file to the current values.

This is also an easy trick to see which files have changed during your work:

    touch start_of_work
    ...
    ...
    find . -newer start_of_work
    rm start_of_work

This will print all files in the current directory tree which have changed.

Change the caption of all filenames

Your filenames have mixed caption. Change them all to lowercase with this script:

    #!/bin/sh
    for i in $@ ;
    do
      export j=`echo $i | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'`
      [[ $i != $j ]] && mv $i $j ;
    done
    # EOF

This script renames all files to lowercase only. If a file already exists with only lowercase letters, the original file won’t renamed.

As it’s used to be, there is another way to do this (thanks to Patrick Meier) using the tr command:

    #!/bin/sh
    for i in $@ ;
    do
      export j=`echo $i | tr A-Z a-z`
      [[ $i != $j ]] && mv $i $j ;
    done
    # EOF

Rename all extensions

You have a bunch of files with the same extension, but you like to rename all files to another extension. Here is a solution:

    for i in *.mpg ;do mv $i ${i%.mpg}.mpeg ;done

This command renames the extensions of all files ending with .mpg to .mpeg

Find a file within several RPM archives

If you are looking for a file and you don’t know in which RPM file it is, you may use the following command:

    for i in *.rpm ;do rpm -qlp $i | grep "pattern" && echo "==> found in" $i ;done

Replace “pattern” with the term you’re looking for. Note: grep uses regular expression.

Tips

sfdisk

Configure SD card for 255/63/? using sfdisk:

    SIZE=`fdisk -l $DRIVE | grep Disk | grep bytes | awk '{print $5}'`
    echo DISK SIZE - $SIZE bytes
    CYLINDERS=`echo $SIZE/255/63/512 | bc`
    echo CYLINDERS - $CYLINDERS
    {
    echo ,9,0x0C,*
    echo ,,,-
    } | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE

Mount an ISO file

    mount file.iso /mnt/point -o loop

Creating an ISO Image

The command in general:

    dd if=device of=file

Assuming you have a CD-ROM or a DVD-ROM on the SCSI bus /dev/scd0, the command to create the image file:

    dd if=/dev/scd0 of=image.iso

You may now write this image to another CD (or DVD):

    cdrecord -v speed=16 dev=0,4,0 -data image.iso

Parameters may vary, depending on your CD-RW/DVD-RW and bus system.

The sequence to create an image works for many other devices as well (floppy disc, etc.).

Writing a floppy disk from an existing image

Often you have a floppy disk image (a boot disk from a Linux distribution for example) and you like to write it onto a floppy disk. Many distributions provide DOS tools to do this. We like to do this with Linux instead. All you need is the image file and the dd utility:

    dd if=imagefile of=/dev/fd0 bs=1440k

This command assumes that the floppy disk is the device /dev/fd0 and it is a 3.5" floppy disk drive with a capacity of 1.44MB (or 1440kB).

Erase content of a CDRW

Just use this command (this expects a SCSI CD rewriter on ID 4, LUN 0, which is able to rewrite at speed 16x.):

    cdrecord dev=0,4,0 blank=all speed=16

Creating directly a CD image

This is a second method which does not use the loopback device. It just creates the CD image of a prepared directory tree of data.

    mkisofs -r -l -f -joliet -o cdimage sourcepath
    cdrecord -v speed=16 dev=0,4,0 -data cdimage
    umount /mnt/cdrw