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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>External Filters, Programs and Commands</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Commands"
HREF="part4.html"><LINK
REL="PREVIOUS"
TITLE="Internal Commands and Builtins"
HREF="internal.html"><LINK
REL="NEXT"
TITLE="Complex Commands"
HREF="moreadv.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="internal.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="moreadv.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="EXTERNAL"
></A
>Chapter 16. External Filters, Programs and Commands</H1
><P
><A
NAME="EXTERNALREF"
></A
></P
><P
>Standard UNIX commands make shell scripts more versatile. The
power of scripts comes from coupling system commands and shell
directives with simple programming constructs.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="BASIC"
></A
>16.1. Basic Commands</H1
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="BASICCOMMANDS1"
></A
>The first commands a novice learns</B
></P
><DL
><DT
><A
NAME="LSREF"
></A
><B
CLASS="COMMAND"
>ls</B
></DT
><DD
><P
>The basic file <SPAN
CLASS="QUOTE"
>"list"</SPAN
> command. It is all too easy
to underestimate the power of this humble command. For
example, using the <TT
CLASS="OPTION"
>-R</TT
>, recursive option,
<B
CLASS="COMMAND"
>ls</B
> provides a tree-like listing of
a directory structure. Other useful options are
<TT
CLASS="OPTION"
>-S</TT
>, sort listing by file size,
<TT
CLASS="OPTION"
>-t</TT
>, sort by file modification time,
<TT
CLASS="OPTION"
>-v</TT
>, sort by (numerical) version numbers
embedded in the filenames,
<A
NAME="AEN10025"
HREF="#FTN.AEN10025"
>[1]</A
>
<TT
CLASS="OPTION"
>-b</TT
>, show escape characters, and
<TT
CLASS="OPTION"
>-i</TT
>, show file inodes (see <A
HREF="moreadv.html#IDELETE"
>Example 16-4</A
>).</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls -l</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls -lv</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
> total 0
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt
-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt</TT
></PRE
></TD
></TR
></TABLE
></P
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
> The <I
CLASS="FIRSTTERM"
>ls</I
> command returns a
non-zero <A
HREF="exit-status.html#EXITSTATUSREF"
>exit status</A
> when
attempting to list a non-existent file.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls abc</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>ls: abc: No such file or directory</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo $?</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>2</TT
></PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX40"
></A
><P
><B
>Example 16-1. Using <I
CLASS="FIRSTTERM"
>ls</I
> to create a table of contents
for burning a <SPAN
CLASS="ABBREV"
>CDR</SPAN
> disk</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # ex40.sh (burn-cd.sh)
3 # Script to automate burning a CDR.
4
5
6 SPEED=10 # May use higher speed if your hardware supports it.
7 IMAGEFILE=cdimage.iso
8 CONTENTSFILE=contents
9 # DEVICE=/dev/cdrom For older versions of cdrecord
10 DEVICE="1,0,0"
11 DEFAULTDIR=/opt # This is the directory containing the data to be burned.
12 # Make sure it exists.
13 # Exercise: Add a test for this.
14
15 # Uses Joerg Schilling's "cdrecord" package:
16 # http://www.fokus.fhg.de/usr/schilling/cdrecord.html
17
18 # If this script invoked as an ordinary user, may need to suid cdrecord
19 #+ chmod u+s /usr/bin/cdrecord, as root.
20 # Of course, this creates a security hole, though a relatively minor one.
21
22 if [ -z "$1" ]
23 then
24 IMAGE_DIRECTORY=$DEFAULTDIR
25 # Default directory, if not specified on command-line.
26 else
27 IMAGE_DIRECTORY=$1
28 fi
29
30 # Create a "table of contents" file.
31 ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE
32 # The "l" option gives a "long" file listing.
33 # The "R" option makes the listing recursive.
34 # The "F" option marks the file types (directories get a trailing /).
35 echo "Creating table of contents."
36
37 # Create an image file preparatory to burning it onto the CDR.
38 mkisofs -r -o $IMAGEFILE $IMAGE_DIRECTORY
39 echo "Creating ISO9660 file system image ($IMAGEFILE)."
40
41 # Burn the CDR.
42 echo "Burning the disk."
43 echo "Please be patient, this will take a while."
44 wodim -v -isosize dev=$DEVICE $IMAGEFILE
45 # In newer Linux distros, the "wodim" utility assumes the
46 #+ functionality of "cdrecord."
47 exitcode=$?
48 echo "Exit code = $exitcode"
49
50 exit $exitcode</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="CATREF"
></A
><B
CLASS="COMMAND"
>cat</B
>, <B
CLASS="COMMAND"
>tac</B
></DT
><DD
><P
><B
CLASS="COMMAND"
>cat</B
>, an acronym for
<I
CLASS="WORDASWORD"
>concatenate</I
>,
lists a file to <TT
CLASS="FILENAME"
>stdout</TT
>. When
combined with redirection (<SPAN
CLASS="TOKEN"
>></SPAN
> or
<SPAN
CLASS="TOKEN"
>>></SPAN
>), it is commonly used to concatenate
files.
<A
NAME="CATUSES"
></A
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Uses of 'cat'
2 cat filename # Lists the file.
3
4 cat file.1 file.2 file.3 > file.123 # Combines three files into one.</PRE
></TD
></TR
></TABLE
>
The <TT
CLASS="OPTION"
>-n</TT
> option to <B
CLASS="COMMAND"
>cat</B
>
inserts consecutive numbers before all lines of the
target file(s). The <TT
CLASS="OPTION"
>-b</TT
> option numbers
only the non-blank lines. The <TT
CLASS="OPTION"
>-v</TT
> option
echoes nonprintable characters, using <SPAN
CLASS="TOKEN"
>^</SPAN
>
notation. The <TT
CLASS="OPTION"
>-s</TT
> option squeezes multiple
consecutive blank lines into a single blank line.</P
><P
>See also <A
HREF="textproc.html#LNUM"
>Example 16-28</A
> and <A
HREF="textproc.html#ROT13"
>Example 16-24</A
>.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="CATLESSEFF"
></A
>
In a <A
HREF="special-chars.html#PIPEREF"
>pipe</A
>, it may be
more efficient to <A
HREF="io-redirection.html#IOREDIRREF"
>redirect</A
>
the <TT
CLASS="FILENAME"
>stdin</TT
> to a file, rather than to
<B
CLASS="COMMAND"
>cat</B
> the file.
</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat filename | tr a-z A-Z
2
3 tr a-z A-Z < filename # Same effect, but starts one less process,
4 #+ and also dispenses with the pipe.</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><P
><B
CLASS="COMMAND"
>tac</B
>, is the inverse of
<I
CLASS="WORDASWORD"
>cat</I
>, listing a file backwards from its end.</P
></DD
><DT
><A
NAME="REVREF"
></A
><B
CLASS="COMMAND"
>rev</B
></DT
><DD
><P
>reverses each line of a file, and outputs to
<TT
CLASS="FILENAME"
>stdout</TT
>. This does not have the same effect
as <B
CLASS="COMMAND"
>tac</B
>, as it preserves the order of
the lines, but flips each one around (mirror image).</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat file1.txt</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>This is line 1.
This is line 2.</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>tac file1.txt</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>This is line 2.
This is line 1.</TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>rev file1.txt</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>.1 enil si sihT
.2 enil si sihT</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></DD
><DT
><A
NAME="CPREF"
></A
><B
CLASS="COMMAND"
>cp</B
></DT
><DD
><P
>This is the file copy command. <TT
CLASS="USERINPUT"
><B
>cp file1
file2</B
></TT
> copies <TT
CLASS="FILENAME"
>file1</TT
>
to <TT
CLASS="FILENAME"
>file2</TT
>, overwriting
<TT
CLASS="FILENAME"
>file2</TT
> if it already exists (see <A
HREF="moreadv.html#EX42"
>Example 16-6</A
>).</P
><DIV
CLASS="TIP"
><TABLE
CLASS="TIP"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/tip.png"
HSPACE="5"
ALT="Tip"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>Particularly useful are the <TT
CLASS="OPTION"
>-a</TT
>
archive flag (for copying an entire directory tree),
the <TT
CLASS="OPTION"
>-u</TT
> update flag (which prevents
overwriting identically-named newer files), and the
<TT
CLASS="OPTION"
>-r</TT
> and <TT
CLASS="OPTION"
>-R</TT
> recursive
flags.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cp -u source_dir/* dest_dir
2 # "Synchronize" dest_dir to source_dir
3 #+ by copying over all newer and not previously existing files.</PRE
></TD
></TR
></TABLE
></P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><A
NAME="MVREF"
></A
><B
CLASS="COMMAND"
>mv</B
></DT
><DD
><P
>This is the file <I
CLASS="FIRSTTERM"
>move</I
> command.
It is equivalent to a combination of <B
CLASS="COMMAND"
>cp</B
>
and <B
CLASS="COMMAND"
>rm</B
>. It may be used to move multiple
files to a directory, or even to rename a directory. For
some examples of using <B
CLASS="COMMAND"
>mv</B
> in a script,
see <A
HREF="parameter-substitution.html#RFE"
>Example 10-11</A
> and <A
HREF="contributed-scripts.html#RN"
>Example A-2</A
>.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>When used in a non-interactive script,
<B
CLASS="COMMAND"
>mv</B
> takes the <TT
CLASS="OPTION"
>-f</TT
>
(<I
CLASS="FIRSTTERM"
>force</I
>) option to bypass user
input.</P
><P
>When a directory is moved to a preexisting directory,
it becomes a subdirectory of the destination directory.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>mv source_directory target_directory</B
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>ls -lF target_directory</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>total 1
drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><A
NAME="RMREF"
></A
><B
CLASS="COMMAND"
>rm</B
></DT
><DD
><P
>Delete (remove) a file or files. The <TT
CLASS="OPTION"
>-f</TT
>
option forces removal of even readonly files, and is useful
for bypassing user input in a script.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="DASHREM"
></A
></P
><P
>The <I
CLASS="FIRSTTERM"
>rm</I
> command will, by
itself, fail to remove filenames beginning with
a dash. Why? Because <I
CLASS="FIRSTTERM"
>rm</I
>
sees a dash-prefixed filename as an
<I
CLASS="FIRSTTERM"
>option</I
>.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>rm -badname</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>rm: invalid option -- b
Try `rm --help' for more information.</TT
></PRE
></TD
></TR
></TABLE
>
</P
><P
> One clever workaround is to precede
the filename with a <SPAN
CLASS="QUOTE"
>" -- "</SPAN
> (the
<I
CLASS="FIRSTTERM"
>end-of-options</I
> flag).
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>rm -- -badname</B
></TT
></PRE
></TD
></TR
></TABLE
>
</P
><P
> Another method to is to preface the filename to be removed
with a <TT
CLASS="FILENAME"
>dot-slash</TT
> .
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>rm ./-badname</B
></TT
></PRE
></TD
></TR
></TABLE
>
</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="WARNING"
><TABLE
CLASS="WARNING"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/warning.png"
HSPACE="5"
ALT="Warning"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="RMRECURS"
></A
>When used with the
recursive flag <TT
CLASS="OPTION"
>-r</TT
>, this command removes
files all the way down the directory tree from the current
directory. A careless <B
CLASS="COMMAND"
>rm -rf *</B
> can wipe
out a big chunk of a directory structure.</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><A
NAME="RMDIRREF"
></A
><B
CLASS="COMMAND"
>rmdir</B
></DT
><DD
><P
>Remove directory. The directory must be empty of
all files -- including <SPAN
CLASS="QUOTE"
>"invisible"</SPAN
>
<I
CLASS="FIRSTTERM"
>dotfiles</I
>
<A
NAME="AEN10228"
HREF="#FTN.AEN10228"
>[2]</A
>
-- for this command to succeed.</P
></DD
><DT
><A
NAME="MKDIRREF"
></A
><B
CLASS="COMMAND"
>mkdir</B
></DT
><DD
><P
>Make directory, creates a new directory. For example,
<TT
CLASS="USERINPUT"
><B
>mkdir -p project/programs/December</B
></TT
>
creates the named directory. The
<TT
CLASS="REPLACEABLE"
><I
>-p</I
></TT
> option automatically creates
any necessary parent directories.</P
></DD
><DT
><A
NAME="CHMODREF"
></A
><B
CLASS="COMMAND"
>chmod</B
></DT
><DD
><P
>Changes the attributes of an existing file or directory
(see <A
HREF="internal.html#EX44"
>Example 15-14</A
>).</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 chmod +x filename
2 # Makes "filename" executable for all users.
3
4 chmod u+s filename
5 # Sets "suid" bit on "filename" permissions.
6 # An ordinary user may execute "filename" with same privileges as the file's owner.
7 # (This does not apply to shell scripts.)</PRE
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 chmod 644 filename
2 # Makes "filename" readable/writable to owner, readable to others
3 #+ (octal mode).
4
5 chmod 444 filename
6 # Makes "filename" read-only for all.
7 # Modifying the file (for example, with a text editor)
8 #+ not allowed for a user who does not own the file (except for root),
9 #+ and even the file owner must force a file-save
10 #+ if she modifies the file.
11 # Same restrictions apply for deleting the file.</PRE
></TD
></TR
></TABLE
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 chmod 1777 directory-name
2 # Gives everyone read, write, and execute permission in directory,
3 #+ however also sets the "sticky bit".
4 # This means that only the owner of the directory,
5 #+ owner of the file, and, of course, root
6 #+ can delete any particular file in that directory.
7
8 chmod 111 directory-name
9 # Gives everyone execute-only permission in a directory.
10 # This means that you can execute and READ the files in that directory
11 #+ (execute permission necessarily includes read permission
12 #+ because you can't execute a file without being able to read it).
13 # But you can't list the files or search for them with the "find" command.
14 # These restrictions do not apply to root.
15
16 chmod 000 directory-name
17 # No permissions at all for that directory.
18 # Can't read, write, or execute files in it.
19 # Can't even list files in it or "cd" to it.
20 # But, you can rename (mv) the directory
21 #+ or delete it (rmdir) if it is empty.
22 # You can even symlink to files in the directory,
23 #+ but you can't read, write, or execute the symlinks.
24 # These restrictions do not apply to root.</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><A
NAME="CHATTRREF"
></A
><B
CLASS="COMMAND"
>chattr</B
></DT
><DD
><P
><B
CLASS="COMMAND"
>Ch</B
>ange file
<B
CLASS="COMMAND"
>attr</B
>ibutes. This is analogous to
<B
CLASS="COMMAND"
>chmod</B
> above, but with different options
and a different invocation syntax, and it works only on
<I
CLASS="FIRSTTERM"
>ext2/ext3</I
> filesystems.</P
><P
>One particularly interesting <B
CLASS="COMMAND"
>chattr</B
>
option is <TT
CLASS="OPTION"
>i</TT
>. A <B
CLASS="COMMAND"
>chattr +i
<TT
CLASS="FILENAME"
>filename</TT
></B
> marks the file
as immutable. The file cannot be modified, linked to, or
deleted, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not even by root</I
></SPAN
>. This
file attribute can be set or removed only by
<I
CLASS="FIRSTTERM"
>root</I
>. In a similar fashion,
the <TT
CLASS="OPTION"
>a</TT
> option marks the file as append
only.</P
><P
> <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>root# </TT
><TT
CLASS="USERINPUT"
><B
>chattr +i file1.txt</B
></TT
>
<TT
CLASS="PROMPT"
>root# </TT
><TT
CLASS="USERINPUT"
><B
>rm file1.txt</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>rm: remove write-protected regular file `file1.txt'? y
rm: cannot remove `file1.txt': Operation not permitted</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
>If a file has the <TT
CLASS="OPTION"
>s</TT
> (secure)
attribute set, then when it is deleted its block is
overwritten with binary zeroes.
<A
NAME="AEN10301"
HREF="#FTN.AEN10301"
>[3]</A
>
</P
><P
>If a file has the <TT
CLASS="OPTION"
>u</TT
> (undelete)
attribute set, then when it is deleted, its contents can still
be retrieved (undeleted).</P
><P
>If a file has the <TT
CLASS="OPTION"
>c</TT
> (compress)
attribute set, then it will automatically be compressed
on writes to disk, and uncompressed on reads.</P
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The file attributes set with
<B
CLASS="COMMAND"
>chattr</B
> do not show in a file listing
(<B
CLASS="COMMAND"
>ls -l</B
>).</P
></TD
></TR
></TABLE
></DIV
></DD
><DT
><A
NAME="LINKREF"
></A
><B
CLASS="COMMAND"
>ln</B
></DT
><DD
><P
>Creates links to pre-existings files. A <SPAN
CLASS="QUOTE"
>"link"</SPAN
>
is a reference to a file, an alternate name for it.
The <B
CLASS="COMMAND"
>ln</B
> command permits referencing
the linked file by more than one name and is a superior
alternative to aliasing (see <A
HREF="othertypesv.html#EX18"
>Example 4-6</A
>).</P
><P
>The <B
CLASS="COMMAND"
>ln</B
> creates only a reference, a
pointer to the file only a few bytes in size.</P
><P
><A
NAME="SYMLINKREF"
></A
></P
><P
>The <B
CLASS="COMMAND"
>ln</B
> command is most often used
with the <TT
CLASS="OPTION"
>-s</TT
>, symbolic or
<SPAN
CLASS="QUOTE"
>"soft"</SPAN
> link flag. Advantages of using the
<TT
CLASS="OPTION"
>-s</TT
> flag are that it permits linking across
file systems or to directories.</P
><P
>The syntax of the command is a bit tricky. For example:
<TT
CLASS="USERINPUT"
><B
>ln -s oldfile newfile</B
></TT
> links the
previously existing <TT
CLASS="FILENAME"
>oldfile</TT
> to the
newly created link, <TT
CLASS="FILENAME"
>newfile</TT
>.</P
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>If a file named <TT
CLASS="FILENAME"
>newfile</TT
> has
previously existed, an error message will
result.</P
></TD
></TR
></TABLE
></DIV
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN10336"
></A
><P
><B
>Which type of link to use?</B
></P
><P
>As John Macdonald explains it:</P
><P
>Both of these [types of links] provide a certain measure of dual reference
-- if you edit the contents of the file using any name,
your changes will affect both the original name and either
a hard or soft new name. The differences between them
occurs when you work at a higher level. The advantage of
a hard link is that the new name is totally independent
of the old name -- if you remove or rename the old name,
that does not affect the hard link, which continues
to point to the data while it would leave a soft link
hanging pointing to the old name which is no longer
there. The advantage of a soft link is that it can refer
to a different file system (since it is just a reference
to a file name, not to actual data). And, unlike a hard
link, a symbolic link can refer to a directory.</P
></DIV
></TD
></TR
></TABLE
><P
><A
NAME="LINKMINVOK"
></A
></P
><P
>Links give the ability to invoke a script (or any other type
of executable) with multiple names, and having that script
behave according to how it was invoked.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="HELLOL"
></A
><P
><B
>Example 16-2. Hello or Good-bye</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # hello.sh: Saying "hello" or "goodbye"
3 #+ depending on how script is invoked.
4
5 # Make a link in current working directory ($PWD) to this script:
6 # ln -s hello.sh goodbye
7 # Now, try invoking this script both ways:
8 # ./hello.sh
9 # ./goodbye
10
11
12 HELLO_CALL=65
13 GOODBYE_CALL=66
14
15 if [ $0 = "./goodbye" ]
16 then
17 echo "Good-bye!"
18 # Some other goodbye-type commands, as appropriate.
19 exit $GOODBYE_CALL
20 fi
21
22 echo "Hello!"
23 # Some other hello-type commands, as appropriate.
24 exit $HELLO_CALL</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="MANREF"
></A
><B
CLASS="COMMAND"
>man</B
>, <A
NAME="INFOREF"
></A
><B
CLASS="COMMAND"
>info</B
></DT
><DD
><P
>These commands access the manual and information pages on
system commands and installed utilities. When available, the
<I
CLASS="FIRSTTERM"
>info</I
> pages usually contain more detailed
descriptions than do the <I
CLASS="FIRSTTERM"
>man</I
> pages.</P
><P
>There have been various attempts at
<SPAN
CLASS="QUOTE"
>"automating"</SPAN
> the writing of <I
CLASS="FIRSTTERM"
>man
pages</I
>. For a script that makes a tentative first
step in that direction, see <A
HREF="contributed-scripts.html#MANED"
>Example A-39</A
>.</P
></DD
></DL
></DIV
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN10025"
HREF="external.html#AEN10025"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>The <TT
CLASS="OPTION"
>-v</TT
> option also orders the
sort by <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>upper- and lowercase prefixed</I
></SPAN
>
filenames.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN10228"
HREF="external.html#AEN10228"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><A
NAME="DOTFILESREF"
></A
></P
><P
><I
CLASS="FIRSTTERM"
>Dotfiles</I
> are files whose
names begin with a <I
CLASS="FIRSTTERM"
>dot</I
>, such as
<TT
CLASS="FILENAME"
>~/.Xdefaults</TT
>. Such filenames do
not appear in a normal <B
CLASS="COMMAND"
>ls</B
> listing
(although an <B
CLASS="COMMAND"
>ls -a</B
> will show
them), and they cannot be deleted by an accidental
<B
CLASS="COMMAND"
>rm -rf *</B
>. Dotfiles are generally
used as setup and configuration files in a user's
home directory.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN10301"
HREF="external.html#AEN10301"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>This particular feature may not yet be
implemented in the version of the ext2/ext3 filesystem
installed on your system. Check the documentation for
your Linux distro.</P
></TD
></TR
></TABLE
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="internal.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="moreadv.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Internal Commands and Builtins</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part4.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Complex Commands</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|