Graeme's Wiki

linux tips

Type the code word, MONDAY here and then press to finish editing.



Last updated: 2011-03-07

Here I have listed some tips on things I needed to do under Linux but can never remember them. I hope somebody finds them useful.

Mounting a SAMBA share

sudo mount -t cifs -o username=<username>,password=<passwd> //Server/Share /mnt/data

Browsing a SAMBA share

smbclient -W M2 -U <username> //server/data <password>

Searching for files containing a certain string

The following command will search for the string GUIVersion in *.pas files.
find . -type f -name \*.pas -exec grep -H -i -n "GUIVersion" {} \;

Mounting removable devices based on there UUID's

This might be needed because the /dev/xxx can change depending on the order the devices were inserted into you computer's USB ports.

  1. Plug in the device (no need to mount them).
  2. dmesg and look for the device name found.
  3. ls -lah /dev/disk/by-uuid note the UUID report based on device name from step 2.
  4. Edit the /etc/fstab file and enter the device information. Example:
    UUID=4750-2439  /mnt/flashdrive vfat    rw,user,noauto,nosuid,exec      0       0
    
  5. Make sure the /mnt/xxx directory exits.
  6. You can now mount the device as follows:
     mount /mnt/flashdrive 


Copy one directory to another (keeping permissions)

cd sourcedir
tar -cpf - * | tar -C /destdir -xvpf -

Compress a directory and content

tar cf - <dir> | gzip -f9 > "<dir>.tar.gz"
..or using bzip2 compression:
tar cf - <dir> | bzip2 -f > "<dir>.tar.bz2"


Unmount partition avoiding "device is busy" error

To unmount a partition avoiding the "device is busy" error you can do the following. By the way, the error is caused by the umount program itself. Use the lazy unmount feature.
umount -l /opt

Search and Replace using vi

Something that we’ve all had to do at one time or another is use find and replace within a document. This is really easy within Vim.

To change all instances of one word to another within one line use:
:s/word/replacement/g
To change all instances of one word to another within the entire file use:
:%s/word/replacement/g
To ask for confirmation before replacing each instance of a word use:
:%s/word/replacement/gc

Convert a PostScript file to PDF

You could use the ps2pdf command, but it doesn't always give the desired results. Instead you could use GhostScript directly which in turn allows you to fine tune the results. Here is such a example:
gs -q -dBATCH -dAutoFilterColorImages=false -sColorImageFilter=FlateEncode \
  -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=file.pdf -sPAPERSIZE=a4 file.ps

Find all executable file in a directory hierarchy

find . -type f -perm /111 -exec echo '{}' \;

If you wanted to delete all executables found, change the last command from echo to rm. For more examples of what find can do, look at the following web page. More Find Examples

Another cool example is find all files greater than say 20Mb and listing the file names with the size at the end.
find / -type f -size +20000k -exec ls -lh '{}' \; | awk '{ print $8 ": " $5 }'

Here is another example.
find . -type f -perm /111 \( ! -regex '.*/\..*' \) -print

Now it finds all executables excluding and hidden directories and files in the results. Even excluding non-hidden files in hidden directories (which is what I want).

Remote Desktop to a Windows PC via the command line

   rdesktop -z -x l -g 1280x800 -u myusername -p mypasswd 192.168.0.19

Ripping a Data CD to ISO image

  dd if=/dev/cdrom of=cd_image.iso bs=4M

Mounting an ISO image file

  sudo mount -o loop -t iso9660 /tmp/image.iso /cdrom

Find what package a file belongs to

dpkg -S /usr/share/i18n/locales/en_ZA
This returns the 'locales' package name.

Find information about a package

dpkg -s locales

Format USB Flash drive with ext4 file-system

The only reason USB Flash drives don't come standard with the free ext3 or ext4 files-systems is because of Windows. Even though the Linux ext3/ext4 file-systems are free and easy to install under Windows, Windows uses will not have out-of-the-box support. Obviously this is not a problem for Linux users. Oh well, here is how you reformat your USB Flash drive with ext4 (latest Linux Journalling file-system)

  • mount your USB stick. With Ubuntu, most automount so this is as simple as sticking it in
  • do a "$ df" and determine your drive's partition name. Mine is /dev/sdb1
  • unmount the drive
  • Be very careful and double-dog sure that you have the right partition name.
  • Use your USB's partition name in-place of my example /dev/sdb1
  • Don't ruin your hard drive by using the wrong partition. Be careful !
  •  $ sudo mkfs.ext4 /dev/sdb1 
  • Give your drive a name
  •  $ sudo e2label /dev/sdb1 usb-mydrive 


Redirecting the stderr and stdout to file

Use the following syntax:
$ command-name &>file
OR
$ command > file-name 2>&1

Rename all files and directories to lowercase

Simply run this one liner Perl command.
find . | perl -ne 'chomp; next unless -e; $new=lc($_); rename $_,$new'