This article contains some linux tips. These are mostly shell commands which I often use. We also conducted a linux workshop in IIT Bombay in 2004 for linux novices. Here are the slides.

Interaction between Linux and Windows:


How to Convert DOS and UNIX text files

The UNIX and DOS operating systems (which includes Microsoft Windows) differ in the format in which they store text files. DOS places both a line feed and a carriage return character at the end of each line of a text file, but Unix uses only a line feed character. Some DOS applications need to see carriage return characters at the ends of lines, and may treat Unix-format files as giant single lines. Some Unix applications won't recognize the carriage returns added by DOS, and will display Ctrl-m characters at the end of each line. This appears on the screen as ^M.

a) remove multiple (repeated) Carriage Returns using search and replace (in vim)
:%s/[^M]$//
:w

b) Using dos2unix command
$ dos2unix dosfile.txt unixfile.txt

c) Using awk
$ awk '{ sub("\r$", ""); print }' dosfile.txt > unixfile.txt

d) Using tr (for removing ^Z chars)
$ tr -d '\15\32' < dosfile.txt > unixfile.txt

e) Using perl
$ perl -p -e 's/\r$//' < dosfile.txt > unixfile.txt

How to get access of windows partitions in linux

As root, edit the file /etc/fstab e.g. let your windows C drive partition is /dev/hda1 and it is 'fat 32' file system. So to mount the C partition in /windows/C directory, in the /etc/fstab file, make an entry as
/dev/hda1     /windows/C    vfat   umask=0000  0 0
Note that umask=0000 will give write permissions to every user. To give write permission only to root, replace by
umask=0002

To send message from linux to windows

$ smbclient -M <hostname> -I <ip-address>
And to find hostname
$ nmblookup -A <ip-address>

Using Awk Command
One can find some very good awk examples on http://student.northpark.edu/pemente/awk/awk1line.txt
Here are some examples which I use more often.

To insert line numbers
awk '{printf("%s:%d\n",$0,NR-1)}' filename

To print except first column
awk '{$1="";$0=substr($0,2)}1' file > outfile

Reverse order of lines (emulates "tac")
 awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*

Split the col into two halves
awk '{l1=length($4)/2;a=substr($4,1,l1); b=substr($4,l1+1,length($4)); print $1" "$2" "$3" "a" "b}' infile.txt > outfile.txt

Using Mount Command


To mount a hard disk partition

$mount -t <filesystem> -o <options> <device> <dir>
e.g. If you want to mount C partition as above, type as root
$mount -t vfat -o rw /dev/hda1 /windows/C

To mount an already visible directory to another place, type
$mount --bind <old-dir> <new-dir>

How to mount or extract .iso file in linux
As super user type
$mount -o loop -t iso9660 isofile mount_place

To mount nfs filesystem, e.g. mount /windows/C of 10.8.1.22 on your machine at /amit/c, type
$mount -t nfs 10.8.1.22:/windows/C /amit/c
For this, the comp 10.8.1.22 should have given you the permissions.

Media Files

How to change encoding of media files


Use mencoder
$mencoder <input-file> -oac <audio-output-format> -ovc <video-output-format> -o <output-filename>

audio-output-format option is given as

    -oac copy     no encoding, just streamcopy
    -oac pcm     encode to uncompressed PCM
    -oac mp3lame     encode to MP3 (using Lame)


video-output-format is given as

     -ovc copy       no encoding, just streamcopy
     -ovc divx4       encode to DivX4/DivX5
     -ovc rawrgb       encode to uncompressed RGB24
     -ovc lavc       encode with a libavcodec codecs

To cut a movie file give the following command
$mencoder <input-file> -ss <start-position> -endpos <end-position> -o <output-filename>-oac <audio-output-format> -ovc <video-output-format> -o <output-file>

here end-postion can be given as
     -endpos 56       encode only 56 seconds
     -endpos 01:10:00       encode only 1 hour 10 minutes
     -endpos 100mb       encode only 100 MBytes
for start-position
     -ss 56       encode from 56 sec
other options are similar as above.

for more option type
$man mplayer

