-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux.sgml
2850 lines (2712 loc) · 68.8 KB
/
linux.sgml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<qandaset defaultlabel='qanda' id='linux'>
<title>Linux Frequently Asked Questions</title>
<qandaentry>
<question>
<para>
What version of Linux am I running?
</para>
</question>
<answer>
<para>
<orderedlist>
<listitem>
<para>
Use data from the
<ulink url="http://www.linuxbase.org/"><citetitle>Linux Standards Base</citetitle></ulink>, on
<ulink url="http://www.debian.org/"><citetitle>Debian</citetitle></ulink> it can be read from
<filename>/etc/lsb-release</filename>.
</para>
</listitem>
<listitem>
<para>
To get your system information, use
<programlisting><![CDATA[
uname -a
]]></programlisting>
</para>
</listitem>
<listitem>
<para>
To show release information (Debian, Ubuntu), use
<programlisting><![CDATA[
cat /etc/lsb-release
]]></programlisting>
</para>
</listitem>
</orderedlist>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to create a picture summary of images in a directory?
</para>
</question>
<answer>
<para>
This command will generate a postcard size summary of images
<programlisting><![CDATA[
convert 'vid:*.jpg' directory.miff
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to resize directory for preview with the GIMP?
</para>
</question>
<answer>
<para>
<programlisting><![CDATA[
for f in *.jpg; do convert -size 80x60 $f ${f#../};done
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to rotate a picture right by 90 degrees?
</para>
</question>
<answer>
<para>
<programlisting><![CDATA[
mogrify -rotate 90 p6260018.jpg
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to total directory sizes?
</para>
</question>
<answer>
<para>
<programlisting><![CDATA[
du -sch [directory] ...
]]></programlisting>
</para>
<para>
<example>
<title>To summarise size of a list of directories</title>
<screen>
$ du -sch pictures/ music/ documents/
8.0G pictures/
698M music/
1.2G documents/
9.9G total
</screen>
</example>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to clear or empty log files?
</para>
</question>
<answer>
<para>
To maintain a log directory
<programlisting><![CDATA[
cp file.log file.log.0
cat /dev/null > file.log
compress file.log.0
]]></programlisting>
</para>
<para>
General rule of thumb
</para>
<para>
Do not remove log files - null them. If you just remove the log file and a
process has the file open, this space will not be reallocated until the demon
is stopped.
</para>
<para>
To empty a log file
<programlisting><![CDATA[
cat /dev/null > file.log
find /tmp -name '*.log' -exec cp /dev/null {} \;
echo > file.log
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to label tapes?
</para>
</question>
<answer>
<para>
You can label tapes by
<programlisting><![CDATA[
mt -f <tape device> rew
echo "tape label and date etc"|dd of=<tape device>
dump filesystems
mt -f <tape device> rewoffl.
]]></programlisting>
</para>
<para>
When you want to retrieve the tape label do
<programlisting><![CDATA[
dd if=<tape device>
]]></programlisting>
</para>
<para>
When retrieving filesystems, just remember to skip the first save set as it
is the label.
To retrieve the 2nd filesystem dumped do
<programlisting><![CDATA[
mt -f <tape device> rew
mt -f <no rewind tape device> fsf 2
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to forward email?
</para>
</question>
<answer>
<para>
Add the following entry to <filename>$HOME/.forward</filename>
<screen>
\frank@localhost, frankhjung@linux.com
</screen>
This says that mail should keep one copy for the local account, and send one
copy to my internet account.
</para>
<para>
When on vacation, do this
<screen>
\frank, "| /usr/bin/vacation -r 0 frank"
</screen>
This keeps one copy for the local account, and sends a vacation message stored
in a file called <filename>$HOME/.vacation.msg</filename> back to the sender of any
mail.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
What mail is currently queued to send?
</para>
</question>
<answer>
<para>
If you are using <command>sendmail(8)</command>, then
<screen>
sendmail -bp
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
What does the ? mark mean under the tty field in the process status (<command>ps -ef</command>)?
</para>
</question>
<answer>
<para>
The "?" mark indicates that the process is not attached to any terminal.
This happens typically for daemons, where the process becomes the leader of the
group and detaches itself from the terminal.
</para>
<para>
See also <command>pstree(1)</command> and <command>ps axf</command>.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to show which users have not logged on recently?
</para>
</question>
<answer>
<para>
The following script reports users who have recently signed in/out.
Limitations: doesn't register remote logins.
<programlisting><![CDATA[
#!/bin/ksh
# Display Recent Logins
# This is really slow for large "last" - so simplify
last | cut -d" " -f1 | sort -u > ./last.out
for i in $( cut -d: -f1 /etc/passwd | sort -u )
do
grep $i ./last.out > /dev/null
if [ $? -eq 0 ]
then print $i"\tOk"
else print $i"\tNone"
fi
done
rm ./last.out
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to format an ASCII file into PostScript?
</para>
</question>
<answer>
<para>
This example uses <command>a2ps(1)</command>. Print PostScript to an ASCII
printer by piping to <command>ghostview(1)</command>
<programlisting><![CDATA[
lp filename | a2ps -2q --strip=3 -o -|gs - -sDEVICE=ljet3 -sOutputFile=|lpr
]]></programlisting>
Alternatively
<programlisting><![CDATA[
gv filename | a2ps -P display
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How can I collect all input and output in a terminal session?
</para>
</question>
<answer>
<para>
Try <command>script(1)</command>, for example
<programlisting><![CDATA[
script -a filename // this appends to filename
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to send a binary files when using email?
</para>
</question>
<answer>
<para>
Compress with uuencoding
<screen>
tar cf - files | gzip | uuencode files.tar.gz > files.tar.gz.uu
</screen>
To extract use
<screen>
uudecode -p files.tar.gz.uu | gzip -c -d - | tar xf -
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to generate random numbers from a shell?
</para>
</question>
<answer>
<para>
To generate a random number, using Korn Shell, between $LO and $HI
(inclusive), use the following code
<programlisting><![CDATA[
#/bin/ksh
(( RANGE = HI - LO + 1 ))
(( NUMBER = RANDOM % RANGE + LO ))
# $NUMBER now contains your "random" number in the specified range.
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to remove a directory that is too deep for <command>ls(1)</command> or
<command>rm -R</command>?
</para>
</question>
<answer>
<para>
Use following script (which does not use <command>pwd(1)</command>)
<programlisting><![CDATA[
#!/usr/local/bin/bash
# Recursive Traversal of Directories
rm_branch () {
# go to bottom of directory tree
if [[ -d $dirName ]]; then
print "going into directory $dirName"
cd $dirName;
rm_branch
else
print "at bottom of $dirName"
return 0
fi
cd ..
rmdir $dirName;
}
# test it
if [[ -d $dirName ]]; then
dirName = ${1};
rm_branch;
fi
]]></programlisting>
</para>
<para>
This is easier to implement, but very slow to execute
<programlisting><![CDATA[
#!/bin/local/bin/bash
rm_branch() {
if [[ -d $dirName ]]; then
DIR=$dirName
while [ -d $dirName ]
do
mv $dirName $dirName.x
cd $dirName.x
mv $dirName ../$dirName
cd ..
rm -r $dirName.x
done
fi
}
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
What devices are available?
</para>
</question>
<answer>
<para>
Use the <command>lspci(8)</command>
<screen>
lspci -v
</screen>
This will provide a list of devices on this machine. See also
<command>lsusb(8)</command> for a list of USB devices, or
<command>fdisk(8)</command>. Note that devices can be mounted by device, UUID
or label. To list mounted devices by UUID use blkid(8). Each will be mapped to
an entries under <filename>/dev/disk/</filename>.
</para>
<para>
To list block devices use <command>blkid(8)</command>. This will also show the
associated UUID.
</para>
<para>
Another tool is <command>devkit-disks(1)</command>. This has a dump option
that will list very detailed information of mounted devices. For example
<screen>
devkit-disks --dump
</screen>
Information about a singe device can also be captured, but for that you
need the name of the device file. Use a combination of commands, like
<screen>
devkit-disks --enumerate
devkit-disks --enumerate-device-files
devkit-disks --show-info /dev/disk/by-uuid/10807b5e-24f6-4031-bdd1-bd07c08afaa3
</screen>
</para>
<para>
To list input devices use <command>xinput(1)</command>. It is a utility that
also allows you to configure and test X input devices.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to copy an audio CD (and convert to MP3 or OGG)?
</para>
</question>
<answer>
<orderedlist>
<listitem>
<para>
This can be done using <command>dagrab(1)</command>, to dump an audio CD to
disk and <command>lame(1)</command> to convert WAV to MP3.
</para>
<para>
FreeBSD: To copy an entire audio CD to disk as WAV files
<screen>dagrab -a -d /dev/acd0c -f "Sacred_Fire-%02d.wav"</screen>
Linux: To copy an entire audio CD to disk as WAV files
<screen>cdda2wav -x -B -D 0,0,0</screen>
Or the more friendly
<screen>nice cdda2wav -D 0,1,0 -alltracks -x -Q -H -S 48 -v disable &</screen>
You may also try the <command>cdparanoia(1)</command> with
<screen>cdparanoia -w -B -d /dev/dvd </screen>
or more simply
<screen>cdparanoia -w -B </screen>
The advantage of <command>cdparanoia(1)</command> is better quality and easier
access to specific tracks.
</para>
</listitem>
<listitem>
<para>
FreeBSD: To copy only certain tracks of an audio CD to disk as WAV files
<screen>dagrab -d /dev/acd0c -f "%02d-Sacred_File.wav" 1 2 3 5 7 11</screen>
</para>
<para>
Where the options to <command>dagrab(1)</command> are
<simplelist type='horiz' columns='2'>
<member>-a</member>
<member>Dumps all tracks and ignores the optional track list.</member>
<member>-d</member>
<member>Sets the cdrom device name.</member>
<member>-f</member>
<member>Sets the output name for dumping tracks.</member>
<member>-i</member>
<member>Prints out the track list from the current cd.</member>
</simplelist>
This will copy the Audio CD's to disk as WAV files.
</para>
</listitem>
<listitem>
<para>
FreeBSD: Don't write these WAV files straight to disk though,
as you will get a nasty audible "click" recorded between each track.
To eliminate this click use <command>sox(1)</command> to convert to raw
audio
<screen>for f in *.wav; do sox $f ${f%%wav}raw; done</screen>
</para>
</listitem>
<listitem>
<para>
Linux: You can seperate these file into volumes with
<screen>v=01 && for f in audio_??.wav; do mv $f audio_${v}_${f##audio_}; done</screen>
</para>
</listitem>
<listitem>
<para>
To convert WAV to OGG Vorbis format use
<command>oggenc(1)</command>
<screen>oggenc *.wav</screen>
</para>
</listitem>
<listitem>
<para>
To convert WAV to MP3 use
<command>lame(1)</command>
<screen>for w in *.wav; do lame -h $w ${w%%wav}mp3; done</screen>
</para>
</listitem>
<listitem>
<para>
To convert MP3 to OGG use
<command>mp32ogg(1)</command>
<screen>mp32ogg --preserve-timestamp [dir1] [dir2] ...</screen>
</para>
</listitem>
<listitem>
<para>
To convert OGG to MP3 use
<command>soundconvert.pl(1)</command>
<screen>for f in *.ogg; do soundconvert -o MP3 "$f"; done</screen>
</para>
</listitem>
<listitem>
<para>
To determine space used by mp3 files use
<screen>du -mc *.mp3</screen>
Or by album
<screen>for ((i=1;i<10;i++)); do du -cm audio_0${i}_??.mp3; done</screen>
</para>
</listitem>
<listitem>
<para>
FreeBSD: To write to CD use <command>burncd(1)</command>:
<screen>burncd -s 8 -f /dev/acd0c audio *.raw fixate</screen>
</para>
<para>
Linux: To write to CD use <command>cdrecord(1)</command>:
<screen>cdrecord -v -ignsize speed=6 dev=/dev/cdrom -dao -useinfo *.wav</screen>
or, which uses TAO single session write mode:
<screen>cdrecord -v speed=8 dev=0,0 *.wav</screen>
</para>
</listitem>
<listitem>
<para>
Finally, if you wish to update an MP3 files ID3 tag, use <command>mid3v2</command>,
<screen>for f in *.mp3; do mid3v2 -t ${f%%.mp3} -A "The Pod Delusion" $f ; done</screen>
This sets the title and Album.
</para>
</listitem>
</orderedlist>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to copy data CD?
</para>
</question>
<answer>
<para>
<orderedlist>
<listitem>
<para>
Copy a data CD
<screen>
dd if=/dev/cdrom of=image.iso
</screen>
This command can also be used to write an ISO image to a USB device
<screen>
dd if=/path/to/iso of=/dev/usb_device
</screen>
</para>
</listitem>
<listitem>
<para>
Check image before writing
<screen>
mount -t iso9660 -o loop image.iso /mnt/cdrom
</screen>
This uses the loop back device. See <command>mount(8)</command> and search
for a section entitled "THE LOOP DEVICE".
</para>
</listitem>
<listitem>
<para>
Write the image to the CD
<screen>
cdrecord -v -speed=6 dev=/dev/cdrom image.iso
</screen>
</para>
</listitem>
</orderedlist>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to record a multi-session CD?
</para>
</question>
<answer>
<para>
This answer is for use of <command>cdrecord(1)</command>.
Recall for a single (or first) session CD, follow this plan
<orderedlist>
<listitem>
<para>
Find location of CDRW device. This will be used in dev option of <command>cdrecord(1)</command>)
<screen>
cdrecord -scanbus
</screen>
</para>
</listitem>
<listitem>
<para>
Write ISO image
<screen>
mkisofs -R -J -o image.iso /thedirectory/
</screen>
</para>
</listitem>
<listitem>
<para>
Write the image to the CD
<screen>
cdrecord -v -speed=6 dev=/dev/cdrom image.iso
</screen>
</para>
</listitem>
</orderedlist>
Now, for a multi-session CD use
<orderedlist>
<listitem>
<para>
Where did last session finish?
<screen>
info=`cdrecord -msinfo dev=/dev/cdrom`
</screen>
</para>
</listitem>
<listitem>
<para>
Use the previous session information to build new ISO image
<screen>
mkisofs -C $info -o image.iso /thedirectory
</screen>
</para>
</listitem>
<listitem>
<para>
Add -multi option when writing multi-session disks
<screen>
cdrecord -v -multi -speed=6 dev=/dev/cdrom image.iso
</screen>
</para>
</listitem>
</orderedlist>
</para>
<para>
To test an ISO image before writing, use
<screen>
mount -t iso9660 -o loop image.raw /mnt/image
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to blank a re-writable CD?
</para>
</question>
<answer>
<para>
This answer is for use of <command>cdrecord(1)</command>.
</para>
<para>
To blank a CDRW use
<screen>
cdrecord -v dev=/dev/cdrom blank=fast
</screen>
Other options for blank are all and session. This uses <command>wodim(1)</command> under the covers.
<screen>
wodim blank=fast dev=/dev/cdrom
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to manage a CUPS printer?
</para>
</question>
<answer>
<para>
<orderedlist>
<listitem>
<para>
CUPS has a web management interface here
<ulink url="http://localhost:631/admin">http://localhost:631/admin</ulink>.
</para>
</listitem>
<listitem>
<para>
As superuser start CUPS printer daemon with
<screen>
/etc/init.d/cupsys start
</screen>
Cups configuration is in <filename>/etc/cups/cupsd.conf</filename>.
</para>
</listitem>
<listitem>
<para>
To see printer status use
<screen>
lpstat -t
</screen>
</para>
</listitem>
<listitem>
<para>
To remove all spooled job entries use
<screen>
lprm -P OfficeJet -
</screen>
</para>
</listitem>
</orderedlist>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
Is it possible to grep through a directory and all subdirectory's of that
directory?
</para>
</question>
<answer>
<para>
<screen>
$ find directory_name -print | xargs grep required_pattern
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to FTP using a script?
</para>
</question>
<answer>
<para>
Use the $HOME/.netrc resource script. For example
<programlisting><![CDATA[
#!/bin/ksh
#
# Start of Program Timestamp
#
echo `date +'%Y-%m-%d %T Program Started'` $0
# define constants
REMOTEHOST=255.255.255.255 # your IP address here
FILE="file_name_here"
# Create .netrc file in $HOME the ftp resource script
echo "machine $REMOTEHOST
login username
password userpassword
macdef init
cd directory_name_here
binary
get $FILE
bye
" > $HOME/.netrc
# for ftp to use the .netrc from $HOME you MUST have these rights
chmod 600 $HOME/.netrc
ftp $REMOTEHOST
# Now remove .netrc
rm -f $HOME/.netrc
#
# End of Program Timestamp
#
echo `date +'%Y-%m-%d %T Program Ended'` $0
]]></programlisting>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How can you turn off cookies?
</para>
</question>
<answer>
<para>
You can forcibly switch off cookies in Communicator/Navigator by
<command>
rm -f ~/.netscape/cookies
ln -s /dev/null ~/.netscape/cookies
</command>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to add five years to a date and display in following format dd-mmm-YYYY?
</para>
</question>
<answer>
<para>
From a shell script this can be achieved by
<screen>
date "+%d-%b-$((year=$(date +%Y)+5))"
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
What is the regular expression need to match a 24 hour clock?
</para>
</question>
<answer>
<para>
Try
<screen>
([01]?[0-9]|2[0-3]):[0-5][0-9]
</screen>
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
Is there a way to change the date timestamp on a file?
</para>
</question>
<answer>
<para>
Use <command>touch(1)</command>, and using the arguments (months, days, hours,
minutes, optional century and years, optional seconds) instead of the
current time
<screen>
touch -t MMDDhhmm[[CC]YY][.ss]
</screen>
If the -t option does not exist read the man page for the equivalent.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How can you extract from a corrupted TAR file?
</para>
</question>
<answer>
<para>
In many case if there is a corrupted tar file, the following command can
be used in an attempt to extract the file
<screen>
cat [tar-filename] | tar -xvf -
</screen>
Where "-" is the <filename>STDOUT</filename>.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How to compare files and show differences?
</para>
<para>
I have 2 text files, (text1 and text2), which are both about 2000 lines long,
and almost identical, except for some minor differences.
How do I print lines that appear only in file text1, and not in the file text2?
How can I print the differences into a third file?
</para>
</question>
<answer>
<para>
Use <command>comm(1)</command> command.
<screen>
comm text1 text2
</screen>
This will print 3 columns
<simplelist type='horiz' columns='1'>
<member>1st column contains lines in file text1 only</member>
<member>2nd column contains lines in file text2 only</member>
<member>3rd column contains lines that are in both file text1 and text2</member>
</simplelist>
You can then mask any of these columns by
<screen>
comm -23 text1 text2
</screen>
This will not print column2 and column3, giving you lines available in
text1 and not in text2. This output can then be redirected to a file.
</para>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>
How do you do decimal calculations in a Shell script?
</para>
</question>
<answer>
<para>
Use <command>bc(1)</command>
<example>
<title>To calculate 12/3</title>
<screen>
echo 12/3 | bc -l
4.00000000000000000000
</screen>
</example>
So to do this in a script do something like
<programlisting><![CDATA[
#!/bin/ksh
# Decimal Calculations
if [[ $# -eq 3 ]]; then
echo "$1$2$3" | bc -l
fi
]]></programlisting>
</para>
<para>
As an aside, <command>bc(1)</command> can also be scripted to perform calculations.
<example>
<title>Count from 1 to 10</title>
<programlisting><![CDATA[
#!/usr/bin/bc -q
for ( i=0; i<10; i++) {
print (i+1) % 12, "\n"
}
quit
]]></programlisting>
</example>
Which is just a complicated way to count from 1 to 10.
</para>
<para>
In <command>bash(1)</command> you can count from 1 to 10 by
<programlisting><![CDATA[
for ((i=1;i<11;i++));
do
echo $i;
read;
done
]]></programlisting>
</para>
<para>
You can take it one step further by using <command>expr(1)</command>
<programlisting><![CDATA[
for ((i=4;i<9;i++));
do
echo mv ring4-$i.jpg ring4-0`expr $i - 3`.jpg;
done