Clemens' Linux FAQ


Chr. Clemens Lee

2001-03-05

A collection of notes regarding questions I had working with Linux.

Table of Contents

1. How to remove a file in a shell script if the file exists?
2. Howto invoke make in a sub directory?
3. How to find out the last time a file has been accessed?
4. How to set a password for the anonymous ftp user?
5. How to set the time using the date command?
6. How to create a new CVS project without having to import all files immediately?
7. How to change all file names in a directory tree to lower case (after transfer from a DOS box)?
8. How to view a man page that is not in the MANPATH but whose location you know?
9. How to cut off a fixed number of characters from the content of a shell environment variable?
10. How to show the content of an rpm file before installing it?
11. How to repeat the 3rd last command from the bash history?
12. How to get the file size in a shell script?
13. How to find out which .deb package contains a single file?
14. How to get debian security updates?
15. How to increase the limit of simultanious processes allowed by the kernel? How to find out the current limit?
16. How to get a list of all text files in a directory (so you can e.g. grep afterwards on them)?
17. How to install an individual package besides from stable ones you donwloaded it somewhere from (e.g. pool)?
18. How to get printing to work with Debian Potato and an HP LaserJet 5L?
19. How to put the external zip drive to my home machine?
20. How to install Debian Woody on a new machine?
21. How to finally remove a program from the startup procedure during reboot/boot?
22. How to download Blackdown debian Java packages?
23. How to update a package from unstable in Debian stable without compromising the system more than necessary?
24. How to write text in korean with hangul font?
25. How to create a variable with an absolute path?
26. How to get the current iso date?
27. How to check if a backup with dump is identical to the original files?
28. How to use cut with text where there are multiple spaces between individual columns?
29. How to make a X Window screenshot on a remote machien?
30. How to restore a single file from a backup dump file?
31. How to install a Debian package without direct access to the net from your machine?
32. How to find the shell's process id?
33. How to print a file starting from line X?
34. How to get the last n characters from a shell variable?
35. How to show all processes for a user including the parent process ids?
36. How to set my own ip address in a shell variable?
37. How to find all suid programs?
38. How to find out the version number of the current ksh?
39. How to uudecode base64 stuff?
40. How to create a command line dialog using ksh select?
41. How to input a password in a shell script?
42. How to invoke a script during boot time (using Debian)?
43. How to check if a special value is part of the command line in a shell script?
44. Where is the file that describes all debian packages?
45. How to specify the paramater for an application in Netscape for a mime data type?
46. How to restore some files from a set of dump files?
47. How to use grep in OR mode?
48. Can you give me a short list of VI commands?
49. How to use lynx through a firewall with authentification?
50. How to start the X Window server so that it uses an tcp port?
51. How to find out which shell /bin/sh is linked to by default (bourne shell, ksh, or bash)?
52. How to find out which version of a debian package is currently installed?
53. How to upgrade a single package in debian?
54. How to synchronize local time with a time server and to also update the hardware clock?
55. How to print the last parameter from the current line with awk?
56. How to invoke an external program from an awk script?
57. How to setup bsd-ftpd or lukemftpd?
58. How to play a dvd vob file?
59. Ho to check ident server?
60. How to create a ext3 partition?
61. How can I find out which processes are using a certain partition?
62. What is the maximum partition size when ext2 will be the file system used?
63. How to change your MAC address dynamically?
64. How to find out details when using yellow pages (nis)?
65. How to find out the cpu type and model?
66. How to extract a substring using awk?
67. How to show hidden files in midnight commander?
68. How to parse shell script options?
69. How to use lynx through a proxy with a user password?
70. How to use co processes?
71. How to set the keyboard language used on the console in Debian?
72. How to print from a line all elements from position n until the last one?
73. How to read a password in a shell script so it will not be visible what gets keyed in?
74. How to remove a directory from a CVS project?
75. How to print out a left aligned string in AWK?
76. How to find out how many inodes on an ext2 partition are still free?
77. How to get yesterday's date in a script?
78. How to format an ext3 partition?
79. How to copy an avi file from a corrupt cdrom?
80. How to use aspell with a different language?
81. How to use sed to change more than a single line at once?
82. How to get the absolute number of a float value in awk?
83. How to format a big number like this: 1,428,811,513 ?
84. How to invoke a single command in mc using as two parameters a file existing in both panels?
85. What is the sed syntax to reuse a substring?
86. How to replace tabs with newlines in a file?
87. How to check an ext2 file system for errors even if it is read only mounted?
88. What does "Inode 313, i_blocks is 256, should be 232. Fix? no" mean?
89. When I log into the system for the first time my .bashrc doesn't get executed, only when I do an "bash" manually afterwards. How can this be changed?
90. How to compile your own 2.4 kernel?
91. How to change the field separator for awk?
92. How to set the other user file permissions the same as the group permissions are set?
93. How to automatically login using ssh or scp?
 
