Debian Codegraph @

HTML/filearchiv.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >File and Archiving Commands</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="External Filters, Programs and Commands"
15 HREF="external.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Text Processing Commands"
18 HREF="textproc.html"><LINK
19 REL="NEXT"
20 TITLE="Communications Commands"
21 HREF="communications.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="SECT1"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="textproc.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 16. External Filters, Programs and Commands</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="communications.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="FILEARCHIV"
96 ></A
97 >16.5. File and Archiving Commands</H1
98 ><DIV
99 CLASS="VARIABLELIST"
100 ><P
101 ><B
102 ><A
103 NAME="FAARCHIVING1"
104 ></A
105 >Archiving</B
106 ></P
107 ><DL
108 ><DT
109 ><A
110 NAME="TARREF"
111 ></A
112 ><B
113 CLASS="COMMAND"
114 >tar</B
115 ></DT
116 ><DD
117 ><P
118 >The standard UNIX archiving utility.
119
120 <A
121 NAME="AEN11885"
122 HREF="#FTN.AEN11885"
123 >[1]</A
124 >
125
126 Originally a
127 <I
128 CLASS="WORDASWORD"
129 >Tape ARchiving</I
130 > program, it has
131 developed into a general purpose package that can handle
132 all manner of archiving with all types of destination
133 devices, ranging from tape drives to regular files to even
134 <TT
135 CLASS="FILENAME"
136 >stdout</TT
137 > (see <A
138 HREF="special-chars.html#EX58"
139 >Example 3-4</A
140 >). GNU
141 <I
142 CLASS="FIRSTTERM"
143 >tar</I
144 > has been patched to accept
145 various compression filters, for example: <B
146 CLASS="COMMAND"
147 >tar
148 czvf archive_name.tar.gz *</B
149 >, which recursively
150 archives and <A
151 HREF="filearchiv.html#GZIPREF"
152 >gzips</A
153 >
154 all files in a directory tree except <A
155 HREF="external.html#DOTFILESREF"
156 >dotfiles</A
157 > in the current
158 working directory (<A
159 HREF="variables2.html#PWDREF"
160 >$PWD</A
161 >).
162
163 <A
164 NAME="AEN11896"
165 HREF="#FTN.AEN11896"
166 >[2]</A
167 >
168 </P
169 ><P
170 >Some useful <B
171 CLASS="COMMAND"
172 >tar</B
173 > options:
174 <OL
175 TYPE="1"
176 ><LI
177 ><P
178 ><TT
179 CLASS="OPTION"
180 >-c</TT
181 > create (a new
182 archive)</P
183 ></LI
184 ><LI
185 ><P
186 ><TT
187 CLASS="OPTION"
188 >-x</TT
189 > extract (files from
190 existing archive)</P
191 ></LI
192 ><LI
193 ><P
194 ><TT
195 CLASS="OPTION"
196 >--delete</TT
197 > delete (files
198 from existing archive)</P
199 ><DIV
200 CLASS="CAUTION"
201 ><TABLE
202 CLASS="CAUTION"
203 WIDTH="90%"
204 BORDER="0"
205 ><TR
206 ><TD
207 WIDTH="25"
208 ALIGN="CENTER"
209 VALIGN="TOP"
210 ><IMG
211 SRC="common/caution.png"
212 HSPACE="5"
213 ALT="Caution"></TD
214 ><TD
215 ALIGN="LEFT"
216 VALIGN="TOP"
217 ><P
218 >This option will not work on magnetic tape
219 devices.</P
220 ></TD
221 ></TR
222 ></TABLE
223 ></DIV
224 ></LI
225 ><LI
226 ><P
227 ><TT
228 CLASS="OPTION"
229 >-r</TT
230 > append (files to
231 existing archive)</P
232 ></LI
233 ><LI
234 ><P
235 ><TT
236 CLASS="OPTION"
237 >-A</TT
238 > append
239 (<I
240 CLASS="FIRSTTERM"
241 >tar</I
242 > files to
243 existing archive)</P
244 ></LI
245 ><LI
246 ><P
247 ><TT
248 CLASS="OPTION"
249 >-t</TT
250 > list (contents of
251 existing archive)</P
252 ></LI
253 ><LI
254 ><P
255 ><TT
256 CLASS="OPTION"
257 >-u</TT
258 > update archive</P
259 ></LI
260 ><LI
261 ><P
262 ><TT
263 CLASS="OPTION"
264 >-d</TT
265 > compare archive with
266 specified filesystem</P
267 ></LI
268 ><LI
269 ><P
270 ><TT
271 CLASS="OPTION"
272 >--after-date</TT
273 > only process
274 files with a date stamp <SPAN
275 CLASS="emphasis"
276 ><I
277 CLASS="EMPHASIS"
278 >after</I
279 ></SPAN
280 >
281 specified date</P
282 ></LI
283 ><LI
284 ><P
285 ><TT
286 CLASS="OPTION"
287 >-z</TT
288 > <A
289 HREF="filearchiv.html#GZIPREF"
290 >gzip</A
291 > the archive</P
292 ><P
293 >(compress or uncompress, depending on whether
294 combined with the <TT
295 CLASS="OPTION"
296 >-c</TT
297 > or
298 <TT
299 CLASS="OPTION"
300 >-x</TT
301 >) option</P
302 ></LI
303 ><LI
304 ><P
305 ><TT
306 CLASS="OPTION"
307 >-j</TT
308 >
309 <A
310 HREF="filearchiv.html#BZIPREF"
311 >bzip2</A
312 > the
313 archive</P
314 ></LI
315 ></OL
316 >
317 </P
318 ><DIV
319 CLASS="CAUTION"
320 ><TABLE
321 CLASS="CAUTION"
322 WIDTH="90%"
323 BORDER="0"
324 ><TR
325 ><TD
326 WIDTH="25"
327 ALIGN="CENTER"
328 VALIGN="TOP"
329 ><IMG
330 SRC="common/caution.png"
331 HSPACE="5"
332 ALT="Caution"></TD
333 ><TD
334 ALIGN="LEFT"
335 VALIGN="TOP"
336 ><P
337 >It may be difficult to recover data from a
338 corrupted <I
339 CLASS="FIRSTTERM"
340 >gzipped</I
341 > tar
342 archive. When archiving important files, make multiple
343 backups.</P
344 ></TD
345 ></TR
346 ></TABLE
347 ></DIV
348 ></DD
349 ><DT
350 ><A
351 NAME="SHARREF"
352 ></A
353 ><B
354 CLASS="COMMAND"
355 >shar</B
356 ></DT
357 ><DD
358 ><P
359 ><I
360 CLASS="FIRSTTERM"
361 >Shell archiving</I
362 > utility.
363 The text and/or binary files in a shell archive are
364 concatenated without compression, and the resultant
365 archive is essentially a shell script, complete with
366 <SPAN
367 CLASS="TOKEN"
368 >#!/bin/sh</SPAN
369 > header, containing all the
370 necessary unarchiving commands, as well as the files
371 themselves. Unprintable binary characters in the target
372 file(s) are converted to printable ASCII characters in the
373 output <I
374 CLASS="FIRSTTERM"
375 >shar</I
376 > file. <I
377 CLASS="FIRSTTERM"
378 >Shar
379 archives</I
380 > still show up in Usenet newsgroups,
381 but otherwise <B
382 CLASS="COMMAND"
383 >shar</B
384 > has been replaced
385 by <B
386 CLASS="COMMAND"
387 >tar</B
388 >/<B
389 CLASS="COMMAND"
390 >gzip</B
391 >.
392 The <B
393 CLASS="COMMAND"
394 >unshar</B
395 > command unpacks
396 <I
397 CLASS="FIRSTTERM"
398 >shar</I
399 > archives.</P
400 ><P
401 >The
402 <B
403 CLASS="COMMAND"
404 >mailshar</B
405 > command is a Bash script that
406 uses <B
407 CLASS="COMMAND"
408 >shar</B
409 > to concatenate multiple files
410 into a single one for e-mailing.
411 This script supports compression and <A
412 HREF="filearchiv.html#UUENCODEREF"
413 >uuencoding</A
414 >.</P
415 ></DD
416 ><DT
417 ><A
418 NAME="ARREF"
419 ></A
420 ><B
421 CLASS="COMMAND"
422 >ar</B
423 ></DT
424 ><DD
425 ><P
426 >Creation and manipulation utility for archives, mainly
427 used for binary object file libraries.</P
428 ></DD
429 ><DT
430 ><A
431 NAME="RPMREF"
432 ></A
433 ><B
434 CLASS="COMMAND"
435 >rpm</B
436 ></DT
437 ><DD
438 ><P
439 >The <I
440 CLASS="FIRSTTERM"
441 >Red Hat Package Manager</I
442 >, or
443 <B
444 CLASS="COMMAND"
445 >rpm</B
446 > utility provides a wrapper for
447 source or binary archives. It includes commands for
448 installing and checking the integrity of packages, among
449 other things.</P
450 ><P
451 >A simple <B
452 CLASS="COMMAND"
453 >rpm -i package_name.rpm</B
454 >
455 usually suffices to install a package, though there are many
456 more options available.</P
457 ><DIV
458 CLASS="TIP"
459 ><TABLE
460 CLASS="TIP"
461 WIDTH="90%"
462 BORDER="0"
463 ><TR
464 ><TD
465 WIDTH="25"
466 ALIGN="CENTER"
467 VALIGN="TOP"
468 ><IMG
469 SRC="common/tip.png"
470 HSPACE="5"
471 ALT="Tip"></TD
472 ><TD
473 ALIGN="LEFT"
474 VALIGN="TOP"
475 ><P
476 ><TT
477 CLASS="USERINPUT"
478 ><B
479 >rpm -qf</B
480 ></TT
481 > identifies which package a
482 file originates from.</P
483 ><P
484 > <TABLE
485 BORDER="0"
486 BGCOLOR="#E0E0E0"
487 WIDTH="90%"
488 ><TR
489 ><TD
490 ><PRE
491 CLASS="SCREEN"
492 > <TT
493 CLASS="PROMPT"
494 >bash$ </TT
495 ><TT
496 CLASS="USERINPUT"
497 ><B
498 >rpm -qf /bin/ls</B
499 ></TT
500 >
501 <TT
502 CLASS="COMPUTEROUTPUT"
503 >coreutils-5.2.1-31</TT
504 >
505 </PRE
506 ></TD
507 ></TR
508 ></TABLE
509 >
510 </P
511 ></TD
512 ></TR
513 ></TABLE
514 ></DIV
515 ><DIV
516 CLASS="TIP"
517 ><TABLE
518 CLASS="TIP"
519 WIDTH="90%"
520 BORDER="0"
521 ><TR
522 ><TD
523 WIDTH="25"
524 ALIGN="CENTER"
525 VALIGN="TOP"
526 ><IMG
527 SRC="common/tip.png"
528 HSPACE="5"
529 ALT="Tip"></TD
530 ><TD
531 ALIGN="LEFT"
532 VALIGN="TOP"
533 ><P
534 ><TT
535 CLASS="USERINPUT"
536 ><B
537 >rpm -qa</B
538 ></TT
539 > gives a
540 complete list of all installed <I
541 CLASS="FIRSTTERM"
542 >rpm</I
543 > packages
544 on a given system. An <TT
545 CLASS="USERINPUT"
546 ><B
547 >rpm -qa package_name</B
548 ></TT
549 >
550 lists only the package(s) corresponding to
551 <TT
552 CLASS="FILENAME"
553 >package_name</TT
554 >.</P
555 ><P
556 > <TABLE
557 BORDER="0"
558 BGCOLOR="#E0E0E0"
559 WIDTH="90%"
560 ><TR
561 ><TD
562 ><PRE
563 CLASS="SCREEN"
564 > <TT
565 CLASS="PROMPT"
566 >bash$ </TT
567 ><TT
568 CLASS="USERINPUT"
569 ><B
570 >rpm -qa</B
571 ></TT
572 >
573 <TT
574 CLASS="COMPUTEROUTPUT"
575 >redhat-logos-1.1.3-1
576 glibc-2.2.4-13
577 cracklib-2.7-12
578 dosfstools-2.7-1
579 gdbm-1.8.0-10
580 ksymoops-2.4.1-1
581 mktemp-1.5-11
582 perl-5.6.0-17
583 reiserfs-utils-3.x.0j-2
584 ...</TT
585 >
586
587
588 <TT
589 CLASS="PROMPT"
590 >bash$ </TT
591 ><TT
592 CLASS="USERINPUT"
593 ><B
594 >rpm -qa docbook-utils</B
595 ></TT
596 >
597 <TT
598 CLASS="COMPUTEROUTPUT"
599 >docbook-utils-0.6.9-2</TT
600 >
601
602
603 <TT
604 CLASS="PROMPT"
605 >bash$ </TT
606 ><TT
607 CLASS="USERINPUT"
608 ><B
609 >rpm -qa docbook | grep docbook</B
610 ></TT
611 >
612 <TT
613 CLASS="COMPUTEROUTPUT"
614 >docbook-dtd31-sgml-1.0-10
615 docbook-style-dsssl-1.64-3
616 docbook-dtd30-sgml-1.0-10
617 docbook-dtd40-sgml-1.0-11
618 docbook-utils-pdf-0.6.9-2
619 docbook-dtd41-sgml-1.0-10
620 docbook-utils-0.6.9-2</TT
621 >
622 </PRE
623 ></TD
624 ></TR
625 ></TABLE
626 >
627 </P
628 ></TD
629 ></TR
630 ></TABLE
631 ></DIV
632 ></DD
633 ><DT
634 ><A
635 NAME="CPIOREF"
636 ></A
637 ><B
638 CLASS="COMMAND"
639 >cpio</B
640 ></DT
641 ><DD
642 ><P
643 >This specialized archiving copy command
644 (<B
645 CLASS="COMMAND"
646 >c</B
647 >o<B
648 CLASS="COMMAND"
649 >p</B
650 >y
651 <B
652 CLASS="COMMAND"
653 >i</B
654 >nput and <B
655 CLASS="COMMAND"
656 >o</B
657 >utput)
658 is rarely seen any more, having been supplanted by
659 <B
660 CLASS="COMMAND"
661 >tar</B
662 >/<B
663 CLASS="COMMAND"
664 >gzip</B
665 >. It still
666 has its uses, such as moving a directory tree. With an
667 appropriate block size (for copying) specified, it
668 can be appreciably faster than <B
669 CLASS="COMMAND"
670 >tar</B
671 >.</P
672 ><DIV
673 CLASS="EXAMPLE"
674 ><HR><A
675 NAME="EX48"
676 ></A
677 ><P
678 ><B
679 >Example 16-30. Using <I
680 CLASS="FIRSTTERM"
681 >cpio</I
682 > to move a directory tree</B
683 ></P
684 ><TABLE
685 BORDER="0"
686 BGCOLOR="#E0E0E0"
687 WIDTH="90%"
688 ><TR
689 ><TD
690 ><PRE
691 CLASS="PROGRAMLISTING"
692 > 1&nbsp;#!/bin/bash
693 2&nbsp;
694 3&nbsp;# Copying a directory tree using cpio.
695 4&nbsp;
696 5&nbsp;# Advantages of using 'cpio':
697 6&nbsp;# Speed of copying. It's faster than 'tar' with pipes.
698 7&nbsp;# Well suited for copying special files (named pipes, etc.)
699 8&nbsp;#+ that 'cp' may choke on.
700 9&nbsp;
701 10&nbsp;ARGS=2
702 11&nbsp;E_BADARGS=65
703 12&nbsp;
704 13&nbsp;if [ $# -ne "$ARGS" ]
705 14&nbsp;then
706 15&nbsp; echo "Usage: `basename $0` source destination"
707 16&nbsp; exit $E_BADARGS
708 17&nbsp;fi
709 18&nbsp;
710 19&nbsp;source="$1"
711 20&nbsp;destination="$2"
712 21&nbsp;
713 22&nbsp;###################################################################
714 23&nbsp;find "$source" -depth | cpio -admvp "$destination"
715 24&nbsp;# ^^^^^ ^^^^^
716 25&nbsp;# Read the 'find' and 'cpio' info pages to decipher these options.
717 26&nbsp;# The above works only relative to $PWD (current directory) . . .
718 27&nbsp;#+ full pathnames are specified.
719 28&nbsp;###################################################################
720 29&nbsp;
721 30&nbsp;
722 31&nbsp;# Exercise:
723 32&nbsp;# --------
724 33&nbsp;
725 34&nbsp;# Add code to check the exit status ($?) of the 'find | cpio' pipe
726 35&nbsp;#+ and output appropriate error messages if anything went wrong.
727 36&nbsp;
728 37&nbsp;exit $?</PRE
729 ></TD
730 ></TR
731 ></TABLE
732 ><HR></DIV
733 ></DD
734 ><DT
735 ><A
736 NAME="RPM2CPIOREF"
737 ></A
738 ><B
739 CLASS="COMMAND"
740 >rpm2cpio</B
741 ></DT
742 ><DD
743 ><P
744 >This command extracts a
745 <B
746 CLASS="COMMAND"
747 >cpio</B
748 > archive from an <A
749 HREF="filearchiv.html#RPMREF"
750 >rpm</A
751 > one.</P
752 ><DIV
753 CLASS="EXAMPLE"
754 ><HR><A
755 NAME="DERPM"
756 ></A
757 ><P
758 ><B
759 >Example 16-31. Unpacking an <I
760 CLASS="FIRSTTERM"
761 >rpm</I
762 > archive</B
763 ></P
764 ><TABLE
765 BORDER="0"
766 BGCOLOR="#E0E0E0"
767 WIDTH="90%"
768 ><TR
769 ><TD
770 ><PRE
771 CLASS="PROGRAMLISTING"
772 > 1&nbsp;#!/bin/bash
773 2&nbsp;# de-rpm.sh: Unpack an 'rpm' archive
774 3&nbsp;
775 4&nbsp;: ${1?"Usage: `basename $0` target-file"}
776 5&nbsp;# Must specify 'rpm' archive name as an argument.
777 6&nbsp;
778 7&nbsp;
779 8&nbsp;TEMPFILE=$$.cpio # Tempfile with "unique" name.
780 9&nbsp; # $$ is process ID of script.
781 10&nbsp;
782 11&nbsp;rpm2cpio &#60; $1 &#62; $TEMPFILE # Converts rpm archive into
783 12&nbsp; #+ cpio archive.
784 13&nbsp;cpio --make-directories -F $TEMPFILE -i # Unpacks cpio archive.
785 14&nbsp;rm -f $TEMPFILE # Deletes cpio archive.
786 15&nbsp;
787 16&nbsp;exit 0
788 17&nbsp;
789 18&nbsp;# Exercise:
790 19&nbsp;# Add check for whether 1) "target-file" exists and
791 20&nbsp;#+ 2) it is an rpm archive.
792 21&nbsp;# Hint: Parse output of 'file' command.</PRE
793 ></TD
794 ></TR
795 ></TABLE
796 ><HR></DIV
797 ></DD
798 ><DT
799 ><A
800 NAME="PAXREF"
801 ></A
802 ><B
803 CLASS="COMMAND"
804 >pax</B
805 ></DT
806 ><DD
807 ><P
808 >The <I
809 CLASS="FIRSTTERM"
810 >pax</I
811 >
812 <B
813 CLASS="COMMAND"
814 >p</B
815 >ortable <B
816 CLASS="COMMAND"
817 >a</B
818 >rchive
819 e<B
820 CLASS="COMMAND"
821 >x</B
822 >change toolkit facilitates periodic
823 file backups and is designed to be cross-compatible
824 between various flavors of UNIX. It was designed
825 to replace <A
826 HREF="filearchiv.html#TARREF"
827 >tar</A
828 > and <A
829 HREF="filearchiv.html#CPIOREF"
830 >cpio</A
831 >.</P
832 ><P
833 > <TABLE
834 BORDER="0"
835 BGCOLOR="#E0E0E0"
836 WIDTH="90%"
837 ><TR
838 ><TD
839 ><PRE
840 CLASS="PROGRAMLISTING"
841 > 1&nbsp;pax -wf daily_backup.pax ~/linux-server/files
842 2&nbsp;# Creates a tar archive of all files in the target directory.
843 3&nbsp;# Note that the options to pax must be in the correct order --
844 4&nbsp;#+ pax -fw has an entirely different effect.
845 5&nbsp;
846 6&nbsp;pax -f daily_backup.pax
847 7&nbsp;# Lists the files in the archive.
848 8&nbsp;
849 9&nbsp;pax -rf daily_backup.pax ~/bsd-server/files
850 10&nbsp;# Restores the backed-up files from the Linux machine
851 11&nbsp;#+ onto a BSD one.</PRE
852 ></TD
853 ></TR
854 ></TABLE
855 >
856 </P
857 ><P
858 >Note that <I
859 CLASS="FIRSTTERM"
860 >pax</I
861 > handles many of
862 the standard archiving and compression commands.</P
863 ></DD
864 ></DL
865 ></DIV
866 ><DIV
867 CLASS="VARIABLELIST"
868 ><P
869 ><B
870 ><A
871 NAME="FACOMPRESSION1"
872 ></A
873 >Compression</B
874 ></P
875 ><DL
876 ><DT
877 ><A
878 NAME="GZIPREF"
879 ></A
880 ><B
881 CLASS="COMMAND"
882 >gzip</B
883 ></DT
884 ><DD
885 ><P
886 >The standard GNU/UNIX compression utility, replacing
887 the inferior and proprietary
888 <B
889 CLASS="COMMAND"
890 >compress</B
891 >. The corresponding decompression
892 command is <B
893 CLASS="COMMAND"
894 >gunzip</B
895 >, which is the equivalent of
896 <B
897 CLASS="COMMAND"
898 >gzip -d</B
899 >.</P
900 ><DIV
901 CLASS="NOTE"
902 ><TABLE
903 CLASS="NOTE"
904 WIDTH="90%"
905 BORDER="0"
906 ><TR
907 ><TD
908 WIDTH="25"
909 ALIGN="CENTER"
910 VALIGN="TOP"
911 ><IMG
912 SRC="common/note.png"
913 HSPACE="5"
914 ALT="Note"></TD
915 ><TD
916 ALIGN="LEFT"
917 VALIGN="TOP"
918 ><P
919 >The <TT
920 CLASS="OPTION"
921 >-c</TT
922 > option sends the output of
923 <B
924 CLASS="COMMAND"
925 >gzip</B
926 > to <TT
927 CLASS="FILENAME"
928 >stdout</TT
929 >. This
930 is useful when <A
931 HREF="special-chars.html#PIPEREF"
932 >piping</A
933 > to other
934 commands.</P
935 ></TD
936 ></TR
937 ></TABLE
938 ></DIV
939 ><P
940 ><A
941 NAME="ZCATREF"
942 ></A
943 ></P
944 ><P
945 >The <B
946 CLASS="COMMAND"
947 >zcat</B
948 > filter decompresses a
949 <I
950 CLASS="FIRSTTERM"
951 >gzipped</I
952 > file to
953 <TT
954 CLASS="FILENAME"
955 >stdout</TT
956 >, as possible input to a pipe or
957 redirection. This is, in effect, a <B
958 CLASS="COMMAND"
959 >cat</B
960 >
961 command that works on compressed files (including files
962 processed with the older <A
963 HREF="filearchiv.html#COMPRESSREF"
964 >compress</A
965 >
966 utility). The <B
967 CLASS="COMMAND"
968 >zcat</B
969 > command is equivalent to
970 <B
971 CLASS="COMMAND"
972 >gzip -dc</B
973 >.</P
974 ><DIV
975 CLASS="CAUTION"
976 ><TABLE
977 CLASS="CAUTION"
978 WIDTH="90%"
979 BORDER="0"
980 ><TR
981 ><TD
982 WIDTH="25"
983 ALIGN="CENTER"
984 VALIGN="TOP"
985 ><IMG
986 SRC="common/caution.png"
987 HSPACE="5"
988 ALT="Caution"></TD
989 ><TD
990 ALIGN="LEFT"
991 VALIGN="TOP"
992 ><P
993 >On some commercial UNIX systems, <B
994 CLASS="COMMAND"
995 >zcat</B
996 >
997 is a synonym for <B
998 CLASS="COMMAND"
999 >uncompress -c</B
1000 >,
1001 and will not work on <I
1002 CLASS="FIRSTTERM"
1003 >gzipped</I
1004 >
1005 files.</P
1006 ></TD
1007 ></TR
1008 ></TABLE
1009 ></DIV
1010 ><P
1011 >See also <A
1012 HREF="comparison-ops.html#EX14"
1013 >Example 7-7</A
1014 >.</P
1015 ></DD
1016 ><DT
1017 ><A
1018 NAME="BZIPREF"
1019 ></A
1020 ><B
1021 CLASS="COMMAND"
1022 >bzip2</B
1023 ></DT
1024 ><DD
1025 ><P
1026 >An alternate compression utility, usually more efficient
1027 (but slower) than <B
1028 CLASS="COMMAND"
1029 >gzip</B
1030 >, especially on
1031 large files. The corresponding decompression command is
1032 <B
1033 CLASS="COMMAND"
1034 >bunzip2</B
1035 >.</P
1036 ><P
1037 >Similar to the <B
1038 CLASS="COMMAND"
1039 >zcat</B
1040 > command,
1041 <B
1042 CLASS="COMMAND"
1043 >bzcat</B
1044 > decompresses a
1045 <I
1046 CLASS="FIRSTTERM"
1047 >bzipped2-ed</I
1048 > file to
1049 <TT
1050 CLASS="FILENAME"
1051 >stdout</TT
1052 >.</P
1053 ><DIV
1054 CLASS="NOTE"
1055 ><TABLE
1056 CLASS="NOTE"
1057 WIDTH="90%"
1058 BORDER="0"
1059 ><TR
1060 ><TD
1061 WIDTH="25"
1062 ALIGN="CENTER"
1063 VALIGN="TOP"
1064 ><IMG
1065 SRC="common/note.png"
1066 HSPACE="5"
1067 ALT="Note"></TD
1068 ><TD
1069 ALIGN="LEFT"
1070 VALIGN="TOP"
1071 ><P
1072 >Newer versions of <A
1073 HREF="filearchiv.html#TARREF"
1074 >tar</A
1075 > have been patched with
1076 <B
1077 CLASS="COMMAND"
1078 >bzip2</B
1079 > support.</P
1080 ></TD
1081 ></TR
1082 ></TABLE
1083 ></DIV
1084 ></DD
1085 ><DT
1086 ><A
1087 NAME="COMPRESSREF"
1088 ></A
1089 ><B
1090 CLASS="COMMAND"
1091 >compress</B
1092 >, <A
1093 NAME="UNCOMPRESSREF"
1094 ></A
1095 ><B
1096 CLASS="COMMAND"
1097 >uncompress</B
1098 ></DT
1099 ><DD
1100 ><P
1101 >This is an older, proprietary compression
1102 utility found in commercial UNIX distributions. The
1103 more efficient <B
1104 CLASS="COMMAND"
1105 >gzip</B
1106 > has largely
1107 replaced it. Linux distributions generally include a
1108 <B
1109 CLASS="COMMAND"
1110 >compress</B
1111 > workalike for compatibility,
1112 although <B
1113 CLASS="COMMAND"
1114 >gunzip</B
1115 > can unarchive files
1116 treated with <B
1117 CLASS="COMMAND"
1118 >compress</B
1119 >.</P
1120 ><DIV
1121 CLASS="TIP"
1122 ><TABLE
1123 CLASS="TIP"
1124 WIDTH="90%"
1125 BORDER="0"
1126 ><TR
1127 ><TD
1128 WIDTH="25"
1129 ALIGN="CENTER"
1130 VALIGN="TOP"
1131 ><IMG
1132 SRC="common/tip.png"
1133 HSPACE="5"
1134 ALT="Tip"></TD
1135 ><TD
1136 ALIGN="LEFT"
1137 VALIGN="TOP"
1138 ><P
1139 >The <B
1140 CLASS="COMMAND"
1141 >znew</B
1142 > command transforms
1143 <I
1144 CLASS="FIRSTTERM"
1145 >compressed</I
1146 > files into
1147 <I
1148 CLASS="FIRSTTERM"
1149 >gzipped</I
1150 > ones.</P
1151 ></TD
1152 ></TR
1153 ></TABLE
1154 ></DIV
1155 ></DD
1156 ><DT
1157 ><A
1158 NAME="SQREF"
1159 ></A
1160 ><B
1161 CLASS="COMMAND"
1162 >sq</B
1163 ></DT
1164 ><DD
1165 ><P
1166 >Yet another compression (<B
1167 CLASS="COMMAND"
1168 >sq</B
1169 >ueeze)
1170 utility, a filter that works only on sorted
1171 <A
1172 HREF="special-chars.html#ASCIIDEF"
1173 >ASCII</A
1174 > word lists. It
1175 uses the standard invocation syntax for a filter,
1176 <B
1177 CLASS="COMMAND"
1178 >sq &#60; input-file &#62; output-file</B
1179 >.
1180 Fast, but not nearly as efficient as <A
1181 HREF="filearchiv.html#GZIPREF"
1182 >gzip</A
1183 >. The corresponding
1184 uncompression filter is <B
1185 CLASS="COMMAND"
1186 >unsq</B
1187 >, invoked
1188 like <B
1189 CLASS="COMMAND"
1190 >sq</B
1191 >.</P
1192 ><DIV
1193 CLASS="TIP"
1194 ><TABLE
1195 CLASS="TIP"
1196 WIDTH="90%"
1197 BORDER="0"
1198 ><TR
1199 ><TD
1200 WIDTH="25"
1201 ALIGN="CENTER"
1202 VALIGN="TOP"
1203 ><IMG
1204 SRC="common/tip.png"
1205 HSPACE="5"
1206 ALT="Tip"></TD
1207 ><TD
1208 ALIGN="LEFT"
1209 VALIGN="TOP"
1210 ><P
1211 >The output of <B
1212 CLASS="COMMAND"
1213 >sq</B
1214 > may be
1215 piped to <B
1216 CLASS="COMMAND"
1217 >gzip</B
1218 > for further
1219 compression.</P
1220 ></TD
1221 ></TR
1222 ></TABLE
1223 ></DIV
1224 ></DD
1225 ><DT
1226 ><A
1227 NAME="ZIPREF"
1228 ></A
1229 ><B
1230 CLASS="COMMAND"
1231 >zip</B
1232 >, <B
1233 CLASS="COMMAND"
1234 >unzip</B
1235 ></DT
1236 ><DD
1237 ><P
1238 >Cross-platform file archiving and compression utility
1239 compatible with DOS <I
1240 CLASS="FIRSTTERM"
1241 >pkzip.exe</I
1242 >.
1243 <SPAN
1244 CLASS="QUOTE"
1245 >"Zipped"</SPAN
1246 > archives seem to be a more
1247 common medium of file exchange on the Internet than
1248 <SPAN
1249 CLASS="QUOTE"
1250 >"tarballs."</SPAN
1251 ></P
1252 ></DD
1253 ><DT
1254 ><A
1255 NAME="UNARCREF"
1256 ></A
1257 ><B
1258 CLASS="COMMAND"
1259 >unarc</B
1260 >, <B
1261 CLASS="COMMAND"
1262 >unarj</B
1263 >, <B
1264 CLASS="COMMAND"
1265 >unrar</B
1266 ></DT
1267 ><DD
1268 ><P
1269 >These Linux utilities permit unpacking archives
1270 compressed with the DOS <I
1271 CLASS="FIRSTTERM"
1272 >arc.exe</I
1273 >,
1274 <I
1275 CLASS="FIRSTTERM"
1276 >arj.exe</I
1277 >, and
1278 <I
1279 CLASS="FIRSTTERM"
1280 >rar.exe</I
1281 > programs.</P
1282 ></DD
1283 ><DT
1284 ><A
1285 NAME="LZMAREF"
1286 ></A
1287 ><B
1288 CLASS="COMMAND"
1289 >lzma</B
1290 >, <B
1291 CLASS="COMMAND"
1292 >unlzma</B
1293 >, <B
1294 CLASS="COMMAND"
1295 >lzcat</B
1296 ></DT
1297 ><DD
1298 ><P
1299 >Highly efficient Lempel-Ziv-Markov compression.
1300 The syntax of <I
1301 CLASS="FIRSTTERM"
1302 >lzma</I
1303 > is similar to
1304 that of <I
1305 CLASS="FIRSTTERM"
1306 >gzip</I
1307 >. The <A
1308 HREF="http://www.7-zip.org/sdk.html"
1309 TARGET="_top"
1310 >7-zip Website</A
1311 >
1312 has more information.</P
1313 ></DD
1314 ><DT
1315 ><A
1316 NAME="XZREF"
1317 ></A
1318 ><B
1319 CLASS="COMMAND"
1320 >xz</B
1321 >, <B
1322 CLASS="COMMAND"
1323 >unxz</B
1324 >, <B
1325 CLASS="COMMAND"
1326 >xzcat</B
1327 ></DT
1328 ><DD
1329 ><P
1330 >A new high-efficiency compression tool, backward compatible
1331 with <I
1332 CLASS="FIRSTTERM"
1333 >lzma</I
1334 >, and with an invocation
1335 syntax similar to <I
1336 CLASS="FIRSTTERM"
1337 >gzip</I
1338 >. For
1339 more information, see the <A
1340 HREF="http://en.wikipedia.org/wiki/Xz"
1341 TARGET="_top"
1342 >Wikipedia
1343 entry</A
1344 >.</P
1345 ></DD
1346 ></DL
1347 ></DIV
1348 ><DIV
1349 CLASS="VARIABLELIST"
1350 ><P
1351 ><B
1352 ><A
1353 NAME="FAINFORMATION1"
1354 ></A
1355 >File Information</B
1356 ></P
1357 ><DL
1358 ><DT
1359 ><A
1360 NAME="FILEREF"
1361 ></A
1362 ><B
1363 CLASS="COMMAND"
1364 >file</B
1365 ></DT
1366 ><DD
1367 ><P
1368 >A utility for identifying file types. The command
1369 <TT
1370 CLASS="USERINPUT"
1371 ><B
1372 >file file-name</B
1373 ></TT
1374 > will return a
1375 file specification for <TT
1376 CLASS="FILENAME"
1377 >file-name</TT
1378 >,
1379 such as <TT
1380 CLASS="COMPUTEROUTPUT"
1381 >ascii text</TT
1382 > or
1383 <TT
1384 CLASS="COMPUTEROUTPUT"
1385 >data</TT
1386 >. It references
1387 the <A
1388 HREF="sha-bang.html#MAGNUMREF"
1389 >magic numbers</A
1390 >
1391 found in <TT
1392 CLASS="FILENAME"
1393 >/usr/share/magic</TT
1394 >,
1395 <TT
1396 CLASS="FILENAME"
1397 >/etc/magic</TT
1398 >, or
1399 <TT
1400 CLASS="FILENAME"
1401 >/usr/lib/magic</TT
1402 >, depending on the
1403 Linux/UNIX distribution.</P
1404 ><P
1405 >The <TT
1406 CLASS="OPTION"
1407 >-f</TT
1408 > option causes
1409 <B
1410 CLASS="COMMAND"
1411 >file</B
1412 > to run in <A
1413 HREF="timedate.html#BATCHPROCREF"
1414 >batch</A
1415 > mode, to read from
1416 a designated file a list of filenames to analyze. The
1417 <TT
1418 CLASS="OPTION"
1419 >-z</TT
1420 > option, when used on a compressed
1421 target file, forces an attempt to analyze the uncompressed
1422 file type.</P
1423 ><P
1424 > <TABLE
1425 BORDER="0"
1426 BGCOLOR="#E0E0E0"
1427 WIDTH="90%"
1428 ><TR
1429 ><TD
1430 ><PRE
1431 CLASS="SCREEN"
1432 > <TT
1433 CLASS="PROMPT"
1434 >bash$ </TT
1435 ><TT
1436 CLASS="USERINPUT"
1437 ><B
1438 >file test.tar.gz</B
1439 ></TT
1440 >
1441 <TT
1442 CLASS="COMPUTEROUTPUT"
1443 >test.tar.gz: gzip compressed data, deflated,
1444 last modified: Sun Sep 16 13:34:51 2001, os: Unix</TT
1445 >
1446
1447 <TT
1448 CLASS="PROMPT"
1449 >bash </TT
1450 ><TT
1451 CLASS="USERINPUT"
1452 ><B
1453 >file -z test.tar.gz</B
1454 ></TT
1455 >
1456 <TT
1457 CLASS="COMPUTEROUTPUT"
1458 >test.tar.gz: GNU tar archive (gzip compressed data, deflated,
1459 last modified: Sun Sep 16 13:34:51 2001, os: Unix)</TT
1460 >
1461 </PRE
1462 ></TD
1463 ></TR
1464 ></TABLE
1465 >
1466 </P
1467 ><P
1468 > <TABLE
1469 BORDER="0"
1470 BGCOLOR="#E0E0E0"
1471 WIDTH="90%"
1472 ><TR
1473 ><TD
1474 ><PRE
1475 CLASS="PROGRAMLISTING"
1476 > 1&nbsp;# Find sh and Bash scripts in a given directory:
1477 2&nbsp;
1478 3&nbsp;DIRECTORY=/usr/local/bin
1479 4&nbsp;KEYWORD=Bourne
1480 5&nbsp;# Bourne and Bourne-Again shell scripts
1481 6&nbsp;
1482 7&nbsp;file $DIRECTORY/* | fgrep $KEYWORD
1483 8&nbsp;
1484 9&nbsp;# Output:
1485 10&nbsp;
1486 11&nbsp;# /usr/local/bin/burn-cd: Bourne-Again shell script text executable
1487 12&nbsp;# /usr/local/bin/burnit: Bourne-Again shell script text executable
1488 13&nbsp;# /usr/local/bin/cassette.sh: Bourne shell script text executable
1489 14&nbsp;# /usr/local/bin/copy-cd: Bourne-Again shell script text executable
1490 15&nbsp;# . . .</PRE
1491 ></TD
1492 ></TR
1493 ></TABLE
1494 >
1495 </P
1496 ><DIV
1497 CLASS="EXAMPLE"
1498 ><HR><A
1499 NAME="STRIPC"
1500 ></A
1501 ><P
1502 ><B
1503 >Example 16-32. Stripping comments from C program files</B
1504 ></P
1505 ><TABLE
1506 BORDER="0"
1507 BGCOLOR="#E0E0E0"
1508 WIDTH="90%"
1509 ><TR
1510 ><TD
1511 ><PRE
1512 CLASS="PROGRAMLISTING"
1513 > 1&nbsp;#!/bin/bash
1514 2&nbsp;# strip-comment.sh: Strips out the comments (/* COMMENT */) in a C program.
1515 3&nbsp;
1516 4&nbsp;E_NOARGS=0
1517 5&nbsp;E_ARGERROR=66
1518 6&nbsp;E_WRONG_FILE_TYPE=67
1519 7&nbsp;
1520 8&nbsp;if [ $# -eq "$E_NOARGS" ]
1521 9&nbsp;then
1522 10&nbsp; echo "Usage: `basename $0` C-program-file" &#62;&#38;2 # Error message to stderr.
1523 11&nbsp; exit $E_ARGERROR
1524 12&nbsp;fi
1525 13&nbsp;
1526 14&nbsp;# Test for correct file type.
1527 15&nbsp;type=`file $1 | awk '{ print $2, $3, $4, $5 }'`
1528 16&nbsp;# "file $1" echoes file type . . .
1529 17&nbsp;# Then awk removes the first field, the filename . . .
1530 18&nbsp;# Then the result is fed into the variable "type."
1531 19&nbsp;correct_type="ASCII C program text"
1532 20&nbsp;
1533 21&nbsp;if [ "$type" != "$correct_type" ]
1534 22&nbsp;then
1535 23&nbsp; echo
1536 24&nbsp; echo "This script works on C program files only."
1537 25&nbsp; echo
1538 26&nbsp; exit $E_WRONG_FILE_TYPE
1539 27&nbsp;fi
1540 28&nbsp;
1541 29&nbsp;
1542 30&nbsp;# Rather cryptic sed script:
1543 31&nbsp;#--------
1544 32&nbsp;sed '
1545 33&nbsp;/^\/\*/d
1546 34&nbsp;/.*\*\//d
1547 35&nbsp;' $1
1548 36&nbsp;#--------
1549 37&nbsp;# Easy to understand if you take several hours to learn sed fundamentals.
1550 38&nbsp;
1551 39&nbsp;
1552 40&nbsp;# Need to add one more line to the sed script to deal with
1553 41&nbsp;#+ case where line of code has a comment following it on same line.
1554 42&nbsp;# This is left as a non-trivial exercise.
1555 43&nbsp;
1556 44&nbsp;# Also, the above code deletes non-comment lines with a "*/" . . .
1557 45&nbsp;#+ not a desirable result.
1558 46&nbsp;
1559 47&nbsp;exit 0
1560 48&nbsp;
1561 49&nbsp;
1562 50&nbsp;# ----------------------------------------------------------------
1563 51&nbsp;# Code below this line will not execute because of 'exit 0' above.
1564 52&nbsp;
1565 53&nbsp;# Stephane Chazelas suggests the following alternative:
1566 54&nbsp;
1567 55&nbsp;usage() {
1568 56&nbsp; echo "Usage: `basename $0` C-program-file" &#62;&#38;2
1569 57&nbsp; exit 1
1570 58&nbsp;}
1571 59&nbsp;
1572 60&nbsp;WEIRD=`echo -n -e '\377'` # or WEIRD=$'\377'
1573 61&nbsp;[[ $# -eq 1 ]] || usage
1574 62&nbsp;case `file "$1"` in
1575 63&nbsp; *"C program text"*) sed -e "s%/\*%${WEIRD}%g;s%\*/%${WEIRD}%g" "$1" \
1576 64&nbsp; | tr '\377\n' '\n\377' \
1577 65&nbsp; | sed -ne 'p;n' \
1578 66&nbsp; | tr -d '\n' | tr '\377' '\n';;
1579 67&nbsp; *) usage;;
1580 68&nbsp;esac
1581 69&nbsp;
1582 70&nbsp;# This is still fooled by things like:
1583 71&nbsp;# printf("/*");
1584 72&nbsp;# or
1585 73&nbsp;# /* /* buggy embedded comment */
1586 74&nbsp;#
1587 75&nbsp;# To handle all special cases (comments in strings, comments in string
1588 76&nbsp;#+ where there is a \", \\" ...),
1589 77&nbsp;#+ the only way is to write a C parser (using lex or yacc perhaps?).
1590 78&nbsp;
1591 79&nbsp;exit 0</PRE
1592 ></TD
1593 ></TR
1594 ></TABLE
1595 ><HR></DIV
1596 ></DD
1597 ><DT
1598 ><A
1599 NAME="WHICHREF"
1600 ></A
1601 ><B
1602 CLASS="COMMAND"
1603 >which</B
1604 ></DT
1605 ><DD
1606 ><P
1607 ><B
1608 CLASS="COMMAND"
1609 >which command</B
1610 > gives the full path
1611 to <SPAN
1612 CLASS="QUOTE"
1613 >"command."</SPAN
1614 > This is useful for finding
1615 out whether a particular command or utility is installed
1616 on the system.</P
1617 ><P
1618 ><TT
1619 CLASS="USERINPUT"
1620 ><B
1621 >$bash which rm</B
1622 ></TT
1623 >
1624 <TABLE
1625 BORDER="0"
1626 BGCOLOR="#E0E0E0"
1627 WIDTH="90%"
1628 ><TR
1629 ><TD
1630 ><PRE
1631 CLASS="SCREEN"
1632 > <TT
1633 CLASS="COMPUTEROUTPUT"
1634 >/usr/bin/rm</TT
1635 ></PRE
1636 ></TD
1637 ></TR
1638 ></TABLE
1639 >
1640 </P
1641 ><P
1642 >For an interesting use of this command, see <A
1643 HREF="colorizing.html#HORSERACE"
1644 >Example 36-16</A
1645 >.</P
1646 ></DD
1647 ><DT
1648 ><A
1649 NAME="WHEREISREF"
1650 ></A
1651 ><B
1652 CLASS="COMMAND"
1653 >whereis</B
1654 ></DT
1655 ><DD
1656 ><P
1657 >Similar to <B
1658 CLASS="COMMAND"
1659 >which</B
1660 >, above,
1661 <B
1662 CLASS="COMMAND"
1663 >whereis command</B
1664 > gives the
1665 full path to <SPAN
1666 CLASS="QUOTE"
1667 >"command,"</SPAN
1668 > but also to its
1669 <A
1670 HREF="external.html#MANREF"
1671 >manpage</A
1672 >.</P
1673 ><P
1674 ><TT
1675 CLASS="USERINPUT"
1676 ><B
1677 >$bash whereis rm</B
1678 ></TT
1679 >
1680 <TABLE
1681 BORDER="0"
1682 BGCOLOR="#E0E0E0"
1683 WIDTH="90%"
1684 ><TR
1685 ><TD
1686 ><PRE
1687 CLASS="SCREEN"
1688 > <TT
1689 CLASS="COMPUTEROUTPUT"
1690 >rm: /bin/rm /usr/share/man/man1/rm.1.bz2</TT
1691 ></PRE
1692 ></TD
1693 ></TR
1694 ></TABLE
1695 >
1696 </P
1697 ></DD
1698 ><DT
1699 ><A
1700 NAME="WHATISREF"
1701 ></A
1702 ><B
1703 CLASS="COMMAND"
1704 >whatis</B
1705 ></DT
1706 ><DD
1707 ><P
1708 ><B
1709 CLASS="COMMAND"
1710 >whatis command</B
1711 > looks up
1712 <SPAN
1713 CLASS="QUOTE"
1714 >"command"</SPAN
1715 > in the
1716 <TT
1717 CLASS="REPLACEABLE"
1718 ><I
1719 >whatis</I
1720 ></TT
1721 > database. This is useful
1722 for identifying system commands and important configuration
1723 files. Consider it a simplified <B
1724 CLASS="COMMAND"
1725 >man</B
1726 >
1727 command.</P
1728 ><P
1729 ><TT
1730 CLASS="USERINPUT"
1731 ><B
1732 >$bash whatis whatis</B
1733 ></TT
1734 >
1735 <TABLE
1736 BORDER="0"
1737 BGCOLOR="#E0E0E0"
1738 WIDTH="90%"
1739 ><TR
1740 ><TD
1741 ><PRE
1742 CLASS="SCREEN"
1743 > <TT
1744 CLASS="COMPUTEROUTPUT"
1745 >whatis (1) - search the whatis database for complete words</TT
1746 ></PRE
1747 ></TD
1748 ></TR
1749 ></TABLE
1750 >
1751 </P
1752 ><DIV
1753 CLASS="EXAMPLE"
1754 ><HR><A
1755 NAME="WHAT"
1756 ></A
1757 ><P
1758 ><B
1759 >Example 16-33. Exploring <TT
1760 CLASS="FILENAME"
1761 >/usr/X11R6/bin</TT
1762 ></B
1763 ></P
1764 ><TABLE
1765 BORDER="0"
1766 BGCOLOR="#E0E0E0"
1767 WIDTH="90%"
1768 ><TR
1769 ><TD
1770 ><PRE
1771 CLASS="PROGRAMLISTING"
1772 > 1&nbsp;#!/bin/bash
1773 2&nbsp;
1774 3&nbsp;# What are all those mysterious binaries in /usr/X11R6/bin?
1775 4&nbsp;
1776 5&nbsp;DIRECTORY="/usr/X11R6/bin"
1777 6&nbsp;# Try also "/bin", "/usr/bin", "/usr/local/bin", etc.
1778 7&nbsp;
1779 8&nbsp;for file in $DIRECTORY/*
1780 9&nbsp;do
1781 10&nbsp; whatis `basename $file` # Echoes info about the binary.
1782 11&nbsp;done
1783 12&nbsp;
1784 13&nbsp;exit 0
1785 14&nbsp;
1786 15&nbsp;# Note: For this to work, you must create a "whatis" database
1787 16&nbsp;#+ with /usr/sbin/makewhatis.
1788 17&nbsp;# You may wish to redirect output of this script, like so:
1789 18&nbsp;# ./what.sh &#62;&#62;whatis.db
1790 19&nbsp;# or view it a page at a time on stdout,
1791 20&nbsp;# ./what.sh | less</PRE
1792 ></TD
1793 ></TR
1794 ></TABLE
1795 ><HR></DIV
1796 ><P
1797 >See also <A
1798 HREF="loops.html#FILEINFO"
1799 >Example 11-3</A
1800 >.</P
1801 ></DD
1802 ><DT
1803 ><A
1804 NAME="VDIRREF"
1805 ></A
1806 ><B
1807 CLASS="COMMAND"
1808 >vdir</B
1809 ></DT
1810 ><DD
1811 ><P
1812 >Show a detailed directory listing. The effect is similar to
1813 <A
1814 HREF="external.html#LSREF"
1815 >ls -lb</A
1816 >.</P
1817 ><P
1818 >This is one of the GNU
1819 <I
1820 CLASS="FIRSTTERM"
1821 >fileutils</I
1822 >.</P
1823 ><P
1824 > <TABLE
1825 BORDER="0"
1826 BGCOLOR="#E0E0E0"
1827 WIDTH="90%"
1828 ><TR
1829 ><TD
1830 ><PRE
1831 CLASS="SCREEN"
1832 > <TT
1833 CLASS="PROMPT"
1834 >bash$ </TT
1835 ><TT
1836 CLASS="USERINPUT"
1837 ><B
1838 >vdir</B
1839 ></TT
1840 >
1841 <TT
1842 CLASS="COMPUTEROUTPUT"
1843 >total 10
1844 -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo
1845 -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak
1846 -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</TT
1847 >
1848
1849 <TT
1850 CLASS="PROMPT"
1851 >bash </TT
1852 ><TT
1853 CLASS="USERINPUT"
1854 ><B
1855 >ls -l</B
1856 ></TT
1857 >
1858 <TT
1859 CLASS="COMPUTEROUTPUT"
1860 >total 10
1861 -rw-r--r-- 1 bozo bozo 4034 Jul 18 22:04 data1.xrolo
1862 -rw-r--r-- 1 bozo bozo 4602 May 25 13:58 data1.xrolo.bak
1863 -rw-r--r-- 1 bozo bozo 877 Dec 17 2000 employment.xrolo</TT
1864 >
1865 </PRE
1866 ></TD
1867 ></TR
1868 ></TABLE
1869 >
1870 </P
1871 ></DD
1872 ><DT
1873 ><A
1874 NAME="LOCATEREF"
1875 ></A
1876 ><B
1877 CLASS="COMMAND"
1878 >locate</B
1879 >, <A
1880 NAME="SLOCATEREF"
1881 ></A
1882 ><B
1883 CLASS="COMMAND"
1884 >slocate</B
1885 ></DT
1886 ><DD
1887 ><P
1888 >The <B
1889 CLASS="COMMAND"
1890 >locate</B
1891 > command searches for
1892 files using a database stored for just that purpose. The
1893 <B
1894 CLASS="COMMAND"
1895 >slocate</B
1896 > command is the secure version of
1897 <B
1898 CLASS="COMMAND"
1899 >locate</B
1900 > (which may be aliased to
1901 <B
1902 CLASS="COMMAND"
1903 >slocate</B
1904 >).</P
1905 ><P
1906 ><TT
1907 CLASS="USERINPUT"
1908 ><B
1909 >$bash locate hickson</B
1910 ></TT
1911 >
1912 <TABLE
1913 BORDER="0"
1914 BGCOLOR="#E0E0E0"
1915 WIDTH="90%"
1916 ><TR
1917 ><TD
1918 ><PRE
1919 CLASS="SCREEN"
1920 > <TT
1921 CLASS="COMPUTEROUTPUT"
1922 >/usr/lib/xephem/catalogs/hickson.edb</TT
1923 ></PRE
1924 ></TD
1925 ></TR
1926 ></TABLE
1927 ></P
1928 ></DD
1929 ><DT
1930 ><A
1931 NAME="GETFACLREF"
1932 ></A
1933 ><B
1934 CLASS="COMMAND"
1935 >getfacl</B
1936 >, <A
1937 NAME="SETFACLREF"
1938 ></A
1939 ><B
1940 CLASS="COMMAND"
1941 >setfacl</B
1942 ></DT
1943 ><DD
1944 ><P
1945 >These commands <I
1946 CLASS="FIRSTTERM"
1947 >retrieve</I
1948 > or
1949 <I
1950 CLASS="FIRSTTERM"
1951 >set</I
1952 > the <B
1953 CLASS="COMMAND"
1954 >f</B
1955 >ile
1956 <B
1957 CLASS="COMMAND"
1958 >a</B
1959 >ccess <B
1960 CLASS="COMMAND"
1961 >c</B
1962 >ontrol
1963 <B
1964 CLASS="COMMAND"
1965 >l</B
1966 >ist -- the <I
1967 CLASS="FIRSTTERM"
1968 >owner</I
1969 >,
1970 <I
1971 CLASS="FIRSTTERM"
1972 >group</I
1973 >, and file permissions.</P
1974 ><P
1975 > <TABLE
1976 BORDER="0"
1977 BGCOLOR="#E0E0E0"
1978 WIDTH="90%"
1979 ><TR
1980 ><TD
1981 ><PRE
1982 CLASS="SCREEN"
1983 > <TT
1984 CLASS="PROMPT"
1985 >bash$ </TT
1986 ><TT
1987 CLASS="USERINPUT"
1988 ><B
1989 >getfacl *</B
1990 ></TT
1991 >
1992 <TT
1993 CLASS="COMPUTEROUTPUT"
1994 ># file: test1.txt
1995 # owner: bozo
1996 # group: bozgrp
1997 user::rw-
1998 group::rw-
1999 other::r--
2000
2001 # file: test2.txt
2002 # owner: bozo
2003 # group: bozgrp
2004 user::rw-
2005 group::rw-
2006 other::r--</TT
2007 >
2008
2009
2010
2011 <TT
2012 CLASS="PROMPT"
2013 >bash$ </TT
2014 ><TT
2015 CLASS="USERINPUT"
2016 ><B
2017 >setfacl -m u:bozo:rw yearly_budget.csv</B
2018 ></TT
2019 >
2020 <TT
2021 CLASS="PROMPT"
2022 >bash$ </TT
2023 ><TT
2024 CLASS="USERINPUT"
2025 ><B
2026 >getfacl yearly_budget.csv</B
2027 ></TT
2028 >
2029 <TT
2030 CLASS="COMPUTEROUTPUT"
2031 ># file: yearly_budget.csv
2032 # owner: accountant
2033 # group: budgetgrp
2034 user::rw-
2035 user:bozo:rw-
2036 user:accountant:rw-
2037 group::rw-
2038 mask::rw-
2039 other::r--</TT
2040 >
2041 </PRE
2042 ></TD
2043 ></TR
2044 ></TABLE
2045 >
2046 </P
2047 ></DD
2048 ><DT
2049 ><A
2050 NAME="READLINKREF"
2051 ></A
2052 ><B
2053 CLASS="COMMAND"
2054 >readlink</B
2055 ></DT
2056 ><DD
2057 ><P
2058 >Disclose the file that a symbolic link points to.</P
2059 ><P
2060 > <TABLE
2061 BORDER="0"
2062 BGCOLOR="#E0E0E0"
2063 WIDTH="90%"
2064 ><TR
2065 ><TD
2066 ><PRE
2067 CLASS="SCREEN"
2068 > <TT
2069 CLASS="PROMPT"
2070 >bash$ </TT
2071 ><TT
2072 CLASS="USERINPUT"
2073 ><B
2074 >readlink /usr/bin/awk</B
2075 ></TT
2076 >
2077 <TT
2078 CLASS="COMPUTEROUTPUT"
2079 >../../bin/gawk</TT
2080 >
2081 </PRE
2082 ></TD
2083 ></TR
2084 ></TABLE
2085 >
2086 </P
2087 ></DD
2088 ><DT
2089 ><A
2090 NAME="STRINGSREF"
2091 ></A
2092 ><B
2093 CLASS="COMMAND"
2094 >strings</B
2095 ></DT
2096 ><DD
2097 ><P
2098 >Use the <B
2099 CLASS="COMMAND"
2100 >strings</B
2101 > command to find
2102 printable strings in a binary or data file. It will list
2103 sequences of printable characters found in the target
2104 file. This might be handy for a quick 'n dirty examination
2105 of a core dump or for looking at an unknown graphic image
2106 file (<TT
2107 CLASS="USERINPUT"
2108 ><B
2109 >strings image-file | more</B
2110 ></TT
2111 > might
2112 show something like <I
2113 CLASS="FIRSTTERM"
2114 >JFIF</I
2115 >,
2116 which would identify the file as a <I
2117 CLASS="FIRSTTERM"
2118 >jpeg</I
2119 >
2120 graphic). In a script, you would probably
2121 parse the output of <B
2122 CLASS="COMMAND"
2123 >strings</B
2124 >
2125 with <A
2126 HREF="textproc.html#GREPREF"
2127 >grep</A
2128 > or <A
2129 HREF="sedawk.html#SEDREF"
2130 >sed</A
2131 >. See <A
2132 HREF="loops.html#BINGREP"
2133 >Example 11-8</A
2134 >
2135 and <A
2136 HREF="loops.html#FINDSTRING"
2137 >Example 11-10</A
2138 >.</P
2139 ><DIV
2140 CLASS="EXAMPLE"
2141 ><HR><A
2142 NAME="WSTRINGS"
2143 ></A
2144 ><P
2145 ><B
2146 >Example 16-34. An <SPAN
2147 CLASS="QUOTE"
2148 >"improved"</SPAN
2149 >
2150 <I
2151 CLASS="FIRSTTERM"
2152 >strings</I
2153 > command</B
2154 ></P
2155 ><TABLE
2156 BORDER="0"
2157 BGCOLOR="#E0E0E0"
2158 WIDTH="90%"
2159 ><TR
2160 ><TD
2161 ><PRE
2162 CLASS="PROGRAMLISTING"
2163 > 1&nbsp;#!/bin/bash
2164 2&nbsp;# wstrings.sh: "word-strings" (enhanced "strings" command)
2165 3&nbsp;#
2166 4&nbsp;# This script filters the output of "strings" by checking it
2167 5&nbsp;#+ against a standard word list file.
2168 6&nbsp;# This effectively eliminates gibberish and noise,
2169 7&nbsp;#+ and outputs only recognized words.
2170 8&nbsp;
2171 9&nbsp;# ===========================================================
2172 10&nbsp;# Standard Check for Script Argument(s)
2173 11&nbsp;ARGS=1
2174 12&nbsp;E_BADARGS=85
2175 13&nbsp;E_NOFILE=86
2176 14&nbsp;
2177 15&nbsp;if [ $# -ne $ARGS ]
2178 16&nbsp;then
2179 17&nbsp; echo "Usage: `basename $0` filename"
2180 18&nbsp; exit $E_BADARGS
2181 19&nbsp;fi
2182 20&nbsp;
2183 21&nbsp;if [ ! -f "$1" ] # Check if file exists.
2184 22&nbsp;then
2185 23&nbsp; echo "File \"$1\" does not exist."
2186 24&nbsp; exit $E_NOFILE
2187 25&nbsp;fi
2188 26&nbsp;# ===========================================================
2189 27&nbsp;
2190 28&nbsp;
2191 29&nbsp;MINSTRLEN=3 # Minimum string length.
2192 30&nbsp;WORDFILE=/usr/share/dict/linux.words # Dictionary file.
2193 31&nbsp;# May specify a different word list file
2194 32&nbsp;#+ of one-word-per-line format.
2195 33&nbsp;# For example, the "yawl" word-list package,
2196 34&nbsp;# http://bash.deta.in/yawl-0.3.2.tar.gz
2197 35&nbsp;
2198 36&nbsp;
2199 37&nbsp;wlist=`strings "$1" | tr A-Z a-z | tr '[:space:]' Z | \
2200 38&nbsp; tr -cs '[:alpha:]' Z | tr -s '\173-\377' Z | tr Z ' '`
2201 39&nbsp;
2202 40&nbsp;# Translate output of 'strings' command with multiple passes of 'tr'.
2203 41&nbsp;# "tr A-Z a-z" converts to lowercase.
2204 42&nbsp;# "tr '[:space:]'" converts whitespace characters to Z's.
2205 43&nbsp;# "tr -cs '[:alpha:]' Z" converts non-alphabetic characters to Z's,
2206 44&nbsp;#+ and squeezes multiple consecutive Z's.
2207 45&nbsp;# "tr -s '\173-\377' Z" converts all characters past 'z' to Z's
2208 46&nbsp;#+ and squeezes multiple consecutive Z's,
2209 47&nbsp;#+ which gets rid of all the weird characters that the previous
2210 48&nbsp;#+ translation failed to deal with.
2211 49&nbsp;# Finally, "tr Z ' '" converts all those Z's to whitespace,
2212 50&nbsp;#+ which will be seen as word separators in the loop below.
2213 51&nbsp;
2214 52&nbsp;# ***********************************************************************
2215 53&nbsp;# Note the technique of feeding/piping the output of 'tr' back to itself,
2216 54&nbsp;#+ but with different arguments and/or options on each successive pass.
2217 55&nbsp;# ***********************************************************************
2218 56&nbsp;
2219 57&nbsp;
2220 58&nbsp;for word in $wlist # Important:
2221 59&nbsp; # $wlist must not be quoted here.
2222 60&nbsp; # "$wlist" does not work.
2223 61&nbsp; # Why not?
2224 62&nbsp;do
2225 63&nbsp; strlen=${#word} # String length.
2226 64&nbsp; if [ "$strlen" -lt "$MINSTRLEN" ] # Skip over short strings.
2227 65&nbsp; then
2228 66&nbsp; continue
2229 67&nbsp; fi
2230 68&nbsp;
2231 69&nbsp; grep -Fw $word "$WORDFILE" # Match whole words only.
2232 70&nbsp;# ^^^ # "Fixed strings" and
2233 71&nbsp; #+ "whole words" options.
2234 72&nbsp;done
2235 73&nbsp;
2236 74&nbsp;exit $?</PRE
2237 ></TD
2238 ></TR
2239 ></TABLE
2240 ><HR></DIV
2241 ></DD
2242 ></DL
2243 ></DIV
2244 ><DIV
2245 CLASS="VARIABLELIST"
2246 ><P
2247 ><B
2248 ><A
2249 NAME="COMPARISONN1"
2250 ></A
2251 >Comparison</B
2252 ></P
2253 ><DL
2254 ><DT
2255 ><A
2256 NAME="DIFFREF"
2257 ></A
2258 ><B
2259 CLASS="COMMAND"
2260 >diff</B
2261 >, <B
2262 CLASS="COMMAND"
2263 >patch</B
2264 ></DT
2265 ><DD
2266 ><P
2267 ><B
2268 CLASS="COMMAND"
2269 >diff</B
2270 >: flexible file comparison
2271 utility. It compares the target files line-by-line
2272 sequentially. In some applications, such as comparing
2273 word dictionaries, it may be helpful to filter the
2274 files through <A
2275 HREF="textproc.html#SORTREF"
2276 >sort</A
2277 >
2278 and <B
2279 CLASS="COMMAND"
2280 >uniq</B
2281 > before piping them
2282 to <B
2283 CLASS="COMMAND"
2284 >diff</B
2285 >. <TT
2286 CLASS="USERINPUT"
2287 ><B
2288 >diff file-1
2289 file-2</B
2290 ></TT
2291 > outputs the lines in the files that
2292 differ, with carets showing which file each particular
2293 line belongs to.</P
2294 ><P
2295 >The <TT
2296 CLASS="OPTION"
2297 >--side-by-side</TT
2298 > option to
2299 <B
2300 CLASS="COMMAND"
2301 >diff</B
2302 > outputs each compared file, line by
2303 line, in separate columns, with non-matching lines marked. The
2304 <TT
2305 CLASS="OPTION"
2306 >-c</TT
2307 > and <TT
2308 CLASS="OPTION"
2309 >-u</TT
2310 > options likewise
2311 make the output of the command easier to interpret.</P
2312 ><P
2313 >There are available various fancy frontends for
2314 <B
2315 CLASS="COMMAND"
2316 >diff</B
2317 >, such as <B
2318 CLASS="COMMAND"
2319 >sdiff</B
2320 >,
2321 <B
2322 CLASS="COMMAND"
2323 >wdiff</B
2324 >, <B
2325 CLASS="COMMAND"
2326 >xdiff</B
2327 >, and
2328 <B
2329 CLASS="COMMAND"
2330 >mgdiff</B
2331 >. </P
2332 ><DIV
2333 CLASS="TIP"
2334 ><TABLE
2335 CLASS="TIP"
2336 WIDTH="90%"
2337 BORDER="0"
2338 ><TR
2339 ><TD
2340 WIDTH="25"
2341 ALIGN="CENTER"
2342 VALIGN="TOP"
2343 ><IMG
2344 SRC="common/tip.png"
2345 HSPACE="5"
2346 ALT="Tip"></TD
2347 ><TD
2348 ALIGN="LEFT"
2349 VALIGN="TOP"
2350 ><P
2351 ><A
2352 NAME="DIFFERR2"
2353 ></A
2354 >The <B
2355 CLASS="COMMAND"
2356 >diff</B
2357 >
2358 command returns an exit status of <SPAN
2359 CLASS="ERRORCODE"
2360 >0</SPAN
2361 >
2362 if the compared files are identical, and
2363 <SPAN
2364 CLASS="ERRORCODE"
2365 >1</SPAN
2366 > if they differ (or
2367 <SPAN
2368 CLASS="ERRORCODE"
2369 >2</SPAN
2370 > when <I
2371 CLASS="FIRSTTERM"
2372 >binary</I
2373 >
2374 files are being compared). This permits use of
2375 <B
2376 CLASS="COMMAND"
2377 >diff</B
2378 > in a test construct within a shell
2379 script (see below).</P
2380 ></TD
2381 ></TR
2382 ></TABLE
2383 ></DIV
2384 ><P
2385 >A common use for <B
2386 CLASS="COMMAND"
2387 >diff</B
2388 > is generating
2389 difference files to be used with <B
2390 CLASS="COMMAND"
2391 >patch</B
2392 >
2393 The <TT
2394 CLASS="OPTION"
2395 >-e</TT
2396 > option outputs files suitable
2397 for <B
2398 CLASS="COMMAND"
2399 >ed</B
2400 > or <B
2401 CLASS="COMMAND"
2402 >ex</B
2403 >
2404 scripts.</P
2405 ><P
2406 ><A
2407 NAME="PATCHREF"
2408 ></A
2409 ></P
2410 ><P
2411 ><B
2412 CLASS="COMMAND"
2413 >patch</B
2414 >: flexible versioning
2415 utility. Given a difference file generated by
2416 <B
2417 CLASS="COMMAND"
2418 >diff</B
2419 >, <B
2420 CLASS="COMMAND"
2421 >patch</B
2422 > can
2423 upgrade a previous version of a package to a newer version.
2424 It is much more convenient to distribute a relatively
2425 small <SPAN
2426 CLASS="QUOTE"
2427 >"diff"</SPAN
2428 > file than the entire body of a
2429 newly revised package. Kernel <SPAN
2430 CLASS="QUOTE"
2431 >"patches"</SPAN
2432 > have
2433 become the preferred method of distributing the frequent
2434 releases of the Linux kernel.</P
2435 ><P
2436 ><TABLE
2437 BORDER="0"
2438 BGCOLOR="#E0E0E0"
2439 WIDTH="90%"
2440 ><TR
2441 ><TD
2442 ><PRE
2443 CLASS="PROGRAMLISTING"
2444 > 1&nbsp;patch -p1 &#60;patch-file
2445 2&nbsp;# Takes all the changes listed in 'patch-file'
2446 3&nbsp;# and applies them to the files referenced therein.
2447 4&nbsp;# This upgrades to a newer version of the package.</PRE
2448 ></TD
2449 ></TR
2450 ></TABLE
2451 ></P
2452 ><P
2453 >Patching the kernel:</P
2454 ><P
2455 ><TABLE
2456 BORDER="0"
2457 BGCOLOR="#E0E0E0"
2458 WIDTH="90%"
2459 ><TR
2460 ><TD
2461 ><PRE
2462 CLASS="PROGRAMLISTING"
2463 > 1&nbsp;cd /usr/src
2464 2&nbsp;gzip -cd patchXX.gz | patch -p0
2465 3&nbsp;# Upgrading kernel source using 'patch'.
2466 4&nbsp;# From the Linux kernel docs "README",
2467 5&nbsp;# by anonymous author (Alan Cox?).</PRE
2468 ></TD
2469 ></TR
2470 ></TABLE
2471 ></P
2472 ><DIV
2473 CLASS="NOTE"
2474 ><TABLE
2475 CLASS="NOTE"
2476 WIDTH="90%"
2477 BORDER="0"
2478 ><TR
2479 ><TD
2480 WIDTH="25"
2481 ALIGN="CENTER"
2482 VALIGN="TOP"
2483 ><IMG
2484 SRC="common/note.png"
2485 HSPACE="5"
2486 ALT="Note"></TD
2487 ><TD
2488 ALIGN="LEFT"
2489 VALIGN="TOP"
2490 ><P
2491 >The <B
2492 CLASS="COMMAND"
2493 >diff</B
2494 > command can also
2495 recursively compare directories (for the filenames
2496 present).</P
2497 ><P
2498 > <TABLE
2499 BORDER="0"
2500 BGCOLOR="#E0E0E0"
2501 WIDTH="90%"
2502 ><TR
2503 ><TD
2504 ><PRE
2505 CLASS="SCREEN"
2506 > <TT
2507 CLASS="PROMPT"
2508 >bash$ </TT
2509 ><TT
2510 CLASS="USERINPUT"
2511 ><B
2512 >diff -r ~/notes1 ~/notes2</B
2513 ></TT
2514 >
2515 <TT
2516 CLASS="COMPUTEROUTPUT"
2517 >Only in /home/bozo/notes1: file02
2518 Only in /home/bozo/notes1: file03
2519 Only in /home/bozo/notes2: file04</TT
2520 >
2521 </PRE
2522 ></TD
2523 ></TR
2524 ></TABLE
2525 >
2526 </P
2527 ></TD
2528 ></TR
2529 ></TABLE
2530 ></DIV
2531 ><DIV
2532 CLASS="TIP"
2533 ><TABLE
2534 CLASS="TIP"
2535 WIDTH="90%"
2536 BORDER="0"
2537 ><TR
2538 ><TD
2539 WIDTH="25"
2540 ALIGN="CENTER"
2541 VALIGN="TOP"
2542 ><IMG
2543 SRC="common/tip.png"
2544 HSPACE="5"
2545 ALT="Tip"></TD
2546 ><TD
2547 ALIGN="LEFT"
2548 VALIGN="TOP"
2549 ><P
2550 ><A
2551 NAME="ZDIFFREF"
2552 ></A
2553 ></P
2554 ><P
2555 >Use <B
2556 CLASS="COMMAND"
2557 >zdiff</B
2558 > to compare
2559 <I
2560 CLASS="FIRSTTERM"
2561 >gzipped</I
2562 > files.</P
2563 ></TD
2564 ></TR
2565 ></TABLE
2566 ></DIV
2567 ><DIV
2568 CLASS="TIP"
2569 ><TABLE
2570 CLASS="TIP"
2571 WIDTH="90%"
2572 BORDER="0"
2573 ><TR
2574 ><TD
2575 WIDTH="25"
2576 ALIGN="CENTER"
2577 VALIGN="TOP"
2578 ><IMG
2579 SRC="common/tip.png"
2580 HSPACE="5"
2581 ALT="Tip"></TD
2582 ><TD
2583 ALIGN="LEFT"
2584 VALIGN="TOP"
2585 ><P
2586 ><A
2587 NAME="DIFFSTATREF"
2588 ></A
2589 ></P
2590 ><P
2591 >Use <B
2592 CLASS="COMMAND"
2593 >diffstat</B
2594 > to create
2595 a histogram (point-distribution graph) of output from
2596 <B
2597 CLASS="COMMAND"
2598 >diff</B
2599 >.</P
2600 ></TD
2601 ></TR
2602 ></TABLE
2603 ></DIV
2604 ></DD
2605 ><DT
2606 ><A
2607 NAME="DIFF3REF"
2608 ></A
2609 ><B
2610 CLASS="COMMAND"
2611 >diff3</B
2612 >, <B
2613 CLASS="COMMAND"
2614 >merge</B
2615 ></DT
2616 ><DD
2617 ><P
2618 >An extended version of <B
2619 CLASS="COMMAND"
2620 >diff</B
2621 > that compares
2622 three files at a time. This command returns an exit value
2623 of 0 upon successful execution, but unfortunately this gives
2624 no information about the results of the comparison.</P
2625 ><P
2626 > <TABLE
2627 BORDER="0"
2628 BGCOLOR="#E0E0E0"
2629 WIDTH="90%"
2630 ><TR
2631 ><TD
2632 ><PRE
2633 CLASS="SCREEN"
2634 > <TT
2635 CLASS="PROMPT"
2636 >bash$ </TT
2637 ><TT
2638 CLASS="USERINPUT"
2639 ><B
2640 >diff3 file-1 file-2 file-3</B
2641 ></TT
2642 >
2643 <TT
2644 CLASS="COMPUTEROUTPUT"
2645 >====
2646 1:1c
2647 This is line 1 of "file-1".
2648 2:1c
2649 This is line 1 of "file-2".
2650 3:1c
2651 This is line 1 of "file-3"</TT
2652 >
2653 </PRE
2654 ></TD
2655 ></TR
2656 ></TABLE
2657 >
2658 </P
2659 ><P
2660 ><A
2661 NAME="MERGEREF"
2662 ></A
2663 >The <B
2664 CLASS="COMMAND"
2665 >merge</B
2666 >
2667 (3-way file merge) command is an interesting adjunct to
2668 <I
2669 CLASS="FIRSTTERM"
2670 >diff3</I
2671 >. Its syntax is
2672 <TT
2673 CLASS="USERINPUT"
2674 ><B
2675 >merge Mergefile file1 file2</B
2676 ></TT
2677 >.
2678 The result is to output to <TT
2679 CLASS="FILENAME"
2680 >Mergefile</TT
2681 >
2682 the changes that lead from <TT
2683 CLASS="FILENAME"
2684 >file1</TT
2685 >
2686 to <TT
2687 CLASS="FILENAME"
2688 >file2</TT
2689 >. Consider this command
2690 a stripped-down version of <I
2691 CLASS="FIRSTTERM"
2692 >patch</I
2693 >.</P
2694 ></DD
2695 ><DT
2696 ><A
2697 NAME="SDIFFREF"
2698 ></A
2699 ><B
2700 CLASS="COMMAND"
2701 >sdiff</B
2702 ></DT
2703 ><DD
2704 ><P
2705 >Compare and/or edit two files in order to merge
2706 them into an output file. Because of its interactive nature,
2707 this command would find little use in a script.</P
2708 ></DD
2709 ><DT
2710 ><A
2711 NAME="CMPREF"
2712 ></A
2713 ><B
2714 CLASS="COMMAND"
2715 >cmp</B
2716 ></DT
2717 ><DD
2718 ><P
2719 >The <B
2720 CLASS="COMMAND"
2721 >cmp</B
2722 > command is a simpler version of
2723 <B
2724 CLASS="COMMAND"
2725 >diff</B
2726 >, above. Whereas <B
2727 CLASS="COMMAND"
2728 >diff</B
2729 >
2730 reports the differences between two files,
2731 <B
2732 CLASS="COMMAND"
2733 >cmp</B
2734 > merely shows at what point they
2735 differ.</P
2736 ><DIV
2737 CLASS="NOTE"
2738 ><TABLE
2739 CLASS="NOTE"
2740 WIDTH="90%"
2741 BORDER="0"
2742 ><TR
2743 ><TD
2744 WIDTH="25"
2745 ALIGN="CENTER"
2746 VALIGN="TOP"
2747 ><IMG
2748 SRC="common/note.png"
2749 HSPACE="5"
2750 ALT="Note"></TD
2751 ><TD
2752 ALIGN="LEFT"
2753 VALIGN="TOP"
2754 ><P
2755 >Like <B
2756 CLASS="COMMAND"
2757 >diff</B
2758 >, <B
2759 CLASS="COMMAND"
2760 >cmp</B
2761 >
2762 returns an exit status of 0 if the compared files are
2763 identical, and 1 if they differ. This permits use in a test
2764 construct within a shell script.</P
2765 ></TD
2766 ></TR
2767 ></TABLE
2768 ></DIV
2769 ><DIV
2770 CLASS="EXAMPLE"
2771 ><HR><A
2772 NAME="FILECOMP"
2773 ></A
2774 ><P
2775 ><B
2776 >Example 16-35. Using <I
2777 CLASS="FIRSTTERM"
2778 >cmp</I
2779 > to compare two files
2780 within a script.</B
2781 ></P
2782 ><TABLE
2783 BORDER="0"
2784 BGCOLOR="#E0E0E0"
2785 WIDTH="90%"
2786 ><TR
2787 ><TD
2788 ><PRE
2789 CLASS="PROGRAMLISTING"
2790 > 1&nbsp;#!/bin/bash
2791 2&nbsp;# file-comparison.sh
2792 3&nbsp;
2793 4&nbsp;ARGS=2 # Two args to script expected.
2794 5&nbsp;E_BADARGS=85
2795 6&nbsp;E_UNREADABLE=86
2796 7&nbsp;
2797 8&nbsp;if [ $# -ne "$ARGS" ]
2798 9&nbsp;then
2799 10&nbsp; echo "Usage: `basename $0` file1 file2"
2800 11&nbsp; exit $E_BADARGS
2801 12&nbsp;fi
2802 13&nbsp;
2803 14&nbsp;if [[ ! -r "$1" || ! -r "$2" ]]
2804 15&nbsp;then
2805 16&nbsp; echo "Both files to be compared must exist and be readable."
2806 17&nbsp; exit $E_UNREADABLE
2807 18&nbsp;fi
2808 19&nbsp;
2809 20&nbsp;cmp $1 $2 &#38;&#62; /dev/null
2810 21&nbsp;# Redirection to /dev/null buries the output of the "cmp" command.
2811 22&nbsp;# cmp -s $1 $2 has same result ("-s" silent flag to "cmp")
2812 23&nbsp;# Thank you Anders Gustavsson for pointing this out.
2813 24&nbsp;#
2814 25&nbsp;# Also works with 'diff', i.e.,
2815 26&nbsp;#+ diff $1 $2 &#38;&#62; /dev/null
2816 27&nbsp;
2817 28&nbsp;if [ $? -eq 0 ] # Test exit status of "cmp" command.
2818 29&nbsp;then
2819 30&nbsp; echo "File \"$1\" is identical to file \"$2\"."
2820 31&nbsp;else
2821 32&nbsp; echo "File \"$1\" differs from file \"$2\"."
2822 33&nbsp;fi
2823 34&nbsp;
2824 35&nbsp;exit 0</PRE
2825 ></TD
2826 ></TR
2827 ></TABLE
2828 ><HR></DIV
2829 ><DIV
2830 CLASS="TIP"
2831 ><TABLE
2832 CLASS="TIP"
2833 WIDTH="90%"
2834 BORDER="0"
2835 ><TR
2836 ><TD
2837 WIDTH="25"
2838 ALIGN="CENTER"
2839 VALIGN="TOP"
2840 ><IMG
2841 SRC="common/tip.png"
2842 HSPACE="5"
2843 ALT="Tip"></TD
2844 ><TD
2845 ALIGN="LEFT"
2846 VALIGN="TOP"
2847 ><P
2848 >Use <B
2849 CLASS="COMMAND"
2850 >zcmp</B
2851 > on
2852 <I
2853 CLASS="FIRSTTERM"
2854 >gzipped</I
2855 > files.</P
2856 ></TD
2857 ></TR
2858 ></TABLE
2859 ></DIV
2860 ></DD
2861 ><DT
2862 ><A
2863 NAME="COMMREF"
2864 ></A
2865 ><B
2866 CLASS="COMMAND"
2867 >comm</B
2868 ></DT
2869 ><DD
2870 ><P
2871 >Versatile file comparison utility. The files must be
2872 sorted for this to be useful.</P
2873 ><P
2874 ><B
2875 CLASS="COMMAND"
2876 >comm
2877 <TT
2878 CLASS="REPLACEABLE"
2879 ><I
2880 >-options</I
2881 ></TT
2882 >
2883 <TT
2884 CLASS="REPLACEABLE"
2885 ><I
2886 >first-file</I
2887 ></TT
2888 >
2889 <TT
2890 CLASS="REPLACEABLE"
2891 ><I
2892 >second-file</I
2893 ></TT
2894 ></B
2895 ></P
2896 ><P
2897 ><TT
2898 CLASS="USERINPUT"
2899 ><B
2900 >comm file-1 file-2</B
2901 ></TT
2902 > outputs three columns:
2903 <UL
2904 ><LI
2905 ><P
2906 >column 1 = lines unique to <TT
2907 CLASS="FILENAME"
2908 >file-1</TT
2909 ></P
2910 ></LI
2911 ><LI
2912 ><P
2913 >column 2 = lines unique to <TT
2914 CLASS="FILENAME"
2915 >file-2</TT
2916 ></P
2917 ></LI
2918 ><LI
2919 ><P
2920 >column 3 = lines common to both.</P
2921 ></LI
2922 ></UL
2923 ></P
2924 ><P
2925 >The options allow suppressing output of one or more columns.
2926 <UL
2927 ><LI
2928 ><P
2929 ><TT
2930 CLASS="OPTION"
2931 >-1</TT
2932 > suppresses column
2933 <TT
2934 CLASS="LITERAL"
2935 >1</TT
2936 ></P
2937 ></LI
2938 ><LI
2939 ><P
2940 ><TT
2941 CLASS="OPTION"
2942 >-2</TT
2943 > suppresses column
2944 <TT
2945 CLASS="LITERAL"
2946 >2</TT
2947 ></P
2948 ></LI
2949 ><LI
2950 ><P
2951 ><TT
2952 CLASS="OPTION"
2953 >-3</TT
2954 > suppresses column
2955 <TT
2956 CLASS="LITERAL"
2957 >3</TT
2958 ></P
2959 ></LI
2960 ><LI
2961 ><P
2962 ><TT
2963 CLASS="OPTION"
2964 >-12</TT
2965 > suppresses both columns
2966 <TT
2967 CLASS="LITERAL"
2968 >1</TT
2969 > and <TT
2970 CLASS="LITERAL"
2971 >2</TT
2972 >, etc.</P
2973 ></LI
2974 ></UL
2975 >
2976 </P
2977 ><P
2978 >This command is useful for comparing
2979 <SPAN
2980 CLASS="QUOTE"
2981 >"dictionaries"</SPAN
2982 > or <I
2983 CLASS="FIRSTTERM"
2984 >word
2985 lists</I
2986 > -- sorted text files with one word per
2987 line.</P
2988 ></DD
2989 ></DL
2990 ></DIV
2991 ><DIV
2992 CLASS="VARIABLELIST"
2993 ><P
2994 ><B
2995 ><A
2996 NAME="FAUTILS1"
2997 ></A
2998 >Utilities</B
2999 ></P
3000 ><DL
3001 ><DT
3002 ><A
3003 NAME="BASENAMEREF"
3004 ></A
3005 ><B
3006 CLASS="COMMAND"
3007 >basename</B
3008 ></DT
3009 ><DD
3010 ><P
3011 >Strips the path information from a file name, printing
3012 only the file name. The construction <TT
3013 CLASS="USERINPUT"
3014 ><B
3015 >basename
3016 $0</B
3017 ></TT
3018 > lets the script know its name, that is, the name it
3019 was invoked by. This can be used for <SPAN
3020 CLASS="QUOTE"
3021 >"usage"</SPAN
3022 > messages if,
3023 for example a script is called with missing arguments:
3024 <TABLE
3025 BORDER="0"
3026 BGCOLOR="#E0E0E0"
3027 WIDTH="90%"
3028 ><TR
3029 ><TD
3030 ><PRE
3031 CLASS="PROGRAMLISTING"
3032 > 1&nbsp;echo "Usage: `basename $0` arg1 arg2 ... argn"</PRE
3033 ></TD
3034 ></TR
3035 ></TABLE
3036 >
3037 </P
3038 ></DD
3039 ><DT
3040 ><A
3041 NAME="DIRNAMEREF"
3042 ></A
3043 ><B
3044 CLASS="COMMAND"
3045 >dirname</B
3046 ></DT
3047 ><DD
3048 ><P
3049 >Strips the <B
3050 CLASS="COMMAND"
3051 >basename</B
3052 > from
3053 a filename, printing only the path information.</P
3054 ><DIV
3055 CLASS="NOTE"
3056 ><TABLE
3057 CLASS="NOTE"
3058 WIDTH="90%"
3059 BORDER="0"
3060 ><TR
3061 ><TD
3062 WIDTH="25"
3063 ALIGN="CENTER"
3064 VALIGN="TOP"
3065 ><IMG
3066 SRC="common/note.png"
3067 HSPACE="5"
3068 ALT="Note"></TD
3069 ><TD
3070 ALIGN="LEFT"
3071 VALIGN="TOP"
3072 ><P
3073 ><B
3074 CLASS="COMMAND"
3075 >basename</B
3076 > and <B
3077 CLASS="COMMAND"
3078 >dirname</B
3079 >
3080 can operate on any arbitrary string. The argument
3081 does not need to refer to an existing file, or
3082 even be a filename for that matter (see <A
3083 HREF="contributed-scripts.html#DAYSBETWEEN"
3084 >Example A-7</A
3085 >).</P
3086 ></TD
3087 ></TR
3088 ></TABLE
3089 ></DIV
3090 ><DIV
3091 CLASS="EXAMPLE"
3092 ><HR><A
3093 NAME="EX35"
3094 ></A
3095 ><P
3096 ><B
3097 >Example 16-36. <I
3098 CLASS="FIRSTTERM"
3099 >basename</I
3100 > and
3101 <I
3102 CLASS="FIRSTTERM"
3103 >dirname</I
3104 ></B
3105 ></P
3106 ><TABLE
3107 BORDER="0"
3108 BGCOLOR="#E0E0E0"
3109 WIDTH="90%"
3110 ><TR
3111 ><TD
3112 ><PRE
3113 CLASS="PROGRAMLISTING"
3114 > 1&nbsp;#!/bin/bash
3115 2&nbsp;
3116 3&nbsp;address=/home/bozo/daily-journal.txt
3117 4&nbsp;
3118 5&nbsp;echo "Basename of /home/bozo/daily-journal.txt = `basename $address`"
3119 6&nbsp;echo "Dirname of /home/bozo/daily-journal.txt = `dirname $address`"
3120 7&nbsp;echo
3121 8&nbsp;echo "My own home is `basename ~/`." # `basename ~` also works.
3122 9&nbsp;echo "The home of my home is `dirname ~/`." # `dirname ~` also works.
3123 10&nbsp;
3124 11&nbsp;exit 0</PRE
3125 ></TD
3126 ></TR
3127 ></TABLE
3128 ><HR></DIV
3129 ></DD
3130 ><DT
3131 ><A
3132 NAME="SPLITREF"
3133 ></A
3134 ><B
3135 CLASS="COMMAND"
3136 >split</B
3137 >, <A
3138 NAME="CSPLITREF"
3139 ></A
3140 ><B
3141 CLASS="COMMAND"
3142 >csplit</B
3143 ></DT
3144 ><DD
3145 ><P
3146 >These are utilities for splitting a file into smaller
3147 chunks. Their usual use is for splitting up large files
3148 in order to back them up on floppies or preparatory to
3149 e-mailing or uploading them.</P
3150 ><P
3151 >The <B
3152 CLASS="COMMAND"
3153 >csplit</B
3154 > command splits a file
3155 according to <I
3156 CLASS="FIRSTTERM"
3157 >context</I
3158 >, the split occuring
3159 where patterns are matched.</P
3160 ><DIV
3161 CLASS="EXAMPLE"
3162 ><HR><A
3163 NAME="SPLITCOPY"
3164 ></A
3165 ><P
3166 ><B
3167 >Example 16-37. A script that copies itself in sections</B
3168 ></P
3169 ><TABLE
3170 BORDER="0"
3171 BGCOLOR="#E0E0E0"
3172 WIDTH="90%"
3173 ><TR
3174 ><TD
3175 ><PRE
3176 CLASS="PROGRAMLISTING"
3177 > 1&nbsp;#!/bin/bash
3178 2&nbsp;# splitcopy.sh
3179 3&nbsp;
3180 4&nbsp;# A script that splits itself into chunks,
3181 5&nbsp;#+ then reassembles the chunks into an exact copy
3182 6&nbsp;#+ of the original script.
3183 7&nbsp;
3184 8&nbsp;CHUNKSIZE=4 # Size of first chunk of split files.
3185 9&nbsp;OUTPREFIX=xx # csplit prefixes, by default,
3186 10&nbsp; #+ files with "xx" ...
3187 11&nbsp;
3188 12&nbsp;csplit "$0" "$CHUNKSIZE"
3189 13&nbsp;
3190 14&nbsp;# Some comment lines for padding . . .
3191 15&nbsp;# Line 15
3192 16&nbsp;# Line 16
3193 17&nbsp;# Line 17
3194 18&nbsp;# Line 18
3195 19&nbsp;# Line 19
3196 20&nbsp;# Line 20
3197 21&nbsp;
3198 22&nbsp;cat "$OUTPREFIX"* &#62; "$0.copy" # Concatenate the chunks.
3199 23&nbsp;rm "$OUTPREFIX"* # Get rid of the chunks.
3200 24&nbsp;
3201 25&nbsp;exit $?</PRE
3202 ></TD
3203 ></TR
3204 ></TABLE
3205 ><HR></DIV
3206 ></DD
3207 ></DL
3208 ></DIV
3209 ><DIV
3210 CLASS="VARIABLELIST"
3211 ><P
3212 ><B
3213 ><A
3214 NAME="FAENCENCR1"
3215 ></A
3216 >Encoding and Encryption</B
3217 ></P
3218 ><DL
3219 ><DT
3220 ><A
3221 NAME="SUMREF"
3222 ></A
3223 ><B
3224 CLASS="COMMAND"
3225 >sum</B
3226 >, <A
3227 NAME="CKSUMREF"
3228 ></A
3229 ><B
3230 CLASS="COMMAND"
3231 >cksum</B
3232 >, <A
3233 NAME="MD5SUMREF"
3234 ></A
3235 ><B
3236 CLASS="COMMAND"
3237 >md5sum</B
3238 >, <A
3239 NAME="SHA1SUMREF"
3240 ></A
3241 ><B
3242 CLASS="COMMAND"
3243 >sha1sum</B
3244 ></DT
3245 ><DD
3246 ><P
3247 ><A
3248 NAME="CHECKSUMREF"
3249 ></A
3250 >These are utilities for
3251 generating <I
3252 CLASS="FIRSTTERM"
3253 >checksums</I
3254 >. A
3255 <I
3256 CLASS="FIRSTTERM"
3257 >checksum</I
3258 > is a number
3259
3260 <A
3261 NAME="AEN12840"
3262 HREF="#FTN.AEN12840"
3263 >[3]</A
3264 >
3265
3266 mathematically calculated from the contents of a file,
3267 for the purpose of checking its integrity. A script might
3268 refer to a list of checksums for security purposes, such
3269 as ensuring that the contents of key system files have not
3270 been altered or corrupted. For security applications, use
3271 the <B
3272 CLASS="COMMAND"
3273 >md5sum</B
3274 > (<B
3275 CLASS="COMMAND"
3276 >m</B
3277 >essage
3278 <B
3279 CLASS="COMMAND"
3280 >d</B
3281 >igest <B
3282 CLASS="COMMAND"
3283 >5</B
3284 >
3285 check<B
3286 CLASS="COMMAND"
3287 >sum</B
3288 >) command, or better yet, the
3289 newer <B
3290 CLASS="COMMAND"
3291 >sha1sum</B
3292 > (Secure Hash Algorithm).
3293
3294 <A
3295 NAME="AEN12849"
3296 HREF="#FTN.AEN12849"
3297 >[4]</A
3298 >
3299 </P
3300 ><P
3301 > <TABLE
3302 BORDER="0"
3303 BGCOLOR="#E0E0E0"
3304 WIDTH="90%"
3305 ><TR
3306 ><TD
3307 ><PRE
3308 CLASS="SCREEN"
3309 > <TT
3310 CLASS="PROMPT"
3311 >bash$ </TT
3312 ><TT
3313 CLASS="USERINPUT"
3314 ><B
3315 >cksum /boot/vmlinuz</B
3316 ></TT
3317 >
3318 <TT
3319 CLASS="COMPUTEROUTPUT"
3320 >1670054224 804083 /boot/vmlinuz</TT
3321 >
3322
3323 <TT
3324 CLASS="PROMPT"
3325 >bash$ </TT
3326 ><TT
3327 CLASS="USERINPUT"
3328 ><B
3329 >echo -n "Top Secret" | cksum</B
3330 ></TT
3331 >
3332 <TT
3333 CLASS="COMPUTEROUTPUT"
3334 >3391003827 10</TT
3335 >
3336
3337
3338
3339 <TT
3340 CLASS="PROMPT"
3341 >bash$ </TT
3342 ><TT
3343 CLASS="USERINPUT"
3344 ><B
3345 >md5sum /boot/vmlinuz</B
3346 ></TT
3347 >
3348 <TT
3349 CLASS="COMPUTEROUTPUT"
3350 >0f43eccea8f09e0a0b2b5cf1dcf333ba /boot/vmlinuz</TT
3351 >
3352
3353 <TT
3354 CLASS="PROMPT"
3355 >bash$ </TT
3356 ><TT
3357 CLASS="USERINPUT"
3358 ><B
3359 >echo -n "Top Secret" | md5sum</B
3360 ></TT
3361 >
3362 <TT
3363 CLASS="COMPUTEROUTPUT"
3364 >8babc97a6f62a4649716f4df8d61728f -</TT
3365 >
3366 </PRE
3367 ></TD
3368 ></TR
3369 ></TABLE
3370 >
3371 </P
3372 ><DIV
3373 CLASS="NOTE"
3374 ><TABLE
3375 CLASS="NOTE"
3376 WIDTH="90%"
3377 BORDER="0"
3378 ><TR
3379 ><TD
3380 WIDTH="25"
3381 ALIGN="CENTER"
3382 VALIGN="TOP"
3383 ><IMG
3384 SRC="common/note.png"
3385 HSPACE="5"
3386 ALT="Note"></TD
3387 ><TD
3388 ALIGN="LEFT"
3389 VALIGN="TOP"
3390 ><P
3391 >The <B
3392 CLASS="COMMAND"
3393 >cksum</B
3394 > command shows the size,
3395 in bytes, of its target, whether file or
3396 <TT
3397 CLASS="FILENAME"
3398 >stdout</TT
3399 >.</P
3400 ><P
3401 >The <B
3402 CLASS="COMMAND"
3403 >md5sum</B
3404 > and
3405 <B
3406 CLASS="COMMAND"
3407 >sha1sum</B
3408 > commands display a
3409 <A
3410 HREF="special-chars.html#DASHREF2"
3411 >dash</A
3412 > when they receive their input from
3413 <TT
3414 CLASS="FILENAME"
3415 >stdout</TT
3416 >.</P
3417 ></TD
3418 ></TR
3419 ></TABLE
3420 ></DIV
3421 ><DIV
3422 CLASS="EXAMPLE"
3423 ><HR><A
3424 NAME="FILEINTEGRITY"
3425 ></A
3426 ><P
3427 ><B
3428 >Example 16-38. Checking file integrity</B
3429 ></P
3430 ><TABLE
3431 BORDER="0"
3432 BGCOLOR="#E0E0E0"
3433 WIDTH="90%"
3434 ><TR
3435 ><TD
3436 ><PRE
3437 CLASS="PROGRAMLISTING"
3438 > 1&nbsp;#!/bin/bash
3439 2&nbsp;# file-integrity.sh: Checking whether files in a given directory
3440 3&nbsp;# have been tampered with.
3441 4&nbsp;
3442 5&nbsp;E_DIR_NOMATCH=80
3443 6&nbsp;E_BAD_DBFILE=81
3444 7&nbsp;
3445 8&nbsp;dbfile=File_record.md5
3446 9&nbsp;# Filename for storing records (database file).
3447 10&nbsp;
3448 11&nbsp;
3449 12&nbsp;set_up_database ()
3450 13&nbsp;{
3451 14&nbsp; echo ""$directory"" &#62; "$dbfile"
3452 15&nbsp; # Write directory name to first line of file.
3453 16&nbsp; md5sum "$directory"/* &#62;&#62; "$dbfile"
3454 17&nbsp; # Append md5 checksums and filenames.
3455 18&nbsp;}
3456 19&nbsp;
3457 20&nbsp;check_database ()
3458 21&nbsp;{
3459 22&nbsp; local n=0
3460 23&nbsp; local filename
3461 24&nbsp; local checksum
3462 25&nbsp;
3463 26&nbsp; # ------------------------------------------- #
3464 27&nbsp; # This file check should be unnecessary,
3465 28&nbsp; #+ but better safe than sorry.
3466 29&nbsp;
3467 30&nbsp; if [ ! -r "$dbfile" ]
3468 31&nbsp; then
3469 32&nbsp; echo "Unable to read checksum database file!"
3470 33&nbsp; exit $E_BAD_DBFILE
3471 34&nbsp; fi
3472 35&nbsp; # ------------------------------------------- #
3473 36&nbsp;
3474 37&nbsp; while read record[n]
3475 38&nbsp; do
3476 39&nbsp;
3477 40&nbsp; directory_checked="${record[0]}"
3478 41&nbsp; if [ "$directory_checked" != "$directory" ]
3479 42&nbsp; then
3480 43&nbsp; echo "Directories do not match up!"
3481 44&nbsp; # Tried to use file for a different directory.
3482 45&nbsp; exit $E_DIR_NOMATCH
3483 46&nbsp; fi
3484 47&nbsp;
3485 48&nbsp; if [ "$n" -gt 0 ] # Not directory name.
3486 49&nbsp; then
3487 50&nbsp; filename[n]=$( echo ${record[$n]} | awk '{ print $2 }' )
3488 51&nbsp; # md5sum writes records backwards,
3489 52&nbsp; #+ checksum first, then filename.
3490 53&nbsp; checksum[n]=$( md5sum "${filename[n]}" )
3491 54&nbsp;
3492 55&nbsp;
3493 56&nbsp; if [ "${record[n]}" = "${checksum[n]}" ]
3494 57&nbsp; then
3495 58&nbsp; echo "${filename[n]} unchanged."
3496 59&nbsp;
3497 60&nbsp; elif [ "`basename ${filename[n]}`" != "$dbfile" ]
3498 61&nbsp; # Skip over checksum database file,
3499 62&nbsp; #+ as it will change with each invocation of script.
3500 63&nbsp; # ---
3501 64&nbsp; # This unfortunately means that when running
3502 65&nbsp; #+ this script on $PWD, tampering with the
3503 66&nbsp; #+ checksum database file will not be detected.
3504 67&nbsp; # Exercise: Fix this.
3505 68&nbsp; then
3506 69&nbsp; echo "${filename[n]} : CHECKSUM ERROR!"
3507 70&nbsp; # File has been changed since last checked.
3508 71&nbsp; fi
3509 72&nbsp;
3510 73&nbsp; fi
3511 74&nbsp;
3512 75&nbsp;
3513 76&nbsp;
3514 77&nbsp; let "n+=1"
3515 78&nbsp; done &#60;"$dbfile" # Read from checksum database file.
3516 79&nbsp;
3517 80&nbsp;}
3518 81&nbsp;
3519 82&nbsp;# =================================================== #
3520 83&nbsp;# main ()
3521 84&nbsp;
3522 85&nbsp;if [ -z "$1" ]
3523 86&nbsp;then
3524 87&nbsp; directory="$PWD" # If not specified,
3525 88&nbsp;else #+ use current working directory.
3526 89&nbsp; directory="$1"
3527 90&nbsp;fi
3528 91&nbsp;
3529 92&nbsp;clear # Clear screen.
3530 93&nbsp;echo " Running file integrity check on $directory"
3531 94&nbsp;echo
3532 95&nbsp;
3533 96&nbsp;# ------------------------------------------------------------------ #
3534 97&nbsp; if [ ! -r "$dbfile" ] # Need to create database file?
3535 98&nbsp; then
3536 99&nbsp; echo "Setting up database file, \""$directory"/"$dbfile"\"."; echo
3537 100&nbsp; set_up_database
3538 101&nbsp; fi
3539 102&nbsp;# ------------------------------------------------------------------ #
3540 103&nbsp;
3541 104&nbsp;check_database # Do the actual work.
3542 105&nbsp;
3543 106&nbsp;echo
3544 107&nbsp;
3545 108&nbsp;# You may wish to redirect the stdout of this script to a file,
3546 109&nbsp;#+ especially if the directory checked has many files in it.
3547 110&nbsp;
3548 111&nbsp;exit 0
3549 112&nbsp;
3550 113&nbsp;# For a much more thorough file integrity check,
3551 114&nbsp;#+ consider the "Tripwire" package,
3552 115&nbsp;#+ http://sourceforge.net/projects/tripwire/.</PRE
3553 ></TD
3554 ></TR
3555 ></TABLE
3556 ><HR></DIV
3557 ><P
3558 >Also see <A
3559 HREF="contributed-scripts.html#DIRECTORYINFO"
3560 >Example A-19</A
3561 >, <A
3562 HREF="colorizing.html#HORSERACE"
3563 >Example 36-16</A
3564 >, and <A
3565 HREF="manipulatingvars.html#RANDSTRING"
3566 >Example 10-2</A
3567 > for
3568 creative uses of the <B
3569 CLASS="COMMAND"
3570 >md5sum</B
3571 > command.</P
3572 ><DIV
3573 CLASS="NOTE"
3574 ><TABLE
3575 CLASS="NOTE"
3576 WIDTH="90%"
3577 BORDER="0"
3578 ><TR
3579 ><TD
3580 WIDTH="25"
3581 ALIGN="CENTER"
3582 VALIGN="TOP"
3583 ><IMG
3584 SRC="common/note.png"
3585 HSPACE="5"
3586 ALT="Note"></TD
3587 ><TD
3588 ALIGN="LEFT"
3589 VALIGN="TOP"
3590 ><P
3591 > There have been reports that the 128-bit
3592 <B
3593 CLASS="COMMAND"
3594 >md5sum</B
3595 > can be cracked, so the more secure
3596 160-bit <B
3597 CLASS="COMMAND"
3598 >sha1sum</B
3599 > is a welcome new addition
3600 to the checksum toolkit.
3601 </P
3602 ><TABLE
3603 BORDER="0"
3604 BGCOLOR="#E0E0E0"
3605 WIDTH="90%"
3606 ><TR
3607 ><TD
3608 ><PRE
3609 CLASS="SCREEN"
3610 > <TT
3611 CLASS="PROMPT"
3612 >bash$ </TT
3613 ><TT
3614 CLASS="USERINPUT"
3615 ><B
3616 >md5sum testfile</B
3617 ></TT
3618 >
3619 <TT
3620 CLASS="COMPUTEROUTPUT"
3621 >e181e2c8720c60522c4c4c981108e367 testfile</TT
3622 >
3623
3624
3625 <TT
3626 CLASS="PROMPT"
3627 >bash$ </TT
3628 ><TT
3629 CLASS="USERINPUT"
3630 ><B
3631 >sha1sum testfile</B
3632 ></TT
3633 >
3634 <TT
3635 CLASS="COMPUTEROUTPUT"
3636 >5d7425a9c08a66c3177f1e31286fa40986ffc996 testfile</TT
3637 >
3638 </PRE
3639 ></TD
3640 ></TR
3641 ></TABLE
3642 ></TD
3643 ></TR
3644 ></TABLE
3645 ></DIV
3646 ><P
3647 >Security consultants have demonstrated that even
3648 <B
3649 CLASS="COMMAND"
3650 >sha1sum</B
3651 > can be compromised. Fortunately,
3652 newer Linux distros include longer bit-length
3653 <B
3654 CLASS="COMMAND"
3655 >sha224sum</B
3656 >,
3657 <B
3658 CLASS="COMMAND"
3659 >sha256sum</B
3660 >,
3661 <B
3662 CLASS="COMMAND"
3663 >sha384sum</B
3664 >, and
3665 <B
3666 CLASS="COMMAND"
3667 >sha512sum</B
3668 > commands.</P
3669 ></DD
3670 ><DT
3671 ><A
3672 NAME="UUENCODEREF"
3673 ></A
3674 ><B
3675 CLASS="COMMAND"
3676 >uuencode</B
3677 ></DT
3678 ><DD
3679 ><P
3680 >This utility encodes binary files (images, sound files,
3681 compressed files, etc.) into <A
3682 HREF="special-chars.html#ASCIIDEF"
3683 >ASCII</A
3684 > characters, making
3685 them suitable for transmission in the body of an
3686 e-mail message or in a newsgroup posting. This is
3687 especially useful where MIME (multimedia) encoding
3688 is not available.</P
3689 ></DD
3690 ><DT
3691 ><A
3692 NAME="UUDECODEREF"
3693 ></A
3694 ><B
3695 CLASS="COMMAND"
3696 >uudecode</B
3697 ></DT
3698 ><DD
3699 ><P
3700 >This reverses the encoding, decoding
3701 <I
3702 CLASS="FIRSTTERM"
3703 >uuencoded</I
3704 > files back into the
3705 original binaries.</P
3706 ><DIV
3707 CLASS="EXAMPLE"
3708 ><HR><A
3709 NAME="EX52"
3710 ></A
3711 ><P
3712 ><B
3713 >Example 16-39. Uudecoding encoded files</B
3714 ></P
3715 ><TABLE
3716 BORDER="0"
3717 BGCOLOR="#E0E0E0"
3718 WIDTH="90%"
3719 ><TR
3720 ><TD
3721 ><PRE
3722 CLASS="PROGRAMLISTING"
3723 > 1&nbsp;#!/bin/bash
3724 2&nbsp;# Uudecodes all uuencoded files in current working directory.
3725 3&nbsp;
3726 4&nbsp;lines=35 # Allow 35 lines for the header (very generous).
3727 5&nbsp;
3728 6&nbsp;for File in * # Test all the files in $PWD.
3729 7&nbsp;do
3730 8&nbsp; search1=`head -n $lines $File | grep begin | wc -w`
3731 9&nbsp; search2=`tail -n $lines $File | grep end | wc -w`
3732 10&nbsp; # Uuencoded files have a "begin" near the beginning,
3733 11&nbsp; #+ and an "end" near the end.
3734 12&nbsp; if [ "$search1" -gt 0 ]
3735 13&nbsp; then
3736 14&nbsp; if [ "$search2" -gt 0 ]
3737 15&nbsp; then
3738 16&nbsp; echo "uudecoding - $File -"
3739 17&nbsp; uudecode $File
3740 18&nbsp; fi
3741 19&nbsp; fi
3742 20&nbsp;done
3743 21&nbsp;
3744 22&nbsp;# Note that running this script upon itself fools it
3745 23&nbsp;#+ into thinking it is a uuencoded file,
3746 24&nbsp;#+ because it contains both "begin" and "end".
3747 25&nbsp;
3748 26&nbsp;# Exercise:
3749 27&nbsp;# --------
3750 28&nbsp;# Modify this script to check each file for a newsgroup header,
3751 29&nbsp;#+ and skip to next if not found.
3752 30&nbsp;
3753 31&nbsp;exit 0</PRE
3754 ></TD
3755 ></TR
3756 ></TABLE
3757 ><HR></DIV
3758 ><DIV
3759 CLASS="TIP"
3760 ><TABLE
3761 CLASS="TIP"
3762 WIDTH="90%"
3763 BORDER="0"
3764 ><TR
3765 ><TD
3766 WIDTH="25"
3767 ALIGN="CENTER"
3768 VALIGN="TOP"
3769 ><IMG
3770 SRC="common/tip.png"
3771 HSPACE="5"
3772 ALT="Tip"></TD
3773 ><TD
3774 ALIGN="LEFT"
3775 VALIGN="TOP"
3776 ><P
3777 >The <A
3778 HREF="textproc.html#FOLDREF"
3779 >fold -s</A
3780 > command
3781 may be useful (possibly in a pipe) to process long uudecoded
3782 text messages downloaded from Usenet newsgroups.</P
3783 ></TD
3784 ></TR
3785 ></TABLE
3786 ></DIV
3787 ></DD
3788 ><DT
3789 ><A
3790 NAME="MIMENCODEREF"
3791 ></A
3792 ><B
3793 CLASS="COMMAND"
3794 >mimencode</B
3795 >, <A
3796 NAME="MMENCODEREF"
3797 ></A
3798 ><B
3799 CLASS="COMMAND"
3800 >mmencode</B
3801 ></DT
3802 ><DD
3803 ><P
3804 >The <B
3805 CLASS="COMMAND"
3806 >mimencode</B
3807 > and
3808 <B
3809 CLASS="COMMAND"
3810 >mmencode</B
3811 > commands process
3812 multimedia-encoded e-mail attachments. Although
3813 <I
3814 CLASS="FIRSTTERM"
3815 >mail user agents</I
3816 > (such as
3817 <I
3818 CLASS="FIRSTTERM"
3819 >pine</I
3820 > or <I
3821 CLASS="FIRSTTERM"
3822 >kmail</I
3823 >)
3824 normally handle this automatically, these particular
3825 utilities permit manipulating such attachments manually from
3826 the command-line or in <A
3827 HREF="timedate.html#BATCHPROCREF"
3828 >batch
3829 processing mode</A
3830 > by means of a shell script.</P
3831 ></DD
3832 ><DT
3833 ><A
3834 NAME="CRYPTREF"
3835 ></A
3836 ><B
3837 CLASS="COMMAND"
3838 >crypt</B
3839 ></DT
3840 ><DD
3841 ><P
3842 >At one time, this was the standard UNIX file encryption
3843 utility.
3844
3845 <A
3846 NAME="AEN12969"
3847 HREF="#FTN.AEN12969"
3848 >[5]</A
3849 >
3850
3851 Politically-motivated government regulations
3852 prohibiting the export of encryption software resulted
3853 in the disappearance of <B
3854 CLASS="COMMAND"
3855 >crypt</B
3856 >
3857 from much of the UNIX world, and it is still
3858 missing from most Linux distributions. Fortunately,
3859 programmers have come up with a number of decent
3860 alternatives to it, among them the author's very own <A
3861 HREF="ftp://metalab.unc.edu/pub/Linux/utils/file/cruft-0.2.tar.gz"
3862 TARGET="_top"
3863 >cruft</A
3864 >
3865 (see <A
3866 HREF="contributed-scripts.html#ENCRYPTEDPW"
3867 >Example A-4</A
3868 >). </P
3869 ></DD
3870 ><DT
3871 ><A
3872 NAME="OPENSSLREF"
3873 ></A
3874 ><B
3875 CLASS="COMMAND"
3876 >openssl</B
3877 ></DT
3878 ><DD
3879 ><P
3880 >This is an Open Source implementation of
3881 <I
3882 CLASS="FIRSTTERM"
3883 >Secure Sockets Layer</I
3884 > encryption.
3885
3886 <TABLE
3887 BORDER="0"
3888 BGCOLOR="#E0E0E0"
3889 WIDTH="90%"
3890 ><TR
3891 ><TD
3892 ><PRE
3893 CLASS="PROGRAMLISTING"
3894 > 1&nbsp;# To encrypt a file:
3895 2&nbsp;openssl aes-128-ecb -salt -in file.txt -out file.encrypted \
3896 3&nbsp;-pass pass:my_password
3897 4&nbsp;# ^^^^^^^^^^^ User-selected password.
3898 5&nbsp;# aes-128-ecb is the encryption method chosen.
3899 6&nbsp;
3900 7&nbsp;# To decrypt an openssl-encrypted file:
3901 8&nbsp;openssl aes-128-ecb -d -salt -in file.encrypted -out file.txt \
3902 9&nbsp;-pass pass:my_password
3903 10&nbsp;# ^^^^^^^^^^^ User-selected password.</PRE
3904 ></TD
3905 ></TR
3906 ></TABLE
3907 ></P
3908 ><P
3909 ><A
3910 HREF="special-chars.html#PIPEREF"
3911 >Piping</A
3912 >
3913 <I
3914 CLASS="FIRSTTERM"
3915 >openssl</I
3916 > to/from <A
3917 HREF="filearchiv.html#TARREF"
3918 >tar</A
3919 > makes it possible to encrypt
3920 an entire directory tree.
3921
3922 <TABLE
3923 BORDER="0"
3924 BGCOLOR="#E0E0E0"
3925 WIDTH="90%"
3926 ><TR
3927 ><TD
3928 ><PRE
3929 CLASS="PROGRAMLISTING"
3930 > 1&nbsp;# To encrypt a directory:
3931 2&nbsp;
3932 3&nbsp;sourcedir="/home/bozo/testfiles"
3933 4&nbsp;encrfile="encr-dir.tar.gz"
3934 5&nbsp;password=my_secret_password
3935 6&nbsp;
3936 7&nbsp;tar czvf - "$sourcedir" |
3937 8&nbsp;openssl des3 -salt -out "$encrfile" -pass pass:"$password"
3938 9&nbsp;# ^^^^ Uses des3 encryption.
3939 10&nbsp;# Writes encrypted file "encr-dir.tar.gz" in current working directory.
3940 11&nbsp;
3941 12&nbsp;# To decrypt the resulting tarball:
3942 13&nbsp;openssl des3 -d -salt -in "$encrfile" -pass pass:"$password" |
3943 14&nbsp;tar -xzv
3944 15&nbsp;# Decrypts and unpacks into current working directory.</PRE
3945 ></TD
3946 ></TR
3947 ></TABLE
3948 >
3949
3950 </P
3951 ><P
3952 >Of course, <I
3953 CLASS="FIRSTTERM"
3954 >openssl</I
3955 > has many other uses,
3956 such as obtaining signed <I
3957 CLASS="FIRSTTERM"
3958 >certificates</I
3959 >
3960 for Web sites. See the <A
3961 HREF="external.html#INFOREF"
3962 >info</A
3963 >
3964 page.</P
3965 ></DD
3966 ><DT
3967 ><A
3968 NAME="SHREDREF"
3969 ></A
3970 ><B
3971 CLASS="COMMAND"
3972 >shred</B
3973 ></DT
3974 ><DD
3975 ><P
3976 >Securely erase a file by overwriting it multiple times with
3977 random bit patterns before deleting it. This command has
3978 the same effect as <A
3979 HREF="extmisc.html#BLOTOUT"
3980 >Example 16-61</A
3981 >, but does it
3982 in a more thorough and elegant manner.</P
3983 ><P
3984 >This is one of the GNU
3985 <I
3986 CLASS="FIRSTTERM"
3987 >fileutils</I
3988 >.</P
3989 ><DIV
3990 CLASS="CAUTION"
3991 ><TABLE
3992 CLASS="CAUTION"
3993 WIDTH="90%"
3994 BORDER="0"
3995 ><TR
3996 ><TD
3997 WIDTH="25"
3998 ALIGN="CENTER"
3999 VALIGN="TOP"
4000 ><IMG
4001 SRC="common/caution.png"
4002 HSPACE="5"
4003 ALT="Caution"></TD
4004 ><TD
4005 ALIGN="LEFT"
4006 VALIGN="TOP"
4007 ><P
4008 >Advanced forensic technology may still be able to
4009 recover the contents of a file, even after application of
4010 <B
4011 CLASS="COMMAND"
4012 >shred</B
4013 >.</P
4014 ></TD
4015 ></TR
4016 ></TABLE
4017 ></DIV
4018 ></DD
4019 ></DL
4020 ></DIV
4021 ><DIV
4022 CLASS="VARIABLELIST"
4023 ><P
4024 ><B
4025 ><A
4026 NAME="FAMISC1"
4027 ></A
4028 >Miscellaneous</B
4029 ></P
4030 ><DL
4031 ><DT
4032 ><A
4033 NAME="MKTEMPREF"
4034 ></A
4035 ><B
4036 CLASS="COMMAND"
4037 >mktemp</B
4038 ></DT
4039 ><DD
4040 ><P
4041 >Create a <I
4042 CLASS="FIRSTTERM"
4043 >temporary file</I
4044 >
4045
4046 <A
4047 NAME="AEN13030"
4048 HREF="#FTN.AEN13030"
4049 >[6]</A
4050 >
4051
4052 with a <SPAN
4053 CLASS="QUOTE"
4054 >"unique"</SPAN
4055 > filename. When invoked
4056 from the command-line without additional arguments,
4057 it creates a zero-length file in the <TT
4058 CLASS="FILENAME"
4059 >/tmp</TT
4060 > directory.</P
4061 ><P
4062 > <TABLE
4063 BORDER="0"
4064 BGCOLOR="#E0E0E0"
4065 WIDTH="90%"
4066 ><TR
4067 ><TD
4068 ><PRE
4069 CLASS="SCREEN"
4070 > <TT
4071 CLASS="PROMPT"
4072 >bash$ </TT
4073 ><TT
4074 CLASS="USERINPUT"
4075 ><B
4076 >mktemp</B
4077 ></TT
4078 >
4079 <TT
4080 CLASS="COMPUTEROUTPUT"
4081 >/tmp/tmp.zzsvql3154</TT
4082 >
4083 </PRE
4084 ></TD
4085 ></TR
4086 ></TABLE
4087 >
4088 </P
4089 ><P
4090 ><TABLE
4091 BORDER="0"
4092 BGCOLOR="#E0E0E0"
4093 WIDTH="90%"
4094 ><TR
4095 ><TD
4096 ><PRE
4097 CLASS="PROGRAMLISTING"
4098 > 1&nbsp;PREFIX=filename
4099 2&nbsp;tempfile=`mktemp $PREFIX.XXXXXX`
4100 3&nbsp;# ^^^^^^ Need at least 6 placeholders
4101 4&nbsp;#+ in the filename template.
4102 5&nbsp;# If no filename template supplied,
4103 6&nbsp;#+ "tmp.XXXXXXXXXX" is the default.
4104 7&nbsp;
4105 8&nbsp;echo "tempfile name = $tempfile"
4106 9&nbsp;# tempfile name = filename.QA2ZpY
4107 10&nbsp;# or something similar...
4108 11&nbsp;
4109 12&nbsp;# Creates a file of that name in the current working directory
4110 13&nbsp;#+ with 600 file permissions.
4111 14&nbsp;# A "umask 177" is therefore unnecessary,
4112 15&nbsp;#+ but it's good programming practice nevertheless.</PRE
4113 ></TD
4114 ></TR
4115 ></TABLE
4116 ></P
4117 ></DD
4118 ><DT
4119 ><A
4120 NAME="MAKEREF"
4121 ></A
4122 ><B
4123 CLASS="COMMAND"
4124 >make</B
4125 ></DT
4126 ><DD
4127 ><P
4128 ><A
4129 NAME="MAKEFILEREF"
4130 ></A
4131 ></P
4132 ><P
4133 >Utility for building and compiling binary packages.
4134 This can also be used for any set of operations triggered
4135 by incremental changes in source files.</P
4136 ><P
4137 >The <I
4138 CLASS="FIRSTTERM"
4139 >make</I
4140 > command checks a
4141 <TT
4142 CLASS="FILENAME"
4143 >Makefile</TT
4144 >, a list of file dependencies and
4145 operations to be carried out.</P
4146 ><P
4147 >The <I
4148 CLASS="FIRSTTERM"
4149 >make</I
4150 > utility is, in effect,
4151 a powerful scripting language similar in many ways to
4152 <I
4153 CLASS="FIRSTTERM"
4154 >Bash</I
4155 >, but with the capability of
4156 recognizing <I
4157 CLASS="FIRSTTERM"
4158 >dependencies</I
4159 >. For in-depth
4160 coverage of this useful tool set, see the <A
4161 HREF="http://www.gnu.org/manual/manual.html"
4162 TARGET="_top"
4163 >GNU software
4164 documentation site</A
4165 >.</P
4166 ></DD
4167 ><DT
4168 ><A
4169 NAME="INSTALLREF"
4170 ></A
4171 ><B
4172 CLASS="COMMAND"
4173 >install</B
4174 ></DT
4175 ><DD
4176 ><P
4177 >Special purpose file copying command, similar to
4178 <A
4179 HREF="external.html#CPREF"
4180 >cp</A
4181 >, but capable of
4182 setting permissions and attributes of the copied
4183 files. This command seems tailormade for installing
4184 software packages, and as such it shows up frequently in
4185 <TT
4186 CLASS="FILENAME"
4187 >Makefiles</TT
4188 > (in the <TT
4189 CLASS="REPLACEABLE"
4190 ><I
4191 >make
4192 install :</I
4193 ></TT
4194 > section). It could likewise prove
4195 useful in installation scripts.</P
4196 ></DD
4197 ><DT
4198 ><A
4199 NAME="DOS2UNIXREF"
4200 ></A
4201 ><B
4202 CLASS="COMMAND"
4203 >dos2unix</B
4204 ></DT
4205 ><DD
4206 ><P
4207 >This utility, written by Benjamin Lin and collaborators,
4208 converts DOS-formatted text files (lines terminated by
4209 CR-LF) to UNIX format (lines terminated by LF only),
4210 and <A
4211 HREF="gotchas.html#DOSNEWLINES"
4212 >vice-versa</A
4213 >.</P
4214 ></DD
4215 ><DT
4216 ><A
4217 NAME="PTXREF"
4218 ></A
4219 ><B
4220 CLASS="COMMAND"
4221 >ptx</B
4222 ></DT
4223 ><DD
4224 ><P
4225 >The <B
4226 CLASS="COMMAND"
4227 >ptx [targetfile]</B
4228 > command
4229 outputs a permuted index (cross-reference list) of the
4230 targetfile. This may be further filtered and formatted in a
4231 pipe, if necessary.</P
4232 ></DD
4233 ><DT
4234 ><A
4235 NAME="MOREREF"
4236 ></A
4237 ><B
4238 CLASS="COMMAND"
4239 >more</B
4240 >, <A
4241 NAME="LESSREF"
4242 ></A
4243 ><B
4244 CLASS="COMMAND"
4245 >less</B
4246 ></DT
4247 ><DD
4248 ><P
4249 >Pagers that display a text file or stream to
4250 <TT
4251 CLASS="FILENAME"
4252 >stdout</TT
4253 >, one screenful at a time.
4254 These may be used to filter the output of
4255 <TT
4256 CLASS="FILENAME"
4257 >stdout</TT
4258 > . . . or of a script.</P
4259 ><P
4260 > An interesting application of <I
4261 CLASS="FIRSTTERM"
4262 >more</I
4263 >
4264 is to <SPAN
4265 CLASS="QUOTE"
4266 >"test drive"</SPAN
4267 > a command sequence,
4268 to forestall potentially unpleasant consequences.
4269 <TABLE
4270 BORDER="0"
4271 BGCOLOR="#E0E0E0"
4272 WIDTH="90%"
4273 ><TR
4274 ><TD
4275 ><PRE
4276 CLASS="PROGRAMLISTING"
4277 > 1&nbsp;ls /home/bozo | awk '{print "rm -rf " $1}' | more
4278 2&nbsp;# ^^^^
4279 3&nbsp;
4280 4&nbsp;# Testing the effect of the following (disastrous) command-line:
4281 5&nbsp;# ls /home/bozo | awk '{print "rm -rf " $1}' | sh
4282 6&nbsp;# Hand off to the shell to execute . . . ^^</PRE
4283 ></TD
4284 ></TR
4285 ></TABLE
4286 >
4287 </P
4288 ><P
4289 >The <I
4290 CLASS="FIRSTTERM"
4291 >less</I
4292 > pager has the
4293 interesting property of doing a formatted display of
4294 <I
4295 CLASS="FIRSTTERM"
4296 >man page</I
4297 > source. See <A
4298 HREF="contributed-scripts.html#MANED"
4299 >Example A-39</A
4300 >.</P
4301 ></DD
4302 ></DL
4303 ></DIV
4304 ></DIV
4305 ><H3
4306 CLASS="FOOTNOTES"
4307 >Notes</H3
4308 ><TABLE
4309 BORDER="0"
4310 CLASS="FOOTNOTES"
4311 WIDTH="100%"
4312 ><TR
4313 ><TD
4314 ALIGN="LEFT"
4315 VALIGN="TOP"
4316 WIDTH="5%"
4317 ><A
4318 NAME="FTN.AEN11885"
4319 HREF="filearchiv.html#AEN11885"
4320 >[1]</A
4321 ></TD
4322 ><TD
4323 ALIGN="LEFT"
4324 VALIGN="TOP"
4325 WIDTH="95%"
4326 ><P
4327 >An <I
4328 CLASS="FIRSTTERM"
4329 >archive</I
4330 >,
4331 in the sense discussed here, is simply a set of related
4332 files stored in a single location.</P
4333 ></TD
4334 ></TR
4335 ><TR
4336 ><TD
4337 ALIGN="LEFT"
4338 VALIGN="TOP"
4339 WIDTH="5%"
4340 ><A
4341 NAME="FTN.AEN11896"
4342 HREF="filearchiv.html#AEN11896"
4343 >[2]</A
4344 ></TD
4345 ><TD
4346 ALIGN="LEFT"
4347 VALIGN="TOP"
4348 WIDTH="95%"
4349 ><P
4350 > A <TT
4351 CLASS="REPLACEABLE"
4352 ><I
4353 >tar czvf ArchiveName.tar.gz *</I
4354 ></TT
4355 >
4356 <SPAN
4357 CLASS="emphasis"
4358 ><I
4359 CLASS="EMPHASIS"
4360 >will</I
4361 ></SPAN
4362 > include dotfiles in
4363 subdirectories <SPAN
4364 CLASS="emphasis"
4365 ><I
4366 CLASS="EMPHASIS"
4367 >below</I
4368 ></SPAN
4369 > the current
4370 working directory. This is an undocumented GNU
4371 <B
4372 CLASS="COMMAND"
4373 >tar</B
4374 > <SPAN
4375 CLASS="QUOTE"
4376 >"feature."</SPAN
4377 >
4378 </P
4379 ></TD
4380 ></TR
4381 ><TR
4382 ><TD
4383 ALIGN="LEFT"
4384 VALIGN="TOP"
4385 WIDTH="5%"
4386 ><A
4387 NAME="FTN.AEN12840"
4388 HREF="filearchiv.html#AEN12840"
4389 >[3]</A
4390 ></TD
4391 ><TD
4392 ALIGN="LEFT"
4393 VALIGN="TOP"
4394 WIDTH="95%"
4395 ><P
4396 >The checksum may be expressed as a
4397 <I
4398 CLASS="FIRSTTERM"
4399 >hexadecimal</I
4400 > number, or to some
4401 other base.</P
4402 ></TD
4403 ></TR
4404 ><TR
4405 ><TD
4406 ALIGN="LEFT"
4407 VALIGN="TOP"
4408 WIDTH="5%"
4409 ><A
4410 NAME="FTN.AEN12849"
4411 HREF="filearchiv.html#AEN12849"
4412 >[4]</A
4413 ></TD
4414 ><TD
4415 ALIGN="LEFT"
4416 VALIGN="TOP"
4417 WIDTH="95%"
4418 ><P
4419 >For even <SPAN
4420 CLASS="emphasis"
4421 ><I
4422 CLASS="EMPHASIS"
4423 >better</I
4424 ></SPAN
4425 >
4426 security, use the <I
4427 CLASS="FIRSTTERM"
4428 >sha256sum</I
4429 >,
4430 <I
4431 CLASS="FIRSTTERM"
4432 >sha512</I
4433 >, and
4434 <I
4435 CLASS="FIRSTTERM"
4436 >sha1pass</I
4437 >
4438 commands.</P
4439 ></TD
4440 ></TR
4441 ><TR
4442 ><TD
4443 ALIGN="LEFT"
4444 VALIGN="TOP"
4445 WIDTH="5%"
4446 ><A
4447 NAME="FTN.AEN12969"
4448 HREF="filearchiv.html#AEN12969"
4449 >[5]</A
4450 ></TD
4451 ><TD
4452 ALIGN="LEFT"
4453 VALIGN="TOP"
4454 WIDTH="95%"
4455 ><P
4456 >This is a symmetric block cipher, used to
4457 encrypt files on a single system or local network,
4458 as opposed to the <I
4459 CLASS="FIRSTTERM"
4460 >public key</I
4461 >
4462 cipher class, of which <I
4463 CLASS="FIRSTTERM"
4464 >pgp</I
4465 > is a
4466 well-known example.</P
4467 ></TD
4468 ></TR
4469 ><TR
4470 ><TD
4471 ALIGN="LEFT"
4472 VALIGN="TOP"
4473 WIDTH="5%"
4474 ><A
4475 NAME="FTN.AEN13030"
4476 HREF="filearchiv.html#AEN13030"
4477 >[6]</A
4478 ></TD
4479 ><TD
4480 ALIGN="LEFT"
4481 VALIGN="TOP"
4482 WIDTH="95%"
4483 ><P
4484 >Creates a temporary
4485 <I
4486 CLASS="FIRSTTERM"
4487 >directory</I
4488 > when invoked with the
4489 <TT
4490 CLASS="OPTION"
4491 >-d</TT
4492 > option.</P
4493 ></TD
4494 ></TR
4495 ></TABLE
4496 ><DIV
4497 CLASS="NAVFOOTER"
4498 ><HR
4499 ALIGN="LEFT"
4500 WIDTH="100%"><TABLE
4501 SUMMARY="Footer navigation table"
4502 WIDTH="100%"
4503 BORDER="0"
4504 CELLPADDING="0"
4505 CELLSPACING="0"
4506 ><TR
4507 ><TD
4508 WIDTH="33%"
4509 ALIGN="left"
4510 VALIGN="top"
4511 ><A
4512 HREF="textproc.html"
4513 ACCESSKEY="P"
4514 >Prev</A
4515 ></TD
4516 ><TD
4517 WIDTH="34%"
4518 ALIGN="center"
4519 VALIGN="top"
4520 ><A
4521 HREF="index.html"
4522 ACCESSKEY="H"
4523 >Home</A
4524 ></TD
4525 ><TD
4526 WIDTH="33%"
4527 ALIGN="right"
4528 VALIGN="top"
4529 ><A
4530 HREF="communications.html"
4531 ACCESSKEY="N"
4532 >Next</A
4533 ></TD
4534 ></TR
4535 ><TR
4536 ><TD
4537 WIDTH="33%"
4538 ALIGN="left"
4539 VALIGN="top"
4540 >Text Processing Commands</TD
4541 ><TD
4542 WIDTH="34%"
4543 ALIGN="center"
4544 VALIGN="top"
4545 ><A
4546 HREF="external.html"
4547 ACCESSKEY="U"
4548 >Up</A
4549 ></TD
4550 ><TD
4551 WIDTH="33%"
4552 ALIGN="right"
4553 VALIGN="top"
4554 >Communications Commands</TD
4555 ></TR
4556 ></TABLE
4557 ></DIV
4558 ></BODY
4559 ></HTML
4560 >