free software, web technologies and heavy metal coding (and ruby of course)

useful linux commands

This is a random aggregation of useful linux commands.
Useful for those who will be eventually stuck as I once was.
Or those who didn’t know that certain things can be done in such a smooth way…

Print a locked pdf

To be able to print a pdf file even though it has been print locked you can convert the pdf to postscrloipt and back again to a pdf.

$ pdf2ps locked.pdf && ps2pdf unlocked.pdf

Mount a cd-rom image

This will mount an image file to a certain mountpoint such that it can be used as a usual drive (like a virtual drive in windows).

$ mount -o loop -t iso9660 <isofilename> <mountpoint>

ssh with x-window support

With this you can launch an x-window application via an ssh connection (provided that the host computer does support the particular toolkit).

$ ssh -l <loginname> <remote-host> -X -C <path-to-application>

Print out manpage

Prints out a manpage in a nicely formatted way.

$ man <manpage> | col -b | ul -t dumb | lpr -P<printername>

$ man -Tps <manpage> | lpr -P<printername>

Find advanced

Find is a very powerful command, as it is extensible in a very convenient way.\ Every find-result is passed to “command”.

$ find <parameters> -exec <command '{}' ';'

example 1, copies all jpg images to the folder “img/”

$ find -name *.jpg -exec cp '{}' img/ ';'

example 2, searches all textfiles for “searchstring”

$ find -name *.txt -exec grep "searchstring" '{}' ';'

Disable beep

If it’s about time to disable the annoying beep in the shell. For the current user

$ echo "set bell-style none">> ~/.inputrc

For all users (you have to be root) in /etc/inputrc set the line set bell-style none

Convert a text file from DOS to Unix style

This tool converts dos formatted text files with end-of-line ^M^J to unix end-of-line ^J.

$ dos2unix <file

Patch file

With the following command you create a patch file from any source file.

$ diff -u src.old src.new1 > file.patch0

To apply the patch

$ patch -p0 file < file.patch0

Convert a large file into small files

$ split -b 650m file # split file into 650MB chunks

$ cat x* > largefile # merge files into 1 large file

Web page dump

The following will save the contents of a web page to a textfile.

$ lynx -dump > textfile

Clear file contents

In order to clear the contents of a file such as a logfile, do not use rm to delete the file and then create a new empty file, because the file may still be accessed in the interval between commands. The following is the safe way to clear the contents of the file.

$ :>file-to-be-cleared

Merge two PostScript or PDF files

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pswrite -f

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merge.pdf -f file1.pdf file2.pdf

Samba client basics

Mount a windows (samba) share

$ mount -t smbfs -o username=myname,uid=my_uid,gid=my_gid //server/share /mnt/smb

$ smbmount //server/share /mnt/smb -o "username=myname,uid=my_uid,gid=my_gid"

List the shares on a computer

$ smbclient -L

Samba neighbors can be checked from Linux using the following command

$ smbclient -N -L ip_address_of_your_PC | less

$ nmblookup -T "*"

Make iso from cd

$ dd if=/dev/cdrom of=image.iso

Burn iso to cd

For an ide device

$ cdrecord -v speed=SPEED dev=ATAPI:1,0,0 -data image.iso

To find out the device number, use the following command

$ cdrecord -scanbus dev=ATAPI

Alternatively, you can specify the ide device directly

$ cdrecord -v speed=SPEED dev=ATAPI:/dev/hdc -data image.iso

For a scsi device

$ cdrecord -v speed=SPEED dev=1,0,0 -data image.iso

To find out the device number, use the following command

$ cdrecord -scanbus