Linux tips

Christophe Pallier

This is the beginning of a collection of ``tips'' for Linux. I welcome any suggestion to add new items or modify this list (see http://www.pallier.org/perso/contact.html for my email address).

Contents

1  Linux tutorials, manuals... on the Net
2  Common file types
3  Printing
4  Handling graphics files
5  Make a copy of the screen, of a windows...
6  Vector graphics
7  Converting text files from DOS, Windows, Mac...
8  Installing OpenOffice
9  Copying, renaming, moving or deleting files
10  Check or modify the rights of access to a file or a directory
11  Count files in a directory
12  Copying, moving or deleting directories
13  Finding files or directories
14  Check the amount of disk space occupied by a directory
15  Check the space left on disks
16  Linking files
17  Accesing files on a data CD or on a floppy
18  Formatting a floppy
19  Spliting a large file on several floppies
20  Ripping sound files from an audio CD
21  Converting from wav to mp3
22  Converting from wav to ogg vorbis
23  Creating a data CD
24  Creating an audio CD
25  Make backups
26  Who am I?
27  Changing your identity
28  Which computer/system am I working on?
29  Listing the hosts in a NIS domain
30  Which shell is running?
31  Change the password
32  Change the login shell
33  Adding a directory to the PATH
34  Create a script to execute a series of commands
35  Getting help. Finding manuals
36  Comparing two files or two directories
37  Links
38  Substituting strings in many files
39  Running a command on multiple files
40  Converting a directory into a single file
41  Compress / uncompress files
42  Listing all the programs currently running on the system
43  Killing a program which no longer responds
44  Checking who is logged on the computer
45  Connecting to a remote computer
46  Displaying locally a window generated on a remote computer
47  Executing commands on a remote computer, without login
48  Setting up ssh
49  Copying file to or from a remote computer
50  Cut'n paste
51  Setting up an ethernet card to access the internet
52  Check the speed of an ethernet connection
53  Dynamic libraries
54  Installing new software
55  Checking if a software package is installed
56  Using a Palm Pilot

1  Linux tutorials, manuals... on the Net

2  Common file types

typical extension file type application(s)
txt text or ascii file cat, less (view)
nedit, emacs (edit)
pdf Adobe PDF acroread, xpdf, gv (view)
ps, eps postscript gv (view)
pstops (rearrange)
ps2pdf (convert)
html, htm web page links, konqueror, mozilla (view)
soffice (create)
png, jpg, gif... graphic files display (view)
import (snapshot)
convert (convert)
gimp (manipulate)
doc, xls, ppt Office document soffice
sxc, sxi, sxw OpenOffice document soffice
tex TeX and LaTeX documentz tex, latex, pdflatex (process)
dvi Dvi documents xdvi (view)
dvips, dvipdf (convert to ps or pdf)
gz, Z Compressed file gunzip, zip, bunzip2, bzip2
tar tar archive tar tf (view)
tar xf (extract)
tar cf (create)
tar.gz compressed archive tar xzf (extract)
tar.bz2 Compressed tar archive tar xjf
zip zip archive unzip -l (view)
unzip (extract)
zip (create)

3  Printing

Most linux applications can generate postscript files, which usually have the extension '.ps'.

Postscript is a language that describe how things should be drawn on pages; this language is understood by postscript printers. Non-postscript printers needs special drivers that convert from postscript onto their own format.

If the ghostcript package is installed on your system (see section 55 ``how to check if a package is installed''), it is possible to convert pdf files in postscript with the pdf2ps command:

pdf2ps paper.pdf

creates paper.ps.

You can preview a postscript file on the screen with gv or kghostview:

gv file.ps
kghostview file.ps

To put the 'file.ps' into the printing queue of the printer known as 'printername':

lpr -P printername file.ps 

The printers can be attached to the local system via a parallel, serial, or USB port, or available over the network. To get a list of available printers:

lpstat -p -d

To check the status of all printers:

lpstat -a

To remove a printing job:

lprm job-id

(job-id is the number reported by the lpr or lpstat commands).

If your system uses the printing system 'CUPS', you can access its documentation by opening the following address in a browser:

http://localhost:631/documentation.html

4  Handling graphics files

Images are stored in bitmap files which have extensions .gif, .jpg, .png, .tiff, eps...

To display an image, e.g. file.gif, use:

display file.gif

To convert from one format to another:

convert file.jpg file.eps

Note: I advise against using jpeg format because it loses quality: for bitmaps, use the 'png' format instead; for vector graphics, use eps (encapsulated postscript).

The 'display', 'convert' and 'import' commands are part of the package ImageMagick, which provides other commands to manipulate bitmap graphic files. To know more, type:

man ImageMagick

For more complex manipulations of an images, I use ``The Gimp'':

gimp file.jpg

5  Make a copy of the screen, of a windows...

To take a snapshot, i.e. copy the screen or part of the screen into an image file, e.g., 'file.png', type

import file.png

('import' is part of ImageMagick)

6  Vector graphics

I personally use 'xfig' to construct complex graphics. Yet the ergonomy of xfig is very different from what users expect nowadays.

You can try 'sketch', 'dia' or OpenOffice draw.

For mathematical plots, bar plots..., I use the data analysis package R. For simple things, gnuplot may also help.

7  Converting text files from DOS, Windows, Mac...

Texts files under unix, mac, dos and windows use differents codes for end-of-lines and accentuated characters. Linux typically uses the Latin-1 character encoding. The ``recode'' program converts from one format to the other (info recode).

To convert file.txt coming from a Macintosh:

recode mac..latin1 file.txt

To convert from DOS codepage 850 to unix latin1:

recode 850..latin1 file.txt

To just remove ^ M:

recode l1/crlf..l1 file.txt

To convert from Windows codepage 1250 to unix latin 1 (iso-8859-1):

 
recode ms-ee..l1 file.txt

8  Installing OpenOffice

OpenOffice is a ``clone'' of Microsoft Office, except that it is a free software

Supposing that Openoffice has been installed in, say, /usr/local/openoffice, to set it up on your account, type:

/usr/local/openoffice/setup

This creates a folder OpenOffice.org1.1 in your home directory (the exact name of this folder depends on the version of OpenOffice.org1.1). This directory contains the 'soffice' program.

You can now, add a shortcut on the desktop, or a special button on the kde control panel, that launches soffice directly.

You may also want to edit the file types in your file manager to associate the Microsoft Office documents (.doc, .xls) to soffice. In this way, a simple click will open them. If you use Mozilla Mail, to open attached files with soffice, you should configure the helper applications (Menu:Edit/Preference/Navigator/Helper Applications).

Finally, you may also want to add '/home/login/OpenOffice.org1.1' to the PATH (See the tip 33 ``Adding a directory to the PATH''), to allow you to start openoffice from the command line by typing:

soffice 

9  Copying, renaming, moving or deleting files

To copy a file in the same directory, giving it name2:

cp file1 file2

To copy a file from the current directory to the existing directory `dir':

cp file1 dir

To rename a file:

mv file1 file2

To move a file to the existing directory `dir':

mv file1 dir

To delete a file:

rm file

To avoid being asked for confirmation:

 rm -f file

10  Check or modify the rights of access to a file or a directory

When you use 'ls -l' to list the files in a directory, the first string of characters, made of 'x', 'r' 'w', '-'... specifies the access rights (Consult Understanding file permissions on Unix: a brief tutorial)

To allow everybody to read the files in the current directory:

chmod a+r *

11  Count files in a directory

To know how many files with extension img are in the current directory:

ls *.img | wc

12  Copying, moving or deleting directories

To create a new directory:

mkdir newdir

To copy the directory 'dir' in the destination directory 'destdir':

cp -a dir destdir

(Note: the '-a' option does a recursive copy - that is includes the subdirectories - and preserves the attributes of files)

To move the whole directory 'dir' inside the existing 'destdir':

mv dir1 destdir

To rename directory 'dir' as 'dir2':

mv dir dir2

To delete the directory 'dir' and all its content:

rm -rf dir

13  Finding files or directories

To locate the file 'filename', searching all the subdirectories of the current directory:

find . -name 'filename'

You can use a pattern in place of 'filename', e.g. to return the list of all .doc files in the current directory and its subdirectories:

find . -name '*.doc'

The 'find' command can do much more. For example, to find the files that were accessed yesterday:

find ~ -daystart -type f -atime 1

Consult 'info find' for more information.

14  Check the amount of disk space occupied by a directory

du -h dirname 

It may be useful to sort the subdirs by size:

du -k | sort -rn

To list files sorted by size:

ls -Sl

15  Check the space left on disks

To see the space available on the mounted partitions:

df

If there is a quota system that limits the amount of space you can use on your account, you can check how much:

quota

16  Linking files

To avoid copying a file in several places on the same disk, it is a better idea to use a link:

ln existingname newname

Thus the same file can have several names (and be in several directories at the same time).

17  Accesing files on a data CD or on a floppy

With some linux systems, you just insert the CD or the floppy and the content become available in the directory /mnt/cdrom or /mnt/floppy:

ls /mnt/cdrom
ls /mnt/floppy

If the floppy is not write-protected, you can create or copy files in /mnt/floppy just like in any ordinary folder.

Note that if you have several cdrom or floppy drives, they may have names cdrom1, cdrom2, floppy1,...

In some Linux systems, it is necessary to manually ``mount'' the cdrom or the floppy before accessing the files, and ``umount'' before ejecting it. For the cdrom:

mount /mnt/cdrom
ls /mnt/cdrom
...
umount /mnt/cdrom
eject

For the floppy:

mount /mnt/floppy
ls /mnt/floppy
umount /mnt/floppy

If you get an error message like ``mount: only root can do that'', ask the system administrator to grant you right to mount floppies by adding the ``user'' option the configuration file /etc/fstab. More information in the manual pages of mount and fstab:

man mount
man fstab

Concerning floppies, some systems have the mtools system (see 'man mtools') which provide the mdir and mcopy commands that emulate the old DOS commands dir and copy. It is not necessary to mount the floppy to use them.

18  Formatting a floppy

To format the floppy with an ext2 filesystem, and mount it:

fdformat /dev/fd0
mkfs -t ext2 /dev/fd0 
mount -t ext2 /dev/fd0 /mnt/floppy

This floppy can be read only on other linux systems. To be able to read it under Windows/DOS, you should use a DOS filesystem with mkdosfs in place of mkfs -t ext2:

mkdosfs /dev/fd0

19  Spliting a large file on several floppies

First compress the file, with gzip or bzip2 (see section 41). If it still does not fit on a single floppy (1.4Mb), you can use the command split:

split -b1m file

This create a series of x?? files which you can copy on separate floppies.

To reassemble the files:

cat x* >file

20  Ripping sound files from an audio CD

Maybe the easiest way to copy tracks from an audio cd it to open 'konqueror', and type 'audiocd:/' in the address bar. This will show you the content of the CD, which you can copy somewhere else. Copying from the mp3 or ogg folders will do the automatic translations for you.

Else, there are two programs with graphical interface which allow you to rip audio CD: 'grip' and 'kaudiocreator'.

On, the command line, you can use cdparanoia. For example, to rip an entire audio CD:

cdparanoia -B

To rip just one track:

cdparanoia -w track_number file.wav

21  Converting from wav to mp3

I use lame:

lame file.wav file.mp3

But it is a better idea to use the open format 'ogg vorbis' rather than mp3:

22  Converting from wav to ogg vorbis

I use oggenc:

oggenc file.wav -o file.ogg

23  Creating a data CD

  1. Put all the files you want to save in a given directory, e.g. /tmp/mycd.
  2. Create an iso image:
    mkisofs -o cd.iso -J -R /tmp/mycd
    ls -l cd.iso 
    
    

    Check that the resulting ``cd.iso'' file is not too large to fit on the CD; if it less than 650Mb, this should be ok.

  3. Record on the cd (you must be root).

    You must know which is the device is associated to the CD writer drive.

    cdrecord -scanbus
    
    
    To determine the x,y,z scsi coordinates of your cd writer. If it does not appear listed, it may be because the ide-scsi parameter was no passed to the Linux kernel (See the HOWTO about CD Writing).

    To record, do:

    cdrecord dev=x,y,z -multi speed=0 -data cd.iso
    
    

24  Creating an audio CD

To put on an audio CD all the *.wav files which are in the current directory:

cdrecord dev=x,y,z -pad speed=0 -audio *.wav

(x,y,z must be replaced by the numbers returned by cdrecord -scanbus)

25  Make backups

You can backup on CD-RW using the method described in section 23.

If you have a network connection, you can backup on a remote computer, provided it has enough free space on its disk and a sshd server running.

To copy a whole directory, you can type:

scp -r localdir remotehost:remotedir

Yet, I prefer to use unison, which keeps two directories synchronized, transfering only new or modified files, in both directions:

unison localdir ssh://remotehost/remotedir

26  Who am I?

As far a the computer is concerned, the identity of the current user (that is its login), can be printed with:

whoami

More detailed information is available with

id

Note that your login name and home directory are saved in the environment variables HOME and LOGNAME:

echo $HOME
echo $LOGNAME

27  Changing your identity

To temporally become ``newuser'':

su newuser

Of course, you will be prompted for newusers's password.

To stop being ``newuser'', type:

exit

28  Which computer/system am I working on?

To display the network node name (also called the ``hostname''):

uname -a

The following also works:

hostname

29  Listing the hosts in a NIS domain

If you are connected on a local network administrated by NIS (``yellow pages''), you can display the list of other computers on the network:

ypcat hosts

30  Which shell is running?

When you enter commands on the command line in a terminal, the text you type is interpreted by a program called the 'shell'. There are different shells that speak different dialects. To determine the shell you are communicating with, type:

echo $SHELL

Note: this does not work well for subshells:

bash
echo $SHELL
csh
echo $SHELL
exit
exit

31  Change the password

To change your password on a network administrated with NIS (``yellowpages''):

yppasswd

To change your password a local system:

passwd

32  Change the login shell

To change your login shell, e.g. from /bin/csh to /bin/bash:

ypchsh 

or

chsh -s /bin/bash

To change your password on the local system:

passwd

33  Adding a directory to the PATH

Commands that you type on the shell command line are searched in the directories listed in the PATH variable:

echo $PATH

When several directories contain a command with the same name (a bad situation), the first directory encountered in the PATH has priority. To find out the directory that contains the command that will be executed:

which command

To add a new directory 'newdir' to the PATH:

If the shell is sh or bash (check with echo $SHELL):

export PATH=newdir:$PATH

Place these lines in the file '.bashrc' in your home directory to have the PATH set correctly the next time you open a shell.

If the SHELL is tsch or csh:

set path = (newdir $path)
rehash

Place these lines in the file '.cshrc' in your home directory, to have the correct PATH the next time you open a shell.

34  Create a script to execute a series of commands

If you happen to often type the same series of commands, it is a good idea to create a script.

If it does not exist yet, create the directory $HOME/bin.

Use a text editor to create a file 'myscript' in this directory, and type the series of commands.

The first line of the file should be:

#! /bin/bash

Save the file, then enter the command:

chmod +x ~/bin/myscript

You can now type ``myscript'' on the command line to execture the series of commands.

To go further, you should learn how to use arguments to scripts. There are tutorials on shell script programming on the web.

35  Getting help. Finding manuals

Many commands have associated ``man pages''. To read the man page associated, for example, to the command 'cp':

man cp

Some commands also have manuals in the form of 'info files':

info gawk

On many linux systems, there is additional documentation in the /usr/share/doc folder. The HOWTOs can be especially helpful.

36  Comparing two files or two directories

To show all the lines that differ between file1 and file2:

diff file1 file2

To create a patch listing the changes from version1 to version2:

diff -aur version1 version2 >dir2.diff

To apply the patch to version1 and produce version2:

  patch -p1 <dir2.diff

For text files where the wrapping of paragraph may have changed, use 'wdiff'.

To compare two directories:

diff -r --brief dir1 dir2

To copy the content of dir1 into dir2 without the copying the files that already exist and are the same, use rsync:

rsync -a dir1 dir2

If you want to copy the newest files and delete the ones (make dir2 a miror copy of dir1:)

rsync -a --delete dir1 dir2

To synchronize two directories (in both directions), I use the program 'unison' (see section 25).

37  Links

To create a symbolic link (somewhat similar to a 'shortcut' in Windows):

ln -s filename newname

If you delete or move the file, the symbolic links will be 'dangling'.

To find and remove dangling links in a directory:

symlinks -rd directory

38  Substituting strings in many files

perl -pi.bak -e 's/OLDSTING/NEWSTRING/g' FILELIST
# attention: to match words only, use s/\bOLDSTRING\b/NEWSTRING/g

39  Running a command on multiple files

It is sometime useful to run a command on many files. For example, to convert a series of eps files into jpg format:

for f in *.eps; do convert $f ${f/%.eps/.jpg}; done

Note that this is only valid if you are interacting with the shell 'Bash'. If you use another shell (see section 30) then you must type 'bash' first.

40  Converting a directory into a single file

Say you want to all the files in a directory in an email attachment. You can put a whole directory into a file ``file.tar'' with the command:

tar cf file.tar directory

Before sending it, it a good idea to compress ``file.tar'', as described in the next section.

41  Compress / uncompress files

To compressing a single file:

gzip file

To decompress it:

gunzip file.gz

To compress a directory:

tar czf aga.tar.gz directory # compressing
tar tzf aga.tar.gz # listing
tar xf aga.tar directory # uncompressing

.zip files:

zip -r archivename directories files
unzip -l archivename
unzip archivename

42  Listing all the programs currently running on the system

To list all the processes currently running:

ps -axlf

(you may omit the 'a' option if you want to list only the processes owned by you, and -l if you want less information)

The most important columns are 'time' and 'RSS' which show the time used by process since it started and the amount of real memory it takes.

If you want to list just some programs, for example ``matlab'', type

ps -ax | grep matlab

Another useful utility to list running processes is 'top':

top

The info is refreshed every second or so. Type 'M' to sort by memory usage, 'P4 to sort per CPU usage, and 'Q' to quit.

Under KDE, you can add the 'Ksysguard' applet to the Control Panel: this reports the CPU usage and amount of memory use.

43  Killing a program which no longer responds

It may happen that a program monopolizes most of the CPU, but does not longer respond to input. Such a program is crashed and should be destroyed.

For applications running in a terminal, first try 'Ctrl-C'.

If this does not work, or if the application is running in its own window but refusing to close, open a terminal and use 'ps' to locate the application (see section 42) and note the number in the 'PID' column (``process identification number'').

Then, type:

kill PID

(in place of PID, use the number associated to the process listed in 'ps' output). Check if the program was destroyed with the 'ps' command; if not:

kill -9 PID

If the whole graphics system no longer responds, you can try to open a text mode terminal with Ctrl-Alt-F2 or Ctrl-Alt-F3, log in and kill the programs that cause problem (sometimes, it can be the 'X' program itself. 'X' (XWindow) is the program that manages the graphic display).

It the keyboard does not repond anymore, before switching off the computer, you can try to connect from another computer on the same network and kill the applications or do a proper shutdown (typing 'halt' on the command line).

44  Checking who is logged on the computer

To see who is currently logged on the system, use

who

or

w

If you are superuser, you can see a journal of the logins with the command:

last

45  Connecting to a remote computer

A secure method to connect to a remote computer:

ssh computername

Note that the remote computer must be running a 'sshd' server. If you cannot connect using ssh, you can try the insecure commands:

rlogin computername
telnet computername

Note that the login and password are sent in clear over the network.

46  Displaying locally a window generated on a remote computer

The X Window graphic system used by Linux allows to see on the local computer graphic windows generated by a program running on a remote computer.

On the local computer, type:

xhost +

On the remote computer, type:

setenv DISPLAY localname:0

(or 'export DISPLAY=localname:0' if the shell is 'bash')

Replace localname by the name of local computer. If you do not know it, type the following on the localcomputer:

uname -n

47  Executing commands on a remote computer, without login

ssh computername command
rlogin computername command

48  Setting up ssh

See the documentations of ssh-keygen and ssh-agent.

In brief, on the local computer, run:

ssh-keygen -t rsa1

This generates, among other things, a public key (in a file .ssh/identity.pub) that you have to copy in the authorized_keys file in the .ssh directory of the remote computer.

To avoid typing the passphrase each time you log to the remote computer, copy the following lines in your .bash_profile:

eval `ssh-agent`
ssh-add < /dev/null

You will be prompted for the passphrase only once: when you login on the local computer.

49  Copying file to or from a remote computer

scp -r misc remotelogin@computername:


wget


ncftp


rsync

unison

50  Cut'n paste

Cutting & pasting under linux is not always straigtfoward. This is due to the fact that there are various systems of cut'n paste cohabitating.

To copy text, the following works with most applications:

Note that this is very convenient: there no need to explicitly 'copy' the text.

If you use the window manager 'kde', there is a useful applet called 'klipper' located on the panel. Klipper keeps copies of the most recent clipboard contents. If a cut'n paste operation does not work, you may open klipper, select the relevant line, and retry to paste. It usually works.

If it does not work, then you can try the Cut/Copy/Paste functions from the applications' menus. Sometimes, it is necessary to save the region as a file in the first application, and insert this file in the second application.

51  Setting up an ethernet card to access the internet

You need to know IP, MASK, GATEWAY, DNS, HOSTNAME and DOMAIN:

ifconfig eth0 IP netmask MASK up
route add -net default gw GATEWAY netmask 0.0.0.0 eth0
hostname HOSTNAME
echo ``domain DOMAIN'' >/etc/resolv.conf
echo "nameserver DNS" >>/etc/resolv.conf

52  Check the speed of an ethernet connection

mii-tool

ethtool

iperf

53  Dynamic libraries

To run, some programs need to access functions in dynamic libraries. Dynamic libraries have the extension .so. They are located in /lib, /usr/lib, /usr/local/lib...

To list the libraries needed by a program:

ldd program

After adding new a new dynamic library, e.g. in /usr/local/lib, you must run, as superuser:

ldconfig -n /usr/local/lib

It is possible, as a user, to tell linux to search libraries in a particular place, using the LD_LIBRARY_PATH variable. For more information about how dynamic libraries are accessed, consult the manual of ld.so:

man ld.so 

54  Installing new software

If it come as a .tar.gz and contain a configure script

tar xzf package.tar.gz
cd package
./configure --prefix=$HOME & make & make install

This install the software in your home directory. To install it for every user, you need to omit the prefix option and be root when calling ``make install''.

If it comes as a rpm, you need to be root:

rpm -i package.rpm

To check if the package is correctly installed:

rpm -V package

To remove it:

rpm -e package

55  Checking if a software package is installed

To check if, say, ghostscript is installed:

rpm -q ghostscript

You can get the list of all installed packages:

rpm -qa

56  Using a Palm Pilot

To backup data from the PalmPilot to the directory Palm:

export PILOTRATE=115200
export PILOTPORT=/dev/ttyS0
pilot-xfer --backup Palm




File translated from TEX by TTH, version 3.01.
On 4 Jan 2005, 13:21.