1. How to remove a file in a shell script if the file exists?
if [ -f $PP_DATA_FARLOAD/lock ]
then
rm $PP_DATA_FARLOAD/lock
fi

To add a directory which does not exist:
if [! -d $PP_DATA_FARLOAD/log ]
then
mkdir -p $PP_DATA_FARLOAD/log
fi

2. Howto invoke make in a sub directory?

SUBDIRS= \\
 payment

dev :
 @for d in $(SUBDIRS); \\
 do \\
  (cd $$d; make dev); \\
 done

3. How to find out the last time a file has been accessed?
ls -lat --time=atime
or better: ls -lu file

4. How to set a password for the anonymous ftp user?
http://www.redhat.com/support/docs/tips/FTP-Setup-Tips/guestftp.html

5. How to set the time using the date command?
date --set 12:03:45

6. How to create a new CVS project without having to import all files immediately?

export PROJECT_PATH=`pwd`
cd ~/tmp
md new_project
cd new_project
cvs -d /my/cvs/repository -m "New Project Comment" new_project new_project start
cd ..
rmdir new_project
cvs d /my/cvs/repository checkout new_project
cd new_project
cp -av CVS $PROJECT_PATH
cd $PROJECT_PATH

7. How to change all file names in a directory tree to lower case (after transfer from a DOS box)?
I have not found a good solution yet. Right now I use ls -1 * > t and edit the file t with emacs via macros and 'downcase-region' to write a script to rename everything and execute it afterwards with ". ./t" and throw the script away.

8. How to view a man page that is not in the MANPATH but whose location you know?
nroff -man /my/file.1 | more

9. How to cut off a fixed number of characters from the content of a shell environment variable?

export TEST=foo/Bar.java
echo ${TEST%.java}
Here are some examples from O'Reilly's Korn Shell Programming book:
Assume that the variable path has the value /home /billr/mem/long.file.name; then:

