Linux howtos

Christophe Pallier

This is the beginning of an attempt to create a list of 'tricks' 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  Common file types
2  Printing
3  Handling images
4  Vector graphics
5  Count files in a directory
6  Copying, renaming, moving or deleting files
7  Copying, moving or deleting directories
8  Finding files or directories on disk
9  Check the amount of disk space occupied by a directory
10  Check the space left on disks
11  Linking files
12  Read files from a data CD
13  Read/write files on a floppy
14  Ripping sound files from an audio CD
15  Converting from wav to mp3
16  Converting from wav to ogg vorbis
17  Creating a data CD
18  Creating an audio CD
19  Make backups
20  Comparing two files or two directories
21  Substituting strings in many files
22  Running a command on multiple files
23  Which computer/system am I working on?
24  Which shell is running?
25  Adding a directory to the PATH
26  Compress / uncompress files
27  Connecting to a remote computer
28  Executing commands on a remote computer
29  Setting up ssh
30  Copying file to or from a remote computer
31  Listing the hosts in a NIS domain
32  Cut'n paste
33  Setting up an ethernet card to access the internet
34  Check the speed of an ethernet connection
35  Dynamic libraries
36  Installing new software
37  Linux tutorials and howtos on the net

1  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)

2  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.

You can preview a postscript file on the screen;

gv file.ps

To put '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 priting system 'CUPS', you can access its documentation by opening the following address in a browser:

http://localhost:631/documentation.html

3  Handling images

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

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 (encapuslated postscript).

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

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

To manipulate an image, e.g. file.jpg, use

gimp file.jpg

4  Vector graphics

I personnaly 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 impress.

5  Count files in a directory

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

ls *.img | wc

6  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 directory `dir':

cp file1 dir

To rename a file:

mv file1 file2

To move a file to the directory `dir':

mv file1 dir

To delete a file:

rm file

7  Copying, moving or deleting directories

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' into '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

8  Finding files or directories on disk

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.:

find . -name '*.doc'

The 'find' command can do much more: consult 'info find' for more information.

9  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 accroding to their size:

ls -Sl

10  Check the space left on disks

To see the space available on the mounted partitions:

df

If there are quota that limit the amount of space you can use on your account:

quota

11  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).

12  Read files from a data CD

Amovible media (cd, floppies...) must be 'mounted' to make their content accessible, and 'unmounted' before ejection (see the man pagfes of mount and umount). The precise parameters depend on the configuration of your system. On some systems, 'mouting' and 'unmouting' are performed automatically thanks to the 'supermount' system.

On my system, I enter the command:

mount -t iso9660 /dev/cdrom /mnt/cdrom

/mnt/cdrom then points to the content of the CD. When you are finished, do not eject the CD manually. Rather, type:

umount /mnt/cdrom
eject

13  Read/write files on a floppy

The same goes for floppies:

mount /mnt/floppy

# the content of the floppy should now be accessible under /mnt/floppy

# IMPORTANT: before ejecting the floppy, type:
umount /mnt/floppy 

In some systems, normal users are not allowed to mount a floppy. Ask the system administrator to grant this right in /etc/fstab.

In some systems, the 'mdir' and 'mcopy' functions allow to list and copy to or from a floppy without mounting it (see 'man mtools').

14  Ripping sound files from an audio CD

To rip an entire audio CD:

cdparanoia -B

To just rip one track:

cdparanoia -w track_number file.wav

If you prefer programs with graphical interface, you may use 'grip'. One nice feature of 'grip' is that it will interrogate Free-DB to try and determine the songs' titles.

15  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:

16  Converting from wav to ogg vorbis

I use oggenc:

oggenc file.wav -o file.ogg

17  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
    
    
  3. Record on the cd (you must be root). First use
    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
    
    

18  Creating an audio CD

To record all the *.wav files 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)

19  Make backups

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

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

I use unison, which keeps two directories synchronized, transfering only new or modified files:

unison localdir ssh://remotehost/remotedir

20  Comparing two files or two directories

diff file1 file2

See also 'wdiff'

21  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

22  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 (you can check the shell with the commadn 'echo $SHELL'), then you must type 'bash' first.

23  Which computer/system am I working on?

uname -a

24  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

25  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=thepath:$PATH

Place these lines in the file '.bashrc' in your home directory to have the correct PATH 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.

26  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

27  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.

28  Executing commands on a remote computer

ssh computername command
rlogin computername command

29  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.

30  Copying file to or from a remote computer

scp -r misc remotelogin@computername:

wget

ncftp

31  Listing the hosts in a NIS domain

ypcat hosts

32  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.

33  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

34  Check the speed of an ethernet connection

mii-tool

ethtool

iperf

35  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 

36  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.

37  Linux tutorials and howtos on the net

Unix 101 series

Linux tutorials for absolute beginner


File translated from TEX by TTH, version 3.01.
On 8 Oct 2003, 13:51.