Miscellaneous Commands:

1) How can I find certain words in files on my system?

Just type:

$ find DirectoryName -type f -printf "%p"  | xargs egrep -i "String" | less

where DirectoryName is the directory in which the files are located and
String is the string to be searched.

2) How can I find and replace certain words in files?

$ perl -e "s/old_string/new_string/g;" -pi.save $(find DirectoryName -type f)

3) How do I ignore system messages at login?

$ touch ~/.hushlogin

4) I cat a binary file and now my terminal is unreadable. How do I fix it?

Just do at your terminal:
<Ctrl>+v <Esc>+c

5) How can I count number of lines in a file?

At your promt type:
$ wc -l <file-name>

6) How to remove blank lines from a file?

$ sed -e '/^$/d' <file-with-blank-lines> <new-file>

7) How can I get number of files in all directories in the current directory?

$ find . -xdev -type d -exec /bin/echo -n {} \; -exec sh -c "ls {} | wc -l" \;

8) How can I unalias a command?

eg. if u alias the command as
alias ls='ls -l'

to unalias this permanently type
$ unalias ls

to unalias it temporarily just precede pico by a backslash

$ \ls

9) How to turn up num lock at bootup?

for t in 1 2 3 4 5 6 7 8
do
setleds +num < /dev/tty$t > /dev/null
done

10) How to see a movie from other's computer?

$ ssh <login>@<address> cat <movie-path> | mplayer -

eg.
$ ssh wing@10.8.1.82 cat /home/amit/f.mpg | mplayer -


11) How to enable java in mozilla/konqueror?

You can make a softlink from /usr/lib/j2re1.4.0/plugin/i386/ns610/libjavaplugin_oji140.so to /opt/mozilla/plugins
by
$ ln -s /usr/lib/j2re1.4.0/plugin/i386/ns610/libjavaplugin_oji140.so /opt/mozilla/plugins/
 
Some other tips

How to set an environmental variable/How to add path permanently


$ PATH=$PATH:<path you want to set>
So if you want to set this path for all users then put this line in
/etc/rc.local in redhat and
/etc/init.d/boot.local in suse.

How to build an rpm

a) from SRPM
Install the .src.rpm file this way:
$ rpm -i somepackage-1.0-1.src.rpm

This will create files in /usr/src/redhat/SOURCES and a .spec file in /usr/src/redhat/SPECS.
Then go the SPECS directory and give the command to build the RPM:
$ cd /usr/src/redhat/SPECS
$ rpmbuild -bb somepackage.spec

b) from source package

When a source archive (e.g., somepackage-1.0.tar.gz) contains a .spec file, one can give the following command to build the RPM without having to deploy the archive:

$ rpmbuild -tb somepackage-1.0.tar.gz

Give the -ta option instead if you also want to build the SRPM.

How to install fonts

One can install fonts in various ways.
a) type

$ xset fp+ <directory-path-of-font>
$ xset fp rehash

in this case u should have fonts.dir file in the directory.
this is a temporary way of installing fonts. u have to give these commands every time u start ur X-server.

b) Use font installer in control centre(not available in redhat)

c) Type as root
$ /usr/sbin/chkfontpath --add $ fc-cache
this doesn't work with suse but works well in redhat.

d) in /etc/X11/XF86Config file (in redhat XF86Config-4 file), in Section "Files" add the line

FontPath "<dir-path>"

and restart ur X-server. this works well in suse.

to check the list of installed fonts, type
$ xlsfonts

How to make an iso image

To make an ISO from your CD/DVD, place the media in your drive but do not mount it. If it automounts, unmount it.

$ dd if=/dev/dvd of=dvd.iso # for dvd
$ dd if=/dev/cdrom of=cd.iso # for cdrom
$ dd if=/dev/scd0 of=cd.iso # if cdrom is scsi

To make an ISO from files on your hard drive, create a directory which holds the files you want. Then use the mkisofs command.

$ mkisofs -o /tmp/cd.iso /tmp/directory/

This results in a file called cd.iso in folder /tmp which contains all the files and directories in /tmp/directory/.