Expression                Result
${path##/*/}                       long.file.name
${path#/*/}              billr/mem/long.file.name
$path              /home/billr/mem/long.file.name
${path%.*}         /home/billr/mem/long.file
${path%%.*}        /home/billr/mem/long

10. How to show the content of an rpm file before installing it?
rpm -q -i -l -p example_file.rpm
(query, information, list, package file)

11. How to repeat the 3rd last command from the bash history?
Use !-3
!cl - repeats the last command that starts with 'cl'
!:0-3 next arguments - copies the first four objects from the previous command line into the next one

12. How to get the file size in a shell script?
ls -l file.txt | sed -e 's/ */ /g' | cut -d' ' -f5

Note the two spaces before the star in the sed command.

13. How to find out which .deb package contains a single file?
Use the form at the bottom of this page:
http://www.debian.org/distrib/packages

14. How to get debian security updates?
This requires a line such as deb http://security.debian.org/ potato/updates main contrib non-free in your /etc/apt/sources.list file.

15. How to increase the limit of simultanious processes allowed by the kernel? How to find out the current limit?
You have to build yourself a new kernel.
include/linux/tasks.h #define NR_TASKS 512

dselect on debian kernel-source package.
untar package
cd into
make xconfig
make-kpkg kernel_image --revision=ccl1.0
cd ..
dpkg --install kernel-image-...ccl1.0_i386.deb
lilo
reboot

I had problems getting the network running afterwords, but the problem itself was solved.

16. How to get a list of all text files in a directory (so you can e.g. grep afterwards on them)?

find . -type f | xargs file | grep " text$" | while read line 
do
   line=${line%%:*}
   grep -H "whatever you are looking for" $line 
done

17. How to install an individual package besides from stable ones you donwloaded it somewhere from (e.g. pool)?
dpkg -i file.deb and then it will complain if dependencies are not met.

18. How to get printing to work with Debian Potato and an HP LaserJet 5L?
Use ljet4 driver.

19. How to put the external zip drive to my home machine?
modprobe imm

Make sure a zip medium is in the drive.

mount -t vfat /dev/sdb4 /mnt/zip

For the internal drive, type

mount -t vfat /dev/sda4 /mnt/zip

20. How to install Debian Woody on a new machine?
Assuming you have another Linux distribution running and are connected to the internet:

wget -r ftp://ftp.de.debian.org/debian/dists/woody/main/disks-i386/3.0.14-2001-09-25/images-1.44/idepci

21. How to finally remove a program from the startup procedure during reboot/boot?
First stop the process in question. Then uninstall the program using dselect (if you don't run it, why having it installed?). Then delete the main script in /etc/init.d. Now type: update-rc.d scriptname remove

22. How to download Blackdown debian Java packages?
Add this to /etc/apt/sources.list:

deb ftp://sunsite.cnlab-switch.ch/mirror/java-linux/debian woody non-free

23. How to update a package from unstable in Debian stable without compromising the system more than necessary?
Edit /etc/apt/sources.list and add the unstable sources, apt-get update, apt-get install newpackage -s, apt-get install newpackage, outcomment unstable sources in sources.list, dselect update.

24. How to write text in korean with hangul font?
Use xemacs, ESC-x set-buffer-file-coding-system-for-read iso-2022-kr, open file, use edit-mule-toggle dings bums if necessary. Now hangul can be written and at least netscape will be able to display it as well. Netscape does not seem to like euc-kr character encoding. I have no experience using UTF-8 so far, especially with Java.

25. How to create a variable with an absolute path?
export PRG_DIR=`dirname $0` # e.g. ../clesh/bin
OLD_PWD=`pwd`
cd $PRG_DIR
PRG_DIR=`pwd`
cd OLD_PWD
echo $PRG_DIR # e.g. /usr/local/java/clesh/bin

26. How to get the current iso date?
date "+%Y-%m-%d"

For the time without seconds use: "+%T"

27. How to check if a backup with dump is identical to the original files?
E.g. restore -C -M -f hda6_0_20011223.dump

28. How to use cut with text where there are multiple spaces between individual columns?
e.g. ps -eaf | grep dtwm | grep -v grep | sed -e 's/ */ /g' | cut -d' ' -f3

Note especially the two spaces in the sed command before the star.

29. How to make a X Window screenshot on a remote machien?
Telnet to the machine, export DISPLAY=remote:0.0 && import -window root root.jpg && export DISPLAY=local:0.0 && xv root.jpg.

30. How to restore a single file from a backup dump file?

restore -i -f dump_file
help
cd
ls
add
extract
quit

31. How to install a Debian package without direct access to the net from your machine?
Download .deb file somewhere e.g. from ftp://ftp2.de.debian.org/debian/dists/potato/main/binary-i386/. Then transfer the package to your destination machine. Now type in dpkg -i file.deb. If there is a dependency problem, dpkg will tell you so.

32. How to find the shell's process id?
echo $$

Use sleep 12 & echo $! to get the process of the sleep command.

33. How to print a file starting from line X?
tail -r -n $(expr $(wc -l file | cut -c0-8) - X) file

34. How to get the last n characters from a shell variable?

export TEST="9876543210"
echo -n "Last 5 characters of $TEST: "
echo $TEST | cut -c$(expr $(expr $TEST : '.*') - 5 + 1)-

35. How to show all processes for a user including the parent process ids?
On Debian: ps j -u user

36. How to set my own ip address in a shell variable?
IP=$(ifconfig eth0); IP=${IP#*inet addr:}; echo ${IP%% *}

37. How to find all suid programs?
find / -perm 4755

38. How to find out the version number of the current ksh?

39. How to uudecode base64 stuff?
First line must be 'begin-base64 644 filename' and last line I believe '===='. Create file with uuencode -m file name if unsure.

40. How to create a command line dialog using ksh select?

select choice in "Do this" "Do that" exit
    do
        if [[ "$REPLY" = "1" ]]
	then
	    # do this ...

            break
	fi

        if [[ "$REPLY" = "2" ]]
	then
	    # do that ...

            break
	fi

	if [[ "$choice" = "exit" ]] ; then
	    exit;
	fi
    done

41. How to input a password in a shell script?

From: Barry Margolin (barmar@bbnplanet.com)
 Subject: Re: suppressing password echo in ksh 
 Newsgroups: comp.unix.misc
 Date: 1999/11/15 


In article <80prpr$rj1$1@nclient15-gui.server.virgin.net>,
Stuart Archer <stu.archer@virgin.NOSPAM.net> wrote:
<Hi Ed,
<from memory I reckon its "stty -echo/stty echo"....

You should actually use:

oldstty=`stty -g`
stty -echo
read password
stty $oldstty

This way, you don't end up turning on echoing if it wasn't on to begin
with.

People who work in Emacs shell buffers will appreciate this.

42. How to invoke a script during boot time (using Debian)?
First the script has to be in /etc/init.d. Then invoke:

update-rc.d scriptname defaults 93
With update-rc.d -n ... you can see before what will happen.

Or use: update-rc.d scriptname start 40 2 .

43. How to check if a special value is part of the command line in a shell script?

PJA=""
if echo " $@" | grep " -exporthtml " >/dev/null; 
then
  PJA="-Xbooclasspath/a:${JIM_HOME}/lib/pja.jar -Dawt.toolkit=com.eteks.awt.PJAToolkit -Djava.awt.graphicsenv=com.eteks.java2d.PJAGraphicsEnvironment"
fi

44. Where is the file that describes all debian packages?
Look in directory /var/lib/apt/lists for files ending with '_Packages'.

45. How to specify the paramater for an application in Netscape for a mime data type?
E.g. "soffice %s"

46. How to restore some files from a set of dump files?
'restore -a -i -M -f hda6_0_20020514.dump' and then use cd, ls, add, and extract.

47. How to use grep in OR mode?

echo "Hi, are you there?" | grep -E "I
you
he
she"

48. Can you give me a short list of VI commands?

Vi commands:
ZZ - save all files and quit
:q! - quit without any changes
:e! - undo all changes
:w - save file
:w filename - save file 'filename'
:x - save file and quit
:q - quit
i - insert mode
a - insert after mode
0 - beginning of line
$ - end of line
hjkl - cursor movement
:set nu - display line numbers
w - move to next word
W - move to next word without punctuation
b - move to previous word
B - move to previous word without punctuation
cw - change word
c2b - change back 2 words
c$ - change to end of line
C - same as c$
c0 - change to beginning of line
cc - change complete line
d - delete
y - yank
r - replace character
~ - change case
xp - transpose 2 characters
u - undo
U - undo changes on line
A - append text at end of line
^F - scroll forward one screen
^B - scroll backward one screen
^D - scroll forward half screen (down)
^U - scroll backward half screen (up)
z. - move current line to center of screen
nG - goto line n
^G - line number info
G - end of file
O - insert line above
o - insert line below
:r file - insert file into current file
:e load_another_file
:e# jump to other file
:n next file loaded from command line
f forward in line to 
F backwards in line to 
t forward in line to before 
T backwards in line to before 
:set number

49. How to use lynx through a firewall with authentification?

export http_proxy=http://proxyhost:proxyport/
lynx -pauth=user:password

50. How to start the X Window server so that it uses an tcp port?
On Debian 3.0 edit /etc/X11/xinit/xserverrc and outcomment the -nolisten tcp parameter.

51. How to find out which shell /bin/sh is linked to by default (bourne shell, ksh, or bash)?
If it is the pd ksh the KSH_VERSION environment variable will tell you at least.

52. How to find out which version of a debian package is currently installed?
You can of course use dselect to look up the version number. Otherwise use dpkg --search my_package to find out which package you are interested in and then use dpkg -p or dpkg -L my_package for the details. apt-cache showpkg my_package reveals also very interesting details.

53. How to upgrade a single package in debian?
How to different sources.list files and copy the one that contains also testing to sources.list. Then use dselect to update the list off all packages. Then use apt-get -s install my_package to see if you really get a new package. Then do it with apt-get install my_package.

54. How to synchronize local time with a time server and to also update the hardware clock?
ntpdate ntpservername (e.g. ntp.metas.ch) and hwclock --systohw (--utc)

55. How to print the last parameter from the current line with awk?
ls -l | awk '{ print $NF, $5 }'

56. How to invoke an external program from an awk script?
{ system( "echo bla" ) }

57. How to setup bsd-ftpd or lukemftpd?
For lukemftpd edit /etc/ftpusers and for bsd-ftpd edit /etc/ftpchroot.

58. How to play a dvd vob file?
mplayer -hardframedrop -ao sdl -nocache -aid 128 file.vob . q for quitting btw.

59. Ho to check ident server?
telnet to the ident port, then "port_on_server, port_on_client".

60. How to create a ext3 partition?
mke2fs -j /dev/hdXX

61. How can I find out which processes are using a certain partition?
lsof | grep partition (assuming partition is the mount point)

62. What is the maximum partition size when ext2 will be the file system used?
Ext2 support file system of up to 4TB size.

63. How to change your MAC address dynamically?
ifconfig eth0 hw ether aa:dd:rr:ee:ss

64. How to find out details when using yellow pages (nis)?
ypcat passwd | grep myid
ypcat group | grep mygroup

65. How to find out the cpu type and model?
cat /proc/cpuinfo

66. How to extract a substring using awk?
echo "2002-11-09" | awk '{ print substr($1, 6, 2)}'
to extract the '11' from the input.

67. How to show hidden files in midnight commander?
F9 and then options, turn hidden files on.

68. How to parse shell script options?
Put this at the top of your script

#! /bin/sh

while getopts hv option
do
  case $option in
     h) echo "usage: dumphome [-hv] dest_part [dump_level]"; exit;;
     v) echo "dumphome, version 1.0, 2002-11-23, author: Chr. Clemens Lee"; exit;;
  esac
done
or e.g.
while getopts n: option
do
  case $option in
     n) n=$OPTARG ; shift ; shift ;; 
  esac
done

69. How to use lynx through a proxy with a user password?
export http_proxy=http://proxyhost:port/
lynx -pauth=user:password http://my.web.page.com/

70. How to use co processes?
Here is an example (no output, just some action internally):

#! /bin/ksh
 
dc |&
exec 3>&p
exec 4<&p
 
i="0"
while [ $i -lt 50 ]
do
  print -u3 "3
5
+
p
"
  i=`expr $i + 1`
done
print -u3 "q
"

while read -u4 line
do
echo $line
done

71. How to set the keyboard language used on the console in Debian?
dpkg-reconfigure console-common

72. How to print from a line all elements from position n until the last one?
ls -l | awk '{ for (i = 9; i <= NF; i++) print $i }'

73. How to read a password in a shell script so it will not be visible what gets keyed in?

printf "Password: "
oldstty=$(stty -g)
stty -echo
read password
stty $oldstty
echo

74. How to remove a directory from a CVS project?
Assuming the directory is empty: cvs up -P directory

75. How to print out a left aligned string in AWK?
printf "%-8s %s\n", $1, $2

76. How to find out how many inodes on an ext2 partition are still free?
df -i

77. How to get yesterday's date in a script?
perl -e 'use Date::Manip; $date=UnixDate("yesterday", "%Y%m%d"); print $date . "\n"'

78. How to format an ext3 partition?
mke2fs -j /dev/hda9

79. How to copy an avi file from a corrupt cdrom?
avicut -o output.avi -b 1,1631 -b 1638,999999 /cdrom/input.avi

80. How to use aspell with a different language?
aspell --master=/usr/lib/aspell/german check sometext.txt

81. How to use sed to change more than a single line at once?

cat tt.xml | sed "/<\/sect2/{
N
s/<\/sect2><para>\n[ 	 ]*<\/para>/<\/sect2>/
}
"

82. How to get the absolute number of a float value in awk?
print int( length( $1 ) / 3 )

83. How to format a big number like this: 1,428,811,513 ?

size=$(echo $size| awk '{ for( i=0;( i < length( $1 ) - 1 ); i = i + 1 ) 
{
  printf "%s", substr($1,i+1,1)
  if ( (length( $1 ) - 1 - i) % 3 == 0 ) printf "\x27"
}
printf "%s", substr( $1, length( $1 ) )}')

84. How to invoke a single command in mc using as two parameters a file existing in both panels?
select the file in the first panel and then type:
command "%d/%f" "%D/%f"

85. What is the sed syntax to reuse a substring?
echo "aaadddaaabbbcccbbb" | sed 's/\(a*\)b*/\1/'

86. How to replace tabs with newlines in a file?
perl -p -e 's/\t/\n/g;' file > file2 && mv file2 file
or perl -pi -e 's/\t/\n/g;' file

87. How to check an ext2 file system for errors even if it is read only mounted?
e2fsck -n /dev/hda1

88. What does "Inode 313, i_blocks is 256, should be 232. Fix? no" mean?
debugfs /dev/hdxy
ncheck 313 quit

89. When I log into the system for the first time my .bashrc doesn't get executed, only when I do an "bash" manually afterwards. How can this be changed?
Put this into your .bash_profile file:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

90. How to compile your own 2.4 kernel?
Install sources under /usr/src and create a link named linux to it.

cd linux
cp /boot/config-2.4.18-k7 .config
make oldconfig
optionally now: make menuconfig
make dep
make bzImage
make modules
make modules_install
cp arch/i386/boot/bzImage /boot/vmlinuz-2.4.18
As it turns out, lately just installing the first .deb file is already sufficient. But in order to create your own debian kernel package, do the following:
Just copy the config file like above.
make-kpkg --append-to-version "-ccl2" --initrd kernel_image
cd ..
dpkg -i kernel-image...deb
mkinitrd -o /boot/initrd.img-2.4.18-ccl2 /lib/modules/2.4.18-ccl2
(doesn't seem to be necessary: cd /
rm initrd.img
ln -s boot/initrd.img-2.4.18-ccl2 initrd.img)
lilo

In order to also compile the nvidia driver, continue with:
tar -zxf nvidia-kernel-src.tar.gz
cd linux
make-kpkg --append-to-version "-ccl2" modules_image
cd ..
dpkg -i nvidia-kernel-2.4.18-ccl2_1.0.2880-1+10.00.Custom_i386.deb

91. How to change the field separator for awk?
awk -v FS=';' ...

92. How to set the other user file permissions the same as the group permissions are set?
chmod -R o=g .

93. How to automatically login using ssh or scp?

Look in your ~/.ssh directory. There should be two files, id_rsa and id_rsa.pub. If not, create them using ssh-keygen -t rsa. 
Append your local id_rsa.pub to the remote host's ~/.ssh/authorized_keys. 
The file mode of ~/.ssh/authorized_keys must be 644. You can assure this with chmod 644 ~/.ssh/authorized_keys 
For this to work, you might have to change your /etc/sshd_config and add the following lines: 
RSAAuthentication yes 
RSAAuthentication yes