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 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890
|
<?xml version="1.0"?>
<article>
<articleinfo>
<title>DebianReference/Tutorial</title>
</articleinfo>
<para/>
<section>
<title>Do not use Edit(GUI) button.</title>
<para> </para>
<para>Copyright 2007, 2008 Osamu Aoki GPL, (Please agree to GPL, GPL2, and any version of GPL which is compatible with DSFG if you update any part of wiki page) </para>
<para>Generated HTML is at "<ulink url="http://people.debian.org/~osamu/pub/getwiki/html/ch02.en.html">Debian Reference: Chapter 2. GNU/Linux tutorials</ulink>". </para>
<para>I welcome your contributions to update this wiki page. You must follow these rules: </para>
<itemizedlist>
<listitem>
<para>Do not use Edit(GUI) button of <ulink url="/MoinMoin">MoinMoin</ulink>. </para>
</listitem>
<listitem>You can update anytime for: <itemizedlist><listitem>grammar errors </listitem><listitem>spelling errors </listitem><listitem>moved URL location </listitem><listitem>package name transition adjustment (emacs23 etc.) </listitem><listitem>clearly broken script. </listitem></itemizedlist></listitem>
<listitem>Before updating this wiki content: <itemizedlist><listitem><para>Read "<ulink url="http://wiki.debian.org/DebianReference/Test">Guide for contributing to Debian Reference</ulink>". </para></listitem></itemizedlist></listitem>
</itemizedlist>
<para/>
</section>
<section>
<title>GNU/Linux tutorials</title>
<para>I think learning a computer system is like learning a new foreign language. Although tutorial books and documentation are helpful, you have to practice it yourself. In order to help you get started smoothly, I will elaborate a few basic points. </para>
<para>The powerful design of <ulink url="http://www.debian.org">Debian</ulink> <ulink url="http://en.wikipedia.org/wiki/GNU">GNU</ulink>/<ulink url="http://en.wikipedia.org/wiki/Linux">Linux</ulink> comes from the <ulink url="http://en.wikipedia.org/wiki/Unix">Unix</ulink> operating system, i.e., a <ulink url="http://en.wikipedia.org/wiki/Multi-user">multiuser</ulink>, <ulink url="http://en.wikipedia.org/wiki/Computer_multitasking">multitasking</ulink> operating system. You must learn to take advantage of the power of these features and the similarities between Unix and GNU/Linux. </para>
<para>Don't shy away from Unix oriented texts and don't rely solely on GNU/Linux texts, as this will rob you of much useful information. </para>
<para>"<ulink url="http://packages.debian.org/search?keywords=rutebook">Rute User's Tutorial and Exposition</ulink>", in the Debian non-free archive as <code>rutebook</code> package (popcon: @@@pop-rutebook@@@), provides a good online resource to the generic system administration. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> If you have been using any <ulink url="http://en.wikipedia.org/wiki/Unix-like">Unix-like</ulink> system for a while with command line tools, you probably know everything I explain here. Please use this as a reality check and refresher. </para>
<para/>
<section>
<title>Console basics</title>
<para/>
<section>
<title>The shell prompt</title>
<para>Upon starting the system, you are presented with the character based login screen if you did not install X Window System with the display manager such as <code>gdm</code>. Suppose your hostname is <code>foo</code>, the login prompt looks like: </para>
<para/>
<screen><![CDATA[foo login:
]]></screen>
<para>If you did install a GUI environment such as Gnome of KDE, then you can get to a prompt by Ctrl-Alt-F1, and you can return to the GUI screen via Alt-F7 (see Virtual Consoles below for more information). </para>
<para>Following what you selected during the installation process, you type your username, e.g. <code>penguin</code>, and press the Enter-key, then type your password and press the Enter-key again. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Following the Unix tradition, the username and password of the Debian system are case sensitive. The username is usually chosen only from the lowercase. </para>
<para>Now you are in the shell. The shell interprets your commands. The system starts with the greeting message stored in <code>/etc/motd</code> (Message Of The Day) and with the command prompt as: </para>
<para/>
<screen><![CDATA[Debian GNU/Linux lenny/sid foo tty1
foo login: penguin
Password:
Last login: Sun Apr 22 09:29:34 2007 on tty1
Linux snoopy 2.6.20-1-amd64 #1 SMP Sun Apr 15 20:25:49 UTC 2007 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
foo:~$
]]></screen>
<para>Here, the main part of the greeting message can be customized by editing the <code>/etc/motd.tail</code> file. The first line is generated from the system information using "<code>uname -snrvm</code>". </para>
<para/>
</section>
<section>
<title>The shell prompt under X</title>
<para>If you installed <ulink url="http://en.wikipedia.org/wiki/X_Window_System">X Window System</ulink> with a display manager such as Gnome's <code>gdm</code> by selecting "Desktop environment" task during the installation, you will be presented with the graphical login screen upon starting your system. You type your username and your password to login to the non-privileged user account. Use tab to navigate between username and password, or use the mouse and primary click. </para>
<para>You can gain the shell prompt under X by starting a <code>x-terminal-emulator</code> program such as <code>gnome-terminal</code>, <code>rxvt</code> or <code>xterm</code>. Under the Gnome Desktop environment, clicking "Applications" -> "Accessories" -> "Terminal" does the trick. </para>
<para/>
<para>You can also see the section below @{@virtualconsoles@}@. </para>
<para>Under some other Desktop systems (like <code>fluxbox</code>), there may be no obvious starting point for the menu. If this happens, just try (right) clicking the center of the screen and hope for a menu to pop-up. </para>
<para/>
</section>
<section>
<title>The root account</title>
<para>The root account is also called <ulink url="http://en.wikipedia.org/wiki/Superuser">superuser</ulink> or privileged user. From this account, you can perform the following system administration activities: </para>
<itemizedlist>
<listitem>read, write, and remove any files on the system irrespective of their file permissions </listitem>
<listitem>set file ownership and permission of any files on the system </listitem>
<listitem>set the password of any non-privileged users on the system </listitem>
<listitem>login to any accounts without their passwords </listitem>
</itemizedlist>
<para>This unlimited power of root account requires you to be considerate and responsible when using it. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The file permission of the file (including hardware devices such as CD-ROM etc. which are just another file for the Debian system) may render them unusable or inaccessible by non-root users. Although the use of root account is a quick way to test this kind of situation, the resolution of this situation should be done through proper setting of the access permission and the group. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/alert.png" depth="15"/></imageobject><textobject><phrase>/!\</phrase></textobject></inlinemediaobject> Never share the root password with others. </para>
<para/>
</section>
<section>
<title>The root shell prompt</title>
<para>Here are a few basic methods to gain the root shell prompt by using the root password: </para>
<itemizedlist>
<listitem>
<para>At the character based login prompt, you simply type <code>root</code>. </para>
</listitem>
<listitem>
<para>Under the Gnome Desktop environment, click "Applications" -> "Accessories" -> "Root Terminal". </para>
</listitem>
<listitem>
<para>From any user shell prompt, type "<code>su -l</code>". (This does not preserve the environment of the current user) </para>
</listitem>
<listitem>
<para>From any user shell prompt, type "<code>su</code>". (This preserves most of the environment of the current user) </para>
</listitem>
</itemizedlist>
<para/>
</section>
<section>
<title>GUI system administration tools</title>
<para>When your desktop menu does not start GUI system administration tools automatically with the appropriate privilege, you can start them from the root shell prompt of the X terminal emulator, such as <code>gnome-terminal</code>, <code>rxvt</code>, or <code>xterm</code>. See @{@therootshellprompt@}@. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/alert.png" depth="15"/></imageobject><textobject><phrase>/!\</phrase></textobject></inlinemediaobject> Never start the X server under the root account by typing in <code>root</code> to the prompt of the display manager such as <code>gdm</code> because it is considered unsafe (insecure), even when you plan to perform administrative activities. The entire X architecture is considered insecure if run as root. You must always use the lowest privilege level possible, like a regular user account. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/alert.png" depth="15"/></imageobject><textobject><phrase>/!\</phrase></textobject></inlinemediaobject> Never run untrusted remote GUI program under X window when critical information is displayed since it may eavesdrop your X screen. </para>
<para/>
<para/>
<para/>
<para/>
<para/>
</section>
<section>
<title>Virtual consoles</title>
<para>In the default Debian system, there are six switchable <ulink url="http://en.wikipedia.org/wiki/VT100">VT100-like</ulink> character consoles available to start the command shell directly on the Linux host. Unless you are in a GUI environment, you can switch between the virtual consoles by pressing the <code>Left-Alt-key</code> and one of the <code>F1</code>--<code>F6</code> keys simultaneously. Each character console allows independent login to the account and offers the multiuser environment. This multiuser environment is a great Unix feature, and very addictive. </para>
<para>If you are under the X Window System, you gain access to the character console 1 by pressing <code>Ctrl-Alt-F1</code> key, i.e., the <code>left-Ctrl-key</code>, the <code>left-Alt-key</code>, and the <code>F1-key</code> are pressed together. You can get back to the X Window System, normally running on the virtual console 7, by pressing <code>Alt-F7</code>. </para>
<para>You can alternatively change to another virtual console, e.g. to the console 1, by the command: </para>
<screen><![CDATA[# chvt 1
]]></screen>
<para/>
</section>
<section>
<title>How to leave the command prompt</title>
<para>You type <code>Ctrl-D</code>, i.e., the <code>left-Ctrl-key</code> and the <code>d-key</code> pressed together, at the command prompt to close the shell activity. If you are at the character console, you will return to the login prompt with this. Even though these control characters are referred as "control D" with the upper case, you do not need to press the Shift-key. The short hand expression, <code>^D</code>, is also used for <code>Ctrl-D</code>. Alternately, you can type "exit". </para>
<para>If you are at <code>x-terminal-emulator</code>, you can close <code>x-terminal-emulator</code> window with this. </para>
<para/>
</section>
<section>
<title>How to shutdown the system</title>
<para>Just like any other modern OS where the file operation involves <ulink url="http://en.wikipedia.org/wiki/Cache">caching data</ulink> in memory for improved performance, the Debian system needs the proper shutdown procedure before power can safely be turned off. This is to maintain the integrity of files, by forcing all changes in memory to be written to disk. If the software power control is available, the shutdown procedure automatically turns off power of the system. (Otherwise, you may have to press power button for few seconds after the shutdown procedure.) </para>
<para>Under the normal multiuser mode, use following from the root command prompt to shutdown the system: </para>
<screen><![CDATA[# shutdown -h now
]]></screen>
<para>Under the single-user mode, use following from the root command prompt to shutdown the system: </para>
<screen><![CDATA[# poweroff -i -f
]]></screen>
<para>Alternatively, you may type <code>Ctrl-Alt-Delete</code> (The <code>left-Ctrl-key</code>, the <code>left-Alt-Key</code>, and the <code>Delete</code> are pressed together) to shutdown if <code>/etc/inittab</code> contains "<code>ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -h now</code>" in it. See manpage for <code>inittab</code>(5) for details. </para>
<para/>
</section>
<section>
<title>Recovering a sane console</title>
<para>When the screen goes berserk after doing some funny things such as "<code>cat <some-binary-file></code>", type "<code>reset</code>" at the command prompt. You may not be able to see the command echoed as you type. You may also issue "<code>clear</code>" to clean up the screen. </para>
<para/>
</section>
<section>
<title>Additional package suggestions for the newbie</title>
<para>Although even the minimal installation of the Debian system without any desktop environment tasks provides the basic Unix functionality, it is a good idea to install few additional commandline and curses based character terminal packages such as <code>mc</code> and <code>vim</code> with <code>aptitude</code> for beginners to get started. From the shell prompt as root: </para>
<screen><![CDATA[# aptitude update
...
# aptitude install mc vim sudo
...
]]></screen>
<para/>
<para>If you already had these packages installed, nothing will be installed. </para>
<table>
<caption/>
<tgroup cols="4">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<colspec colname="xxx4"/>
<tbody>
<row>
<entry>
<para> List of interesting text-mode program packages. </para>
</entry>
<entry>
<para> 1 </para>
</entry>
<entry>
<para> 2 </para>
</entry>
<entry>
<para> 3 </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">package</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">popcon</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">size</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">description</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>mc</code>
</para>
</entry>
<entry>
<para> 5740 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> A text-mode full-screen file manager </para>
</entry>
</row>
<row>
<entry>
<para>
<code>vim</code>
</para>
</entry>
<entry>
<para> 15655 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Unix text editor Vi IMproved, a programmers text editor (standard version) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>vim-tiny</code>
</para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Unix text editor Vi IMproved, a programmers text editor (compact version) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>emacs21</code>
</para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> GNU project Emacs, the Lisp based extensible text editor (version 21) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>emacs22</code>
</para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> GNU project Emacs, the Lisp based extensible text editor (version 22) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>w3m</code>
</para>
</entry>
<entry>
<para> 6313 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Text-mode WWW browsers </para>
</entry>
</row>
<row>
<entry>
<para>
<code>gpm</code>
</para>
</entry>
<entry>
<para> 2542 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> The Unix style cut-and-paste on the text console (daemon) </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>It may be a good idea to read some informative documentations. </para>
<table>
<caption/>
<tgroup cols="4">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<colspec colname="xxx4"/>
<tbody>
<row>
<entry>
<para> List of informative documentation packages. </para>
</entry>
<entry>
<para> 1 </para>
</entry>
<entry>
<para> 2 </para>
</entry>
<entry>
<para> 3 </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">package</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">popcon</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">size</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">description</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>doc-debian</code>
</para>
</entry>
<entry>
<para> *42664 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Debian Project documentation, (Debian FAQ) and other documents </para>
</entry>
</row>
<row>
<entry>
<para>
<code>debian-policy</code>
</para>
</entry>
<entry>
<para> *2288 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Debian Policy Manual and related documents </para>
</entry>
</row>
<row>
<entry>
<para>
<code>developers-reference</code>
</para>
</entry>
<entry>
<para> *1058 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Guidelines and information for Debian developers </para>
</entry>
</row>
<row>
<entry>
<para>
<code>maint-guide</code>
</para>
</entry>
<entry>
<para> *896 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Debian New Maintainers' Guide </para>
</entry>
</row>
<row>
<entry>
<para>
<code>debian-history</code>
</para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> History of the Debian Project </para>
</entry>
</row>
<row>
<entry>
<para>
<code>debian-faq</code>
</para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Debian FAQ </para>
</entry>
</row>
<row>
<entry>
<para>
<code>doc-linux-text</code>
</para>
</entry>
<entry>
<para> *42187 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Linux HOWTOs and FAQ (text) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>doc-linux-html</code>
</para>
</entry>
<entry>
<para> 613 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Linux HOWTOs and FAQ (html) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>sysadmin-guide</code>
</para>
</entry>
<entry>
<para> *283 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> The Linux System Administrators' Guide </para>
</entry>
</row>
<row>
<entry>
<para>
<code>rutebook</code>
</para>
</entry>
<entry>
<para> 182 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Linux: Rute User's Tutorial and Exposition (non-free) </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>You can install some of these packages by issuing the following command from the root shell prompt: </para>
<screen><![CDATA[# aptitude install package_name
]]></screen>
<para/>
</section>
<section>
<title>An extra user account</title>
<para>If you do not want to use your main user account for the following training activities, you can create a training user account, e.g. <code>fish</code>. Type at root shell prompt: </para>
<screen><![CDATA[# adduser fish
]]></screen>
<itemizedlist>
<listitem>answer all the questions </listitem>
</itemizedlist>
<para>This will create a new account named as <code>fish</code>. After your practice, you can remove this user account and its home directory by: </para>
<screen><![CDATA[# deluser --remove-home fish
]]></screen>
<para/>
</section>
<section>
<title>sudo configuration</title>
<para>For the typical single user workstation such as the desktop Debian system on the laptop PC, it is common to deploy simple configuration of <code>sudo</code>(8) as follows to let the non-privileged user, e.g. <code>penguin</code>, to gain administrative privilege just with his user password (not with the root password). </para>
<para/>
<screen><![CDATA[# echo "penguin ALL=(ALL) ALL" >> /etc/sudoers
]]></screen>
<para>Since the <code>sudo</code>(8) command normally use normal user's environment variables to execute commands and normal user's "$PATH" variable is set to "<code>/usr/local/bin:/usr/bin:/bin:/usr/games</code>" by the system default, you should also adjust "$PATH" variable as: </para>
<para/>
<screen><![CDATA[$ echo 'PATH=$PATH:/usr/sbin:/sbin' >> ~/.bashrc
]]></screen>
<para>This trick should only be used for the single user workstation which you administer and where you are the only user. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/alert.png" depth="15"/></imageobject><textobject><phrase>/!\</phrase></textobject></inlinemediaobject> Do not set up accounts of regular users on multiuser workstation like this because it would be very bad for system security. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/attention.png" depth="15"/></imageobject><textobject><phrase><!></phrase></textobject></inlinemediaobject> The password and the account of the <code>penguin</code> in the above example requires as much protection as the root password and the root account. </para>
<para/>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/attention.png" depth="15"/></imageobject><textobject><phrase><!></phrase></textobject></inlinemediaobject> Administrative privilege in this context belongs to someone authorized to perform the system administration task on the workstation. Never give some manager in the Admin department of your company or your boss such privilege unless they are authorized and capable. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> For providing access privilege to limited devices and limited files, you should consider to use <emphasis role="strong">group</emphasis> to provide limited access instead of using the <code>root</code> privilege via <code>sudo</code>(8). </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> With more thoughtful and careful configuration, the <code>sudo</code> facility may provide means to grant limited administrative privileges to other users on a shared system without sharing the root password. This can help with accountability with hosts with multiple administrators so you can tell who did what. On the other hand, you might not want anyone else to have such privileges. </para>
<para/>
</section>
<section>
<title>Play time</title>
<para>Now you are ready to play with the Debian system without risks as long as you use the non-privileged user account. </para>
<para>This is because the Debian system is, even after the default installation, configured with the proper file permissions which prevent non-privileged users from damaging the system. Of course, there may still be some holes which can be exploited but those who worry about these issues should not be reading this section but should be reading <ulink url="http://www.debian.org/doc/manuals/securing-debian-howto/">Securing Debian Manual</ulink>. </para>
<para>We will review basic Unix filesystem theory first. Then we will learn the Debian system with the easy way using <emphasis role="strong">Midnight Commander (MC)</emphasis> and with the proper <emphasis role="strong">Unix-like</emphasis> ways. </para>
<para/>
</section>
</section>
<section>
<title>Unix-like filesystem</title>
<para>In GNU/Linux and other <ulink url="http://en.wikipedia.org/wiki/Unix-like">Unix-like</ulink> operating systems, <ulink url="http://en.wikipedia.org/wiki/Computer_file">files</ulink> are organized into <ulink url="http://en.wikipedia.org/wiki/Directory_(file_systems)">directories</ulink>. All files and directories are arranged in one big tree, the file hierarchy, rooted at <code>/</code>. It's called a tree because if you draw the file system, it looks like a tree (upside down). </para>
<para>These files and directories can be spread out over several devices. The <code>mount</code>(8) command serves to attach the file system found on some device to the big file tree. Conversely, the <code>umount</code>(8) command will detach it again. On recent Linux kernels, <code>mount</code>(8) operation can bind part of the file hierarchy somewhere else or can mount filesystem as shared, private, slave, or unbindable. Supported mount options for each filesystem are available in <code>/share/doc/linux-doc-2.6.*/Documentation/filesystems/</code>. </para>
<para><emphasis role="strong">Directories</emphasis> on Unix systems are called <emphasis role="strong">folders</emphasis> on some other systems. Please also note that there is no concept for <emphasis role="strong">drive</emphasis> such as <code>A:</code> on any Unix system. There is one file system, and everything is included. This is a huge advantage compared to Windows, so consider yourself lucky. </para>
<para/>
<section>
<title>Unix file basics</title>
<para>Here are Unix file basics: </para>
<itemizedlist>
<listitem>
<para>Filenames are <emphasis role="strong">case sensitive</emphasis>. That is, <code>MYFILE</code> and <code>MyFile</code> are different files. </para>
</listitem>
<listitem>
<para>The <emphasis role="strong">root directory</emphasis> is referred to as simply <code>/</code>. Don't confuse this with the root user or the home directory for the root user: <code>/root</code>. Note that both entities are commonly referred to as "root". The context of the usage should make it clear which is meant. </para>
</listitem>
<listitem>
<para>Every directory has a name which can contain any letters or symbols <emphasis role="strong">except <code>/</code></emphasis>. The root directory is an exception; its name is <code>/</code> (pronounced "slash" or "the root directory") and it cannot be renamed. </para>
</listitem>
<listitem>
<para>Each file or directory is designated by a <emphasis role="strong">fully-qualified filename</emphasis>, <emphasis role="strong">absolute filename</emphasis>, or <emphasis role="strong">path</emphasis>, giving the sequence of directories which must be passed through to reach it. The three terms are synonymous. </para>
</listitem>
<listitem>
<para>All <emphasis role="strong">fully-qualified filenames</emphasis> begin with the <code>/</code> directory, and there's a <code>/</code> between each directory or file in the filename. The first <code>/</code> is the top level directory, and the other <code>/</code>'s separate successive subdirectories, until we reach the last entry which is the name of the actual file. The words used here can be confusing. Take the following <emphasis role="strong">fully-qualified filename</emphasis> as an example: <code>/usr/share/keytables/us.map.gz</code>. However, people will also refer to its basename <code>us.map.gz</code> alone as a filename. </para>
</listitem>
<listitem>
<para>The root directory has a number of branches, such as <code>/etc/</code> and <code>/usr/</code>. These subdirectories in turn branch into still more subdirectories, such as <code>/etc/init.d/</code> and <code>/usr/local/</code>. The whole thing viewed collectively is called the <emphasis role="strong">directory tree</emphasis>. You can think of an absolute filename as a route from the base of the tree (<code>/</code>) to the end of some branch (a file). You will also hear people talk about the directory tree as if it were a <emphasis role="strong">family</emphasis> tree: thus subdirectories have <emphasis role="strong">parents</emphasis>, and a path shows the complete ancestry of a file. There are also relative paths that begin somewhere other than the root directory. You should remember that the directory <code>../</code> refers to the parent directory. This also applies to other directory like structures, such as data structures or other tree hierarchical organized entities. </para>
</listitem>
<listitem>
<para>There's no special directory path name component that corresponds to a physical device, such as your hard disk. This differs from <ulink url="http://en.wikipedia.org/wiki/RT-11">RT-11</ulink>, <ulink url="http://en.wikipedia.org/wiki/CP/M">CP/M</ulink>, <ulink url="http://en.wikipedia.org/wiki/OpenVMS">OpenVMS</ulink>, <ulink url="http://en.wikipedia.org/wiki/MS-DOS">MS-DOS</ulink>, <ulink url="http://en.wikipedia.org/wiki/AmigaOS">AmigaOS</ulink>, and <ulink url="http://en.wikipedia.org/wiki/Microsoft_Windows">Microsoft Windows</ulink>, where the path contains a device name such as <code>C:\</code>. (However, directory entries do exist that refer to physical devices as a part of the normal filesystem. See @{@filesysteminternals@}@.) </para>
</listitem>
</itemizedlist>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> While you <emphasis role="strong">can</emphasis> use almost any letters or symbols in a file name, in practice it is a bad idea to do so. It is better to avoid any characters that often have special meanings on the command line, including spaces, tabs, newlines, and other special characters: <code> { } ( ) [ ] ' ` " \ / > < | ; ! # & ^ * % @ $ </code> . If you want to separate words in a name, good choices are the period, hyphen, and underscore. You could also capitalize each word, "<code>LikeThis</code>". Experienced Linux users tend to avoid spaces in filenames. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The word <emphasis role="strong">path</emphasis> is used not only for <emphasis role="strong">fully-qualified filename</emphasis> as above but also for the <emphasis role="strong">command search path</emphasis>. The intended meaning is usually clear from the context. </para>
<para>The detailed best practices for the file hierarchy are described in the Filesystem Hierarchy Standard (<code>/usr/share/doc/debian-policy/fhs/fhs-2.3.txt.gz</code> and <code>hier</code>(7)). You should remember the following facts as the starter: </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of usage of key directories. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">directory</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">usage</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>/</code>
</para>
</entry>
<entry>
<para> A simple <code>/</code> represents the root directory. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/etc/</code>
</para>
</entry>
<entry>
<para> This is the place for the system wide configuration files. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/var/log/</code>
</para>
</entry>
<entry>
<para> This is the place for the system log files. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/home/</code>
</para>
</entry>
<entry>
<para> This is the directory which contains all the home directories for all non-privileged users. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para/>
</section>
<section>
<title>Filesystem internals</title>
<para>Following the <emphasis role="strong">Unix tradition</emphasis>, the Debian GNU/Linux system provides the <ulink url="http://en.wikipedia.org/wiki/File_system">filesystem</ulink> under which physical data on harddisks and other storage devices reside, and the interaction with the hardware devices such as console screens and remote serial consoles are represented in an unified manner [under /dev/]. </para>
<para>Each file, directory, named pipe (a way two programs can share data), or physical device on a Debian GNU/Linux system has a data structure called an <ulink url="http://en.wikipedia.org/wiki/Inode">inode</ulink> which describes its associated attributes such as the user who owns it (owner), the group that it belongs to, the time last accessed, etc. If you are really interested, see /usr/include/linux/fs.h for the exact definition of <code>struct inode</code> in the Debian GNU/Linux system. The idea of representing just about everything in the file system was a Unix innovation, and modern Linux kernels have developed this idea ever further. Now, even information about processes running in the computer can be found in the file system. </para>
<para>This abstract and unified representation of physical entities and internal processes is very powerful since this allows us to use the same command for the same kind of operation on many totally different devices. It is even possible to change the way the kernel works by writing data to special files that are linked to running processes. </para>
<para/>
<para>All of your files could be on one disk --- or you could have 20 disks, some of them connected to a different computer elsewhere on the network. You can't tell just by looking at the directory tree, and nearly all commands work just the same way no matter what physical device(s) your files are really on. [This is a good thing. Trust us.] Of course, methods do exist whereby you can tell what devices map to which physical or network devices. <code>mount</code> with no arguments will show how storage is mapped to physical or network devices. </para>
<para/>
</section>
<section>
<title>The filesystem permission system</title>
<para>The <ulink url="http://en.wikipedia.org/wiki/File_system_permissions">filesystem permissions</ulink> of Unix-like system are defined for three categories of affected users: </para>
<itemizedlist>
<listitem>
<para>the <emphasis role="strong">user</emphasis> who owns the file (<emphasis role="strong">u</emphasis>), </para>
</listitem>
<listitem>
<para>other users in the <emphasis role="strong">group</emphasis> which the file belongs to (<emphasis role="strong">g</emphasis>), and </para>
</listitem>
<listitem>
<para>all <emphasis role="strong">other</emphasis> users (<emphasis role="strong">o</emphasis>) also referred to as "world" and "everyone". </para>
</listitem>
</itemizedlist>
<para>For the file, each corresponding permission allows: </para>
<itemizedlist>
<listitem>
<para><emphasis role="strong">read</emphasis> (<emphasis role="strong">r</emphasis>): to examine contents of the file, </para>
</listitem>
<listitem>
<para><emphasis role="strong">write</emphasis> (<emphasis role="strong">w</emphasis>): to modify the file, and </para>
</listitem>
<listitem>
<para><emphasis role="strong">execute</emphasis> (<emphasis role="strong">x</emphasis>): to run the file as a command. </para>
</listitem>
</itemizedlist>
<para>For the directory, each corresponding permission allows: </para>
<itemizedlist>
<listitem>
<para><emphasis role="strong">read</emphasis> (<emphasis role="strong">r</emphasis>): to list contents of the directory, </para>
</listitem>
<listitem>
<para><emphasis role="strong">write</emphasis> (<emphasis role="strong">w</emphasis>): to add or remove files in the directory, and </para>
</listitem>
<listitem>
<para><emphasis role="strong">execute</emphasis> (<emphasis role="strong">x</emphasis>): to access files in the directory. </para>
</listitem>
</itemizedlist>
<para>Here, <emphasis role="strong">execute</emphasis> permission on the directory means not only to allow reading of files in its directory but also to allow viewing their attributes, such as the size and the modification time. </para>
<para>To display permission information (and more) for files and directories, <code>ls</code>(1) is used. When <code>ls</code> invoked with the <code>-l</code> option, it displays the following information in the order given: </para>
<itemizedlist>
<listitem>
<para>the <emphasis role="strong">type of file</emphasis> (first character) </para>
</listitem>
<listitem>
<para>the file's access <emphasis role="strong">permissions</emphasis> (the next nine characters, consisting of three characters each for user, group, and other in this order) </para>
</listitem>
<listitem>
<para>the <emphasis role="strong">number of hard links</emphasis> to the file </para>
</listitem>
<listitem>
<para>the name of the <emphasis role="strong">user</emphasis> who owns the file </para>
</listitem>
<listitem>
<para>the name of the <emphasis role="strong">group</emphasis> which the file belongs to </para>
</listitem>
<listitem>
<para>the <emphasis role="strong">size</emphasis> of the file in characters (bytes) </para>
</listitem>
<listitem>
<para>the <emphasis role="strong">date and time</emphasis> of the file (mtime) </para>
</listitem>
<listitem>
<para>the <emphasis role="strong">name</emphasis> of the file. </para>
</listitem>
</itemizedlist>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of the first character of "<code>ls -l</code>" output </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">character</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>-</code>
</para>
</entry>
<entry>
<para> normal file </para>
</entry>
</row>
<row>
<entry>
<para>
<code>d</code>
</para>
</entry>
<entry>
<para> directory </para>
</entry>
</row>
<row>
<entry>
<para>
<code>l</code>
</para>
</entry>
<entry>
<para> symlink </para>
</entry>
</row>
<row>
<entry>
<para>
<code>c</code>
</para>
</entry>
<entry>
<para> character device node </para>
</entry>
</row>
<row>
<entry>
<para>
<code>b</code>
</para>
</entry>
<entry>
<para> block device node </para>
</entry>
</row>
<row>
<entry>
<para>
<code>p</code>
</para>
</entry>
<entry>
<para> named pipe </para>
</entry>
</row>
<row>
<entry>
<para>
<code>s</code>
</para>
</entry>
<entry>
<para> socket </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>To change the owner of the file, <code>chown</code>(1) is used from the root account. To change the group of the file, <code>chgrp</code>(1) is used from the file's owner or root account. To change file and directory access permissions, <code>chmod</code>(1) is used from the file's owner or root account. Basic syntax to manipulate <code>foo</code> file is: </para>
<screen><![CDATA[# chown <newowner> foo
# chgrp <newgroup> foo
# chmod [ugoa][+-=][rwxXst][,...] foo
]]></screen>
<para>For example, in order to make a directory tree to be owned by a user <code>foo</code> and shared by a group <code>bar</code>, issue the following commands from the root account: </para>
<screen><![CDATA[# cd /some/location/
# chown -R foo:bar .
# chmod -R ug+rwX,o=rX .
]]></screen>
<para>There are three more special permission bits: </para>
<itemizedlist>
<listitem>
<para><emphasis role="strong">set user ID</emphasis> (<emphasis role="strong">s</emphasis> or <emphasis role="strong">S</emphasis> instead of user's <emphasis role="strong">x</emphasis>), </para>
</listitem>
<listitem>
<para><emphasis role="strong">set group ID</emphasis> (<emphasis role="strong">s</emphasis> or <emphasis role="strong">S</emphasis> instead of group's <emphasis role="strong">x</emphasis>), and </para>
</listitem>
<listitem>
<para><emphasis role="strong">sticky bit</emphasis> (<emphasis role="strong">t</emphasis> or <emphasis role="strong">T</emphasis> instead of other's <emphasis role="strong">x</emphasis>). </para>
</listitem>
</itemizedlist>
<para>Here the output of <code>ls -l</code> for these bits is <emphasis role="strong">capitalized</emphasis> if execution bits hidden by these outputs are <emphasis role="strong">unset</emphasis>. </para>
<para>Setting <emphasis role="strong">set user ID</emphasis> on an executable file allows a user to execute the executable file with the owner ID of the file (for example <emphasis role="strong">root</emphasis>). Similarly, setting <emphasis role="strong">set group ID</emphasis> on an executable file allows a user to execute the executable file with the group ID of the file (for example <emphasis role="strong">root</emphasis>). Because these settings can cause security risks, enabling them requires extra caution. </para>
<para>Setting <emphasis role="strong">set group ID</emphasis> on a directory enables the <ulink url="http://en.wikipedia.org/wiki/Berkeley_Software_Distribution">BSD-like</ulink> file creation scheme where all files created in the directory belong to the <emphasis role="strong">group</emphasis> of the directory. </para>
<para>Setting the <emphasis role="strong">sticky bit</emphasis> on a directory prevents a file in the directory from being removed by a user who is not the owner of the file. In order to secure the contents of a file in world-writable directories such as <code>/tmp</code> or in group-writable directories, one must not only set <emphasis role="strong">write</emphasis> permission off for the file but also set the <emphasis role="strong">sticky bit</emphasis> on the directory. Otherwise, the file can be removed and a new file can be created with the same name by any user who has write access to the directory. </para>
<para>Here are a few interesting examples of the file permissions. </para>
<screen><![CDATA[$ ls -l /etc/passwd /etc/shadow /dev/ppp /usr/sbin/exim4
crw------- 1 root root 108, 0 2007-04-29 07:00 /dev/ppp
-rw-r--r-- 1 root root 1427 2007-04-16 00:19 /etc/passwd
-rw-r----- 1 root shadow 943 2007-04-16 00:19 /etc/shadow
-rwsr-xr-x 1 root root 700056 2007-04-22 05:29 /usr/sbin/exim4
$ ls -ld /tmp /var/tmp /usr/local /var/mail /usr/src
drwxrwxrwt 10 root root 4096 2007-04-29 07:59 /tmp
drwxrwsr-x 10 root staff 4096 2007-03-24 18:48 /usr/local
drwxrwsr-x 4 root src 4096 2007-04-27 00:31 /usr/src
drwxrwsr-x 2 root mail 4096 2007-03-28 23:33 /var/mail
drwxrwxrwt 2 root root 4096 2007-04-29 07:11 /var/tmp
]]></screen>
<para>There is an alternative numeric mode to describe file permissions in <code>chmod</code>(1) commands. This numeric mode uses 3 to 4 digit wide octal (radix=8) numbers. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The numeric mode for file permissions in <code>chmod</code>(1) commands. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">digit</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> 1st optional digit </para>
</entry>
<entry>
<para> sum of <emphasis role="strong">set user ID</emphasis> (=4), <emphasis role="strong">set group ID</emphasis> (=2), and <emphasis role="strong">sticky bit</emphasis> (=1) </para>
</entry>
</row>
<row>
<entry>
<para> 2nd digit </para>
</entry>
<entry>
<para> sum of <emphasis role="strong">read</emphasis> (=4), <emphasis role="strong">write</emphasis> (=2), and <emphasis role="strong">execute</emphasis> (=1) permissions for <emphasis role="strong">user</emphasis> </para>
</entry>
</row>
<row>
<entry>
<para> 3rd digit </para>
</entry>
<entry>
<para> ditto for <emphasis role="strong">group</emphasis> </para>
</entry>
</row>
<row>
<entry>
<para> 4th digit </para>
</entry>
<entry>
<para> ditto for <emphasis role="strong">other</emphasis> </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>This sounds complicated but it is actually quite simple. If you look at the first few (2-10) columns from "<code>ls -l</code>" command output and read it as a binary (radix=2) representation of file permissions ("-" being "0" and "rwx" being "1"), the last 3 digit of the numeric mode value should make sense as an octal (radix=8) representation of file permissions to you. For example, try: </para>
<screen><![CDATA[$ touch foo bar
$ chmod u=rw,go=r foo
$ chmod 644 bar
$ ls -l foo bar
-rw-r--r-- 1 penguin penguin 17 2007-04-29 08:22 bar
-rw-r--r-- 1 penguin penguin 12 2007-04-29 08:22 foo
]]></screen>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> If you need to access information displayed by "<code>ls -l</code>" in shell script, you should use pertinent commands such as <code>test</code>(1), <code>stat</code>(1) and <code>readlink</code>(1). The shell builtin such as "<code>[</code>" or "<code>test</code>" may be used too. </para>
<para/>
</section>
<section>
<title>Control of permissions for newly created files: umask</title>
<para>What permissions are applied to a newly created file or directory is restricted by the <code>umask</code> shell built-in command. See <code>dash</code>(1), <code>bash</code>(1), and <code>builtins</code>(7). </para>
<screen><![CDATA[ (file permission) = (requested file permission) & ~(umask value)
]]></screen>
<table>
<caption/>
<tgroup cols="4">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<colspec colname="xxx4"/>
<tbody>
<row>
<entry>
<para> The <emphasis role="strong">umask</emphasis> value examples. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">umask</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">usage</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">file permission created</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">directory permission created</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> 0022 </para>
</entry>
<entry>
<para> writable only by the user </para>
</entry>
<entry>
<para>
<code>-rw-r--r--</code>
</para>
</entry>
<entry>
<para>
<code>-rwxr-xr-x</code>
</para>
</entry>
</row>
<row>
<entry>
<para> 0002 </para>
</entry>
<entry>
<para> writable by the group </para>
</entry>
<entry>
<para>
<code>-rw-rw-r--</code>
</para>
</entry>
<entry>
<para>
<code>-rwxrwxr-x</code>
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>The Debian system uses a user private group (UPG) scheme as its default. A UPG is created whenever a new user is added to the system. A UPG has the same name as the user for which it was created and that user is the only member of the UPG. UPGs makes it is safe to set umask to 0002 since every user has their own private group. (In some Unix variants, it is quite common to setup all normal users belonging to a single <emphasis role="strong"><code>users</code></emphasis> group and is good idea to set umask to 0022 for security in such cases.) </para>
<para/>
</section>
<section>
<title>Permissions for groups of users (group)</title>
<para>In order to make the group permission to be applied to a particular user, that user needs to be made a member of the group using "<code>sudo vigr</code>". </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Alternatively, you may dynamically add users to groups during the authentication process by adding "<code>auth optional pam_group.so</code>" line to <code>/etc/pam.d/common-auth</code> and setting <code>/etc/security/group.conf</code>. (See more: @{@authentication@}@.) </para>
<para>The hardware devices are just another kind of file on the Debian system. If you have problems accessing devices such as CD-ROM and USB memory stick from a user account, you should make that user a member of the relevant group. </para>
<para>Some notable system-provided groups allow their members to access particular files and devices without <code>root</code> privilege. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of example system-provided groups for file access. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">group</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">accessible files and devices</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> dialout </para>
</entry>
<entry>
<para> Full and direct access to serial ports. (reconfigure modem, dial anywhere, etc.) </para>
</entry>
</row>
<row>
<entry>
<para> dip </para>
</entry>
<entry>
<para> "Dialup IP", enough to run <code>ppp</code> or <code>dip</code> commands. </para>
</entry>
</row>
<row>
<entry>
<para> cdrom </para>
</entry>
<entry>
<para> CD-ROM, DVD+/-RW drives. </para>
</entry>
</row>
<row>
<entry>
<para> audio </para>
</entry>
<entry>
<para> An audio device. </para>
</entry>
</row>
<row>
<entry>
<para> video </para>
</entry>
<entry>
<para> A video device. </para>
</entry>
</row>
<row>
<entry>
<para> scanner </para>
</entry>
<entry>
<para> Scanner(s). </para>
</entry>
</row>
<row>
<entry>
<para> adm </para>
</entry>
<entry>
<para> System monitoring logs. </para>
</entry>
</row>
<row>
<entry>
<para> staff </para>
</entry>
<entry>
<para> Some directories for junior administrative work: <code>/usr/local</code>, <code>/home</code> . </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>Some notable system provided groups allow their members to execute particular commands without <code>root</code> privilege. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of notable system provided groups for particular command executions. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">group</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">accessible commands</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> sudo </para>
</entry>
<entry>
<para> execute sudo without their password. </para>
</entry>
</row>
<row>
<entry>
<para> lpadmin </para>
</entry>
<entry>
<para> execute commands to add, modify, and remove printers from printer databases. </para>
</entry>
</row>
<row>
<entry>
<para> plugdev </para>
</entry>
<entry>
<para> execute pmount(1) for removable devices such as USB memories. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>For the full listing of the system provided users and groups, see the recent version of the "Users and Groups" (/usr/share/doc/base-passwd/users-and-groups.html) document provided by the <code>base-passwd</code> package. </para>
<para>See manpages of <code>passwd</code>(5), <code>group</code>(5), <code>shadow</code>(5), <code>group</code>(5), <code>vipw</code>(8), <code>vigr</code>(8), and <code>pam_group</code>(8) for the management commands of the user and group system. </para>
<para/>
</section>
<section>
<title>Timestamps</title>
<para>There are three types of timestamps for a GNU/Linux file. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of types of timestamps. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">type</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">mtime</emphasis>
</para>
</entry>
<entry>
<para> the file modification time (<code>ls -l</code>) </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">ctime</emphasis>
</para>
</entry>
<entry>
<para> the file status change time (<code>ls -lc</code>) </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">atime</emphasis>
</para>
</entry>
<entry>
<para> the last file access time (<code>ls -lu</code>) </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>Note that <emphasis role="strong">ctime</emphasis> is not file creation time. </para>
<itemizedlist>
<listitem>
<para>Overwriting a file will change all of the <emphasis role="strong">mtime</emphasis>, <emphasis role="strong">ctime</emphasis>, and <emphasis role="strong">atime</emphasis> attributes of the file. </para>
</listitem>
<listitem>
<para>Changing permission or owner of a file will change the <emphasis role="strong">ctime</emphasis> and <emphasis role="strong">atime</emphasis> attributes of the file. </para>
</listitem>
<listitem>
<para>Reading a file will change the <emphasis role="strong">atime</emphasis> of the file. </para>
</listitem>
</itemizedlist>
<para>Note that even simply reading a file on the Debian system will normally cause a file write operation to update <emphasis role="strong">atime</emphasis> information in the <emphasis role="strong">inode</emphasis>. Mounting a filesystem with the <code>noatime</code> option will let the system skip this operation and will result in faster file access for the read. This is often recommended for laptops, because it reduces hard drive activity and saves power. See <code>mount</code>(8). </para>
<para>Use <code>touch</code>(1) command to change timestamps of existing files. </para>
<para>For timestamps, the <code>ls</code> command outputs different strings under the modern English locale (<code>en_US.UTF-8</code>) from under the old one (<code>C</code>). </para>
<screen><![CDATA[$ LANG=en_US.UTF-8 ls -l foo
-rw-r--r-- 1 penguin penguin 3 2008-03-05 00:47 foo
$ LANG=C ls -l foo
-rw-r--r-- 1 penguin penguin 3 Mar 5 00:47 foo
]]></screen>
<para/>
</section>
<section>
<title>Links</title>
<para>There are two methods of associating a file <code>foo</code> with a different filename <code>bar</code>. </para>
<itemizedlist>
<listitem>
<para>a <ulink url="http://en.wikipedia.org/wiki/Hard_link">hard link</ulink> is a duplicate name for an existing file ("<code>ln foo bar</code>"), </para>
</listitem>
<listitem>
<para>a <ulink url="http://en.wikipedia.org/wiki/Symbolic_link">symbolic link</ulink>, or "symlink", is a special file that points to another file by name ("<code>ln -s foo bar</code>"). </para>
</listitem>
</itemizedlist>
<para>See the following example for the changes in link counts and the subtle differences in the result of the <code>rm</code> command. </para>
<screen><![CDATA[$ echo "Original Content" > foo
$ ls -li foo
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 foo
$ ln foo bar # hard link
$ ln -s foo baz # symlink
$ ls -li foo bar baz
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin 3 2007-04-29 08:16 baz -> foo
2398521 -rw-r--r-- 2 penguin penguin 17 2007-04-29 08:15 foo
$ rm foo
$ echo "New Content" > foo
$ ls -li foo bar baz
2398521 -rw-r--r-- 1 penguin penguin 17 2007-04-29 08:15 bar
2398538 lrwxrwxrwx 1 penguin penguin 3 2007-04-29 08:16 baz -> foo
2398540 -rw-r--r-- 1 penguin penguin 12 2007-04-29 08:17 foo
$ cat bar
Original Content
$ cat baz
New Content
]]></screen>
<para>The hardlink can be made within the same file system and shares the same inode number which the "<code>-i</code>" option with <code>ls</code> command reveals. </para>
<para>The symlink always has nominal file access permissions of "<code>rwxrwxrwx</code>", as shown in the above example, with the effective access permissions dictated by the permissions of the file that it points to. </para>
<para/>
<para/>
<para/>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/attention.png" depth="15"/></imageobject><textobject><phrase><!></phrase></textobject></inlinemediaobject> It is generally good idea not to create complicated symbolic links or hardlinks at all unless you have a very good reason. It may cause nightmares where the logical combination of the symbolic links results in loops in the filesystem. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> It is generally preferable to use symbolic links rather than hardlinks unless you have a good reason for using a hardlink. </para>
<para>The "<code>.</code>" directory links to the directory that it appears in, thus the link count of any new directory starts at 2. The "<code>..</code>" directory links to the parent directory, thus the link count of the directory increases with the addition of new subdirectories. </para>
<para>If you are just moving to Linux from Windows, it will soon become clear how well-designed the file-name linking of Unix is, compared with the nearest Windows equivalent of "shortcuts". Because it is implemented in the file system, applications can't see any difference between a linked file and the original. In the case of hardlinks, there really is no difference. </para>
<para/>
</section>
<section>
<title>Named pipes (FIFOs)</title>
<para>A <ulink url="http://en.wikipedia.org/wiki/Named_pipe">named pipe</ulink> is a file that acts like a pipe. You put something into the file, and it comes out the other end. Thus it's called a FIFO, or First-In-First-Out: the first thing you put in the pipe is the first thing to come out the other end. </para>
<para>If you write to a named pipe, the process which is writing to the pipe doesn't terminate until the information being written is read from the pipe. If you read from a named pipe, the reading process waits until there is something to read before terminating. The size of the pipe is always zero --- it does not store data, it just links two processes like the shell "<code>|</code>". However, since this pipe has a name, the two processes don't have to be on the same command line or even be run by the same user. Pipes were a very influential innovation of Unix. </para>
<para>You can try it by doing the following: </para>
<screen><![CDATA[$ cd; mkfifo mypipe
$ echo "hello" >mypipe & # put into background
[1] 8022
$ ls -l mypipe
prw-r--r-- 1 penguin penguin 0 2007-04-29 08:25 mypipe
$ cat mypipe
hello
[1]+ Done echo "hello" >mypipe
$ ls mypipe
mypipe
$ rm mypipe
]]></screen>
<para/>
</section>
<section>
<title>Sockets</title>
<para>Sockets are used extensively by all the Internet communication, databases, and the operating system itself. It is similar to the named pipe (FIFO) and allows processes to exchange information even between different computers. For the socket, those processes do not need to be running at the same time nor to be running as the children of the same ancestor process. This is the endpoint for <ulink url="http://en.wikipedia.org/wiki/Inter-process_communication">the inter process communication (IPC)</ulink>. The exchange of information may occur over the network between different hosts. The two most common ones are <ulink url="http://en.wikipedia.org/wiki/Internet_socket">the Internet socket</ulink> and <ulink url="http://en.wikipedia.org/wiki/Unix_domain_socket">the Unix domain socket</ulink>. </para>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> "<code>netstat -an</code>" will provide a very useful overview of the sockets that are open on a given system. </para>
<para/>
</section>
<section>
<title>Device files</title>
<para><ulink url="http://en.wikipedia.org/wiki/Device_file">Device files</ulink> refer to physical or virtual devices on your system, such as your hard disk, video card, screen, or keyboard. An example of a virtual device is the console, represented by <code>/dev/console</code>. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The device types. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">device type</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">character device</emphasis>
</para>
</entry>
<entry>
<para> This can be accessed one character at a time, that is, the smallest unit of data which can be written to or read from the device is a character (byte).</para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">block device</emphasis>
</para>
</entry>
<entry>
<para> This must be accessed in larger units called blocks, which contain a number of characters. Your hard disk is a block device.</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>You can read and write device files, though the file may well contain binary data which may be an incomprehensible-to-humans gibberish. Writing data directly to these files is sometimes useful for the troubleshooting of hardware connections. For example, you can dump a text file to the printer device <code>/dev/lp0</code> or send modem commands to the appropriate serial port <code>/dev/ttyS0</code>. But, unless this is done carefully, it may cause a major disaster. So be cautious. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> For the normal access to a printer, use the <code>lp</code>(1) command. </para>
<para>The device node number are displayed by executing <code>ls</code> as: </para>
<screen><![CDATA[$ ls -l /dev/hda /dev/ttyS0 /dev/zero
brw-rw---- 1 root cdrom 3, 0 2007-04-29 07:00 /dev/hda
crw-rw---- 1 root dialout 4, 64 2007-04-29 07:00 /dev/ttyS0
crw-rw-rw- 1 root root 1, 5 2007-04-29 07:00 /dev/zero
]]></screen>
<para>Here, </para>
<itemizedlist>
<listitem>
<para><code>/dev/hda</code> has the major device number 3 and the minor device number 0. This is read/write accessible by the user who belongs to <code>disk</code> group, </para>
</listitem>
<listitem>
<para><code>/dev/ttyS0</code> has the major device number 4 and the minor device number 64. This is read/write accessible by the user who belongs to <code>dialout</code> group, and </para>
</listitem>
<listitem>
<para><code>/dev/zero</code> has the major device number 1 and the minor device number 5. This is read/write accessible by anyone. </para>
</listitem>
</itemizedlist>
<para>In the Linux 2.6 system, the filesystem under <code>/dev</code> is automatically populated by the <code>udev</code>(7) mechanism. </para>
<para/>
</section>
<section>
<title>Special device files</title>
<para>There are some special device files. </para>
<table>
<caption/>
<tgroup cols="3">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<tbody>
<row>
<entry>
<para> List of special device files. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">device file</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">action</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">response</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/null</code>
</para>
</entry>
<entry>
<para> read </para>
</entry>
<entry>
<para> it returns "end-of-file (EOF) character". </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/null</code>
</para>
</entry>
<entry>
<para> write </para>
</entry>
<entry>
<para> it is a bottomless data dump pit. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/zero</code>
</para>
</entry>
<entry>
<para> read </para>
</entry>
<entry>
<para> it returns "the <code>\0</code> (NUL) character" (not the same as the number zero ASCII). </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/random</code>
</para>
</entry>
<entry>
<para> read </para>
</entry>
<entry>
<para> it returns random characters from a true random number generator, delivering real entropy. (slow) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/urandom</code>
</para>
</entry>
<entry>
<para> read </para>
</entry>
<entry>
<para> it returns random characters from a cryptographically secure pseudorandom number generator. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>/dev/full</code>
</para>
</entry>
<entry>
<para> write </para>
</entry>
<entry>
<para> it returns the disk-full (ENOSPC) error. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>These are frequently used in conjunction with the shell redirection (see @{@typicalcommandseshellredirection@}@). </para>
<para/>
<para/>
</section>
<section>
<title>procfs and sysfs</title>
<para>The <ulink url="http://en.wikipedia.org/wiki/Procfs">procfs</ulink> and <ulink url="http://en.wikipedia.org/wiki/Sysfs">sysfs</ulink> mounted on <code>/proc</code> and <code>/sys</code> are the pseudo-filesystem and expose internal data structures of the kernel to the userspace. In other word, these entries are virtual, meaning that they act as a convenient window into the operation of the operating system. </para>
<para>The directory <code>/proc</code> contains (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID). </para>
<para>System utilities that access process information, such as <code>ps</code>, get their information from this directory structure. </para>
<para>The directories under <code>/proc/sys/</code> contain interface to change certain kernel parameters at run time. (You may do the same through specialized command <code>sysctl</code>(8) or its preload/configuration file <code>/etc/sysctrl.conf</code>.) </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The Linux kernel may complain "Too many open files". You can fix avoid this by executing "<code>echo "65536" > /proc/sys/fs/file-max</code>" from the root shell to increase <code>file-max</code> value. </para>
<para>People frequently panic when they notice one file in particular - <code>/proc/kcore</code> - which is generally huge. This is (more or less) a copy of the contents of your computer's memory. It's used to debug the kernel. It is a virtual file that points to computer memory, so don't worry about its size. </para>
<para/>
<para>The directory under <code>/sys</code> contains exported kernel data structures, their attributes, and the linkages between them. It also contains interface to change certain kernel parameters at run time. </para>
<para>See <code>proc.txt(.gz)</code>, <code>sysfs.txt(.gz)</code> and other related documents in the Linux kernel documentation (/usr/share/doc/linux-doc-2.6.*/Documentation/filesystems/*) provided by the <code>linux-doc-2.6.*</code> package. </para>
<para/>
</section>
</section>
<section>
<title>Midnight Commander (MC)</title>
<para>Midnight Commander (MC) is a GNU "Swiss army knife" for the Linux console and other terminal environments. This gives newbie a menu driven console experience which is much easier to learn than standard Unix commands. </para>
<para>You may need to install the Midnight Commander package which is titled mc. </para>
<screen><![CDATA[ sudo aptitude install mc
]]></screen>
<para>Use the mc command to explore the Debian system. This is the best way to learn. Please explore few interesting locations just using the cursor keys and Enter key: </para>
<itemizedlist>
<listitem>
<para><code>/etc</code> and its subdirectories. </para>
</listitem>
<listitem>
<para><code>/var/log</code> and its subdirectories. </para>
</listitem>
<listitem>
<para><code>/usr/share/doc</code> and its subdirectories. </para>
</listitem>
<listitem>
<para><code>/sbin</code> and <code>/bin</code> </para>
</listitem>
</itemizedlist>
<para/>
<section>
<title>Customization of MC</title>
<para>In order to make MC to change working directory upon exit and cd to the frequently used directories, I suggest to modify <code>~/.bashrc</code> to include: </para>
<para/>
<screen><![CDATA[. /usr/share/mc/bin/mc.sh
]]></screen>
<para>See <code>mc</code>(1) (under the "<code>-P</code>" option) for the reason. (If you do not understand what exactly I am talking here, you can do this later.) </para>
<para/>
</section>
<section>
<title>Starting MC</title>
<para>MC can be started by: </para>
<screen><![CDATA[$ mc
]]></screen>
<para>MC takes care of all file operations through its menu, requiring minimal user effort. Just press F1 to get the help screen (see next paragraph if this doesn't work). You can play with MC just by pressing cursor-keys and function-keys. </para>
<para>In some consoles such as <code>gnome-terminal</code>, key strokes of function-keys may be stolen by the console program. You can disable these features by "Edit" -> "Keyboard Shortcuts" for <code>gnome-terminal</code>. In particular, consider removing the mapping of F1 to Gnome Terminal Help; doing so will allow F1 to show Midnight Commander help. </para>
<para>If you encounter character encoding problem which displays garbage characters, adding "<code>-a</code>" to MC's command line may help prevent problems. </para>
<para>If this doesn't clear up your display problems with MC, see @{@theterminalconfiguration@}@. </para>
<para/>
</section>
<section>
<title>File manager in MC</title>
<para>The default is two directory panels containing file lists. Another useful mode is to set the right window to "information" to see file access privilege information, etc. Following are some essential keystrokes. With the <code>gpm</code> daemon running, one can use a mouse, too. (Make sure to press the shift-key to obtain the normal behavior of cut and paste in MC.) </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The key bindings of MC. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">key</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">key binding</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>F1</code>
</para>
</entry>
<entry>
<para> Help menu </para>
</entry>
</row>
<row>
<entry>
<para>
<code>F3</code>
</para>
</entry>
<entry>
<para> Internal file viewer </para>
</entry>
</row>
<row>
<entry>
<para>
<code>F4</code>
</para>
</entry>
<entry>
<para> Internal editor </para>
</entry>
</row>
<row>
<entry>
<para>
<code>F9</code>
</para>
</entry>
<entry>
<para> Activate pull down menu </para>
</entry>
</row>
<row>
<entry>
<para>
<code>F10</code>
</para>
</entry>
<entry>
<para> Exit Midnight Commander </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Tab</code>
</para>
</entry>
<entry>
<para> Move between two windows </para>
</entry>
</row>
<row>
<entry>
<para><code>Insert</code> or <code>Ctrl-T</code> </para>
</entry>
<entry>
<para> Mark file for a multiple-file operation such as copy </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Del</code>
</para>
</entry>
<entry>
<para> Delete file (be careful---set MC to safe delete mode) </para>
</entry>
</row>
<row>
<entry>
<para> Cursor keys </para>
</entry>
<entry>
<para> Self-explanatory </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para/>
</section>
<section>
<title>Command-line tricks in MC</title>
<itemizedlist>
<listitem>
<para>Any <code>cd</code> command will change the directory shown on the selected screen. </para>
</listitem>
<listitem>
<para><code>Ctrl-Enter</code> or <code>Alt-Enter</code> will copy a filename to the command line. Use this with the <code>cp</code> or <code>mv</code> command together with command-line editing. </para>
</listitem>
<listitem>
<para><code>Alt-Tab</code> will show shell filename expansion choices. </para>
</listitem>
<listitem>
<para>One can specify the starting directory for both windows as arguments to MC; for example, <code>mc /etc /root</code>. </para>
</listitem>
<listitem>
<para><code>Esc</code> + <code>n-key</code> == <code>Fn</code> (i.e., <code>Esc</code> + <code>1</code> = <code>F1</code>, etc.; <code>Esc</code> + <code>0</code> = <code>F10</code>) </para>
</listitem>
<listitem>
<para>Pressing <code>Esc</code> before the key has the same effect as pressing the <code>Alt</code> and the key together.; i.e., type <code>Esc</code> + <code>c</code> for <code>Alt-C</code>. <code>Esc</code> is called meta-key and sometimes noted as "<code>M-</code>" </para>
</listitem>
</itemizedlist>
<para/>
</section>
<section>
<title>The internal editor in MC</title>
<para>The internal editor has an interesting cut-and-paste scheme. Pressing <code>F3</code> marks the start of a selection, a second <code>F3</code> marks the end of selection and highlights the selection. Then you can move your cursor. If you press F6, the selected area will be moved to the cursor location. If you press F5, the selected area will be copied and inserted at the cursor location. <code>F2</code> will save the file. <code>F10</code> will get you out. Most cursor keys work intuitively. </para>
<para>This editor can be directly started on a file: </para>
<screen><![CDATA[$ mc -e filename_to_edit
$ mcedit filename_to_edit
]]></screen>
<para>This is not a multi-window editor, but one can use multiple Linux consoles to achieve the same effect. To copy between windows, use Alt-F<n> keys to switch virtual consoles and use "File->Insert file" or "File->Copy to file" to move a portion of a file to another file. </para>
<para>This internal editor can be replaced with any external editor of choice. </para>
<para>Also, many programs use the environment variables <code>EDITOR</code> or <code>VISUAL</code> to decide which editor or viewer to use. If you are uncomfortable with <code>vim</code> or <code>nano</code> initially, you may set these to <code>mcedit</code> by adding these lines to <code>~/.bashrc</code>: </para>
<screen><![CDATA[...
export EDITOR=mcedit
export VISUAL=mcedit
...
]]></screen>
<para>I do recommend setting these to <code>vim</code> if possible. </para>
<para>If you are uncomfortable with <code>vim</code>, you can keep using <code>mcedit</code> for most system maintenance tasks. </para>
<para/>
<para/>
<para/>
</section>
<section>
<title>The internal viewer in MC</title>
<para>Very smart viewer. This is a great tool for searching words in documents. I always use this for files in the <code>/usr/share/doc</code> directory. This is the fastest way to browse through masses of Linux information. This viewer can be directly started like so: </para>
<screen><![CDATA[$ mc -v path/to/filename_to_view
$ mcview path/to/filename_to_view
]]></screen>
<para/>
</section>
<section>
<title>Auto-start features of MC</title>
<para>Press Enter on a file, and the appropriate program will handle the content of the file (see @{@customizingprogramtobestarted@}@). This is a very convenient MC feature. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The reaction to the enter key in MC. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">file type</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">reaction to enter key</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> executable file </para>
</entry>
<entry>
<para> Execute command </para>
</entry>
</row>
<row>
<entry>
<para> man file </para>
</entry>
<entry>
<para> Pipe content to viewer software </para>
</entry>
</row>
<row>
<entry>
<para> html file </para>
</entry>
<entry>
<para> Pipe content to web browser </para>
</entry>
</row>
<row>
<entry>
<para><code>tar.gz</code><code>.deb</code> file </para>
</entry>
<entry>
<para> Browse its contents as if subdirectory </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>In order to allow these viewer and virtual file features to function, viewable files should not be set as executable. Change their status using the <code>chmod</code> command or via the MC file menu. </para>
<para/>
</section>
<section>
<title>FTP virtual filesystem of MC</title>
<para>MC can be used to access files over the Internet using FTP. Go to the menu by pressing <code>F9</code>, then type "<code>p</code>" to activate the FTP virtual filesystem. Enter a URL in the form "<code>username:passwd@hostname.domainname</code>", which will retrieve a remote directory that appears like a local one. </para>
<para>Try "<code>http.us.debian.org/debian</code>" as the URL and browse the Debian archive. </para>
<para/>
</section>
</section>
<section>
<title>The basic Unix-like work environment</title>
<para>Although MC enables you to do almost everything, it is very important for you to learn how to use the command line tools invoked from the shell prompt and become familiar with the Unix-like work environment. </para>
<para/>
<section>
<title>The login shell</title>
<para>You can select your login shell with the <code>chsh</code> command. </para>
<table>
<caption/>
<tgroup cols="4">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<colspec colname="xxx4"/>
<tbody>
<row>
<entry>
<para> List of shell programs. </para>
</entry>
<entry>
<para> 1 </para>
</entry>
<entry>
<para> 2 </para>
</entry>
<entry>
<para> 3 </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">package</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">popcon</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">size</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">POSIX shell</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">description</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>bash</code>
</para>
</entry>
<entry>
<para> 38091 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> The GNU Bourne Again SHell. (de facto standard) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>tcsh</code>
</para>
</entry>
<entry>
<para> 6855 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> No </para>
</entry>
<entry>
<para> TENEX C Shell, an enhanced version of Berkeley csh. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>dash</code>
</para>
</entry>
<entry>
<para> 2624 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> The Debian Almquist Shell. Good for shell script. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>zsh</code>
</para>
</entry>
<entry>
<para> 1639 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> The standard shell with many enhancements. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>pdksh</code>
</para>
</entry>
<entry>
<para> 290 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> A public domain version of the Korn shell. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>csh</code>
</para>
</entry>
<entry>
<para> 256 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> No </para>
</entry>
<entry>
<para> OpenBSD C Shell, a version of Berkeley csh. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>sash</code>
</para>
</entry>
<entry>
<para> ? </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> Stand-alone shell with built-in commands. (Not meant for standard <code>/bin/sh</code>.) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ksh</code>
</para>
</entry>
<entry>
<para> 161 </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> The real, AT&T version of the Korn shell. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>rc</code>
</para>
</entry>
<entry>
<para> ? </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> No </para>
</entry>
<entry>
<para> An implementation of the AT&T Plan 9 shell. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>posh</code>
</para>
</entry>
<entry>
<para> ? </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> Yes </para>
</entry>
<entry>
<para> Policy-compliant Ordinary SHell. A <code>pdksh</code> derivative. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>es</code>
</para>
</entry>
<entry>
<para> ? </para>
</entry>
<entry>
<para> - </para>
</entry>
<entry>
<para> No </para>
</entry>
<entry>
<para> An extensible shell based on the AT&T Plan 9 shell: <code>rc</code>. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>In this tutorial chapter, the interactive shell always means <code>bash</code>. </para>
<para/>
</section>
<section>
<title>Customizing bash</title>
<para>You can customize <code>bash</code> behavior by <code>~/.bashrc</code>. For example, I added followings to <code>~/.bashrc</code>: </para>
<screen><![CDATA[# CD upon exiting MC
. /usr/share/mc/bin/mc.sh
# set CDPATH to good one
CDPATH=.:/usr/share/doc:~/Desktop/src:~/Desktop:~
export CDPATH
PATH="${PATH}":/usr/sbin:/sbin
# set PATH so it includes user's private bin if it exists
if [ -d ~/bin ] ; then
PATH=~/bin:"${PATH}"
fi
export PATH
EDITOR=vim
export EDITOR
]]></screen>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> You can find more <code>bash</code> customization tips, such as @{@colorizedcommands@}@, in @{@systemtips@}@. </para>
<para/>
</section>
<section>
<title>Special key strokes</title>
<para>In the Unix-like environment, there are few key strokes which have special meanings. Please note that on a normal Linux character console, only the left-hand <code>Ctrl</code> and <code>Alt</code> keys work as expected. Here are few notable key strokes to remember. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of key bindings for bash. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">key</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">key binding</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-U</code>
</para>
</entry>
<entry>
<para> Erase line before cursor. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-H</code>
</para>
</entry>
<entry>
<para> Erase a character before cursor. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-D</code>
</para>
</entry>
<entry>
<para> Terminate input. (exit shell if you are using shell) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-C</code>
</para>
</entry>
<entry>
<para> Terminate a running program. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-Z</code>
</para>
</entry>
<entry>
<para> Temporarily stop program by moving it to the background job </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-S</code>
</para>
</entry>
<entry>
<para> Halt output to screen. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-Q</code>
</para>
</entry>
<entry>
<para> Reactivate output to screen. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-Alt-Del</code>
</para>
</entry>
<entry>
<para> Reboot/halt the system, see manpage for <code>inittab</code>. </para>
</entry>
</row>
<row>
<entry>
<para><code>Left-Alt-key</code> (optionally, <code>Windows-key</code>) </para>
</entry>
<entry>
<para>Meta-key for Emacs and the similar UI. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Up-arrow</code>
</para>
</entry>
<entry>
<para> Start command history search under <code>bash</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-R</code>
</para>
</entry>
<entry>
<para> Start incremental command history search under <code>bash</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Tab</code>
</para>
</entry>
<entry>
<para> Complete input of the filename to the command line under <code>bash</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>Ctrl-V</code>
<code>Tab</code>
</para>
</entry>
<entry>
<para> Input <code>Tab</code> without expansion to the command line under <code>bash</code>. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> The terminal feature of <code>Ctrl-S</code> can be disabled using <code>stty</code>(1) command. </para>
<para/>
</section>
<section>
<title>Unix style mouse operations</title>
<para>The Unix style mouse operations are based on the 3 button mouse system. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The Unix style mouse operations. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">action</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">response</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> Left-click-and-drag mouse </para>
</entry>
<entry>
<para> Select and copy to the clipboard. </para>
</entry>
</row>
<row>
<entry>
<para> Left-click </para>
</entry>
<entry>
<para> Select the start of selection. </para>
</entry>
</row>
<row>
<entry>
<para> Right-click </para>
</entry>
<entry>
<para> Select the end of selection and copy to the clipboard. </para>
</entry>
</row>
<row>
<entry>
<para> Middle-click </para>
</entry>
<entry>
<para> Paste clipboard at the cursor. </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>The center wheel on the modern wheel mouse is considered middle mouse button and can be used for middle-click. Clicking left and right mouse buttons together serves as the middle-click under the 2 button mouse system situation. In order to use a mouse in the Linux character console, you need to have <code>gpm</code> running as daemon. </para>
<para/>
</section>
<section>
<title>The pager</title>
<para>The <code>less</code> program is the enhanced pager (file content browser). Hit "<code>h</code>" for help. It can do much more than <code>more</code>. This <code>less</code> command can be supercharged by executing <code>eval $(lesspipe)</code> or <code>eval $(lessfile)</code> in the shell startup script. See more in <code>/usr/share/doc/lessf/LESSOPEN</code>. The <code>-R</code> option allows raw character output and enables ANSI color escape sequences. See <code>less</code>(1). </para>
<para/>
</section>
<section>
<title>The text editor</title>
<para>You should become proficient in one of the variants of <ulink url="http://en.wikipedia.org/wiki/Vim_(text_editor)">Vim</ulink> or <ulink url="http://en.wikipedia.org/wiki/Emacs">Emacs</ulink> programs which are popular in the Unix-like system. </para>
<para>I think getting used to Vim commands is the right thing to do, since Vi-editor is always there in the Linux/Unix world. (Actually, original <code>vi</code> or new <code>nvi</code> are the programs you find everywhere. I chose Vim instead for newbie since it offers you help through <code>F1</code> key while it is similar enough and more powerful.) </para>
<para>If you chose either <ulink url="http://en.wikipedia.org/wiki/Emacs">Emacs</ulink> or <ulink url="http://en.wikipedia.org/wiki/XEmacs">XEmacs</ulink> instead as your choice of the editor, that is another good choice indeed, particularly for programming. Emacs has a plethora of other features as well, including functioning as a newsreader, directory editor, mail program, etc.. When used for programming or editing shell scripts, it intelligently recognizes the format of what you are working on, and tries to provide assistance. Some people maintain that the only program they need on Linux is Emacs. Ten minutes learning Emacs now can save hours later. Having the gnu Emacs manual for reference when learning Emacs is highly recommended. </para>
<para>All these programs usually come with tutoring program for you to learn them by practice. Start Vim by typing "<code>vim</code>" and press F1-key. You should at least read the first 35 lines. Then do the online training course by moving cursor to <code>|tutor|</code> and pressing <code>Ctrl-]</code>. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Good editors, such as Vim and Emacs, can be used to handle UTF-8 and other exotic encoding texts correctly with proper option in the x-terminal-emulator on X under UTF-8 locale with proper font settings. Please refer to their documentation on multibyte text. </para>
<para/>
</section>
<section>
<title>Setting a default text editor</title>
<para>Debian comes with a number of different editors. We recommend to install the <code>vim</code> package, as mentioned above. </para>
<para>Debian provides unified access to the system default editor via command <code>/usr/bin/editor</code> so other programs (e.g., <code>reportbug</code>(1)) can invoke it. You can change it by: </para>
<screen><![CDATA[$ sudo update-alternatives --config editor
]]></screen>
<para>The choice <code>/usr/bin/vim.basic</code> is the recommendation for newbies by the author. This supports syntax highlighting. </para>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> Many programs use the environment variables "<code>EDITOR</code>" or "<code>VISUAL</code>" to decide which editor to use (see @{@theinternaleditorinmc@}@ and @{@customizingprogramtobestarted@}@). </para>
<para/>
</section>
<section>
<title>Customizing vim</title>
<para>You can customize <code>vim</code> behavior by <code>~/.vimrc</code>. For example, I use: </para>
<screen><![CDATA[" -------------------------------
" Local configuration
"
set nocompatible
set nopaste
set pastetoggle=<f2>
syn on
if $USER == "root"
set nomodeline
set noswapfile
else
set modeline
set swapfile
endif
" filler to avoid the line above being recognized as a modeline
" filler
" filler
]]></screen>
<para/>
</section>
<section>
<title>Recording the shell activities</title>
<para>The output of the shell command may roll off your screen and may be lost forever. It is good practice to log shell activities into the file for you to review them later. This kind of record is essential when you perform any system administration tasks. </para>
<para>The basic method of recording the shell activity is to run it under the <code>script</code>(1) command. </para>
<screen><![CDATA[$ script
Script started, file is typescript
]]></screen>
<itemizedlist>
<listitem>do whatever shell commands ... </listitem>
<listitem>
<para>press <code>Ctrl-D</code> to exit <code>script</code>. </para>
</listitem>
</itemizedlist>
<para/>
<screen><![CDATA[$ vim typescript
]]></screen>
<para>See @{@recordingtheshelctivitiescleanly@}@ . </para>
<para/>
</section>
<section>
<title>Basic Unix commands</title>
<para>Let's learn the basic Unix commands. Here I use "Unix" in its generic sense. Any Unix clone OSs usually offer the equivalent commands. The Debian system is no exception. Do not worry if some commands do not work as you wish now. If <code>alias</code> is used in the shell, its corresponding command outputs are different. These examples are not meant to be executed in this order. </para>
<para>Try all the following commands from the non-privileged user account: </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of basic Unix commands. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">command</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">description</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>pwd</code>
</para>
</entry>
<entry>
<para> Display name of current/working directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>whoami</code>
</para>
</entry>
<entry>
<para> Display current user name.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>id</code>
</para>
</entry>
<entry>
<para> Display current user identity (name, uid, gid, and associated groups). </para>
</entry>
</row>
<row>
<entry>
<para>
<code>file <foo></code>
</para>
</entry>
<entry>
<para> Display a type of file for the file <foo>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>type -p <commandname></code>
</para>
</entry>
<entry>
<para> Display a file location of command <code><commandname></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>which <commandname></code>
</para>
</entry>
<entry>
<para> , , </para>
</entry>
</row>
<row>
<entry>
<para>
<code>type <commandname></code>
</para>
</entry>
<entry>
<para> Display information on command <code><commandname></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>apropos <key-word></code>
</para>
</entry>
<entry>
<para> Find commands related to <code><key-word></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>man -k <key-word></code>
</para>
</entry>
<entry>
<para> , , </para>
</entry>
</row>
<row>
<entry>
<para>
<code>whatis <commandname></code>
</para>
</entry>
<entry>
<para> Display one line explanation on command <code><commandname></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>man -a <commandname></code>
</para>
</entry>
<entry>
<para> Display explanation on command <code><commandname></code>. (Unix style)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>info <commandname></code>
</para>
</entry>
<entry>
<para> Display rather long explanation on command <code><commandname></code>. (GNU style)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls</code>
</para>
</entry>
<entry>
<para> List contents of directory. (non-dot files and directories) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls -a</code>
</para>
</entry>
<entry>
<para> List contents of directory. (all files and directories) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls -A</code>
</para>
</entry>
<entry>
<para> List contents of directory. (almost all files and directories, i.e., skip "<code>..</code>" and "<code>.</code>")</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls -la</code>
</para>
</entry>
<entry>
<para> List all contents of directory with detail information.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls -lai</code>
</para>
</entry>
<entry>
<para> List all contents of directory with inode number and detail information.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ls -d</code>
</para>
</entry>
<entry>
<para> List all directories under the current directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tree</code>
</para>
</entry>
<entry>
<para> Display file tree contents.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>lsof <foo></code>
</para>
</entry>
<entry>
<para> List open status of file <code><foo></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>lsof -p <pid></code>
</para>
</entry>
<entry>
<para> List files opened by PID=<code><pid></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>mkdir <foo></code>
</para>
</entry>
<entry>
<para> Make a new directory <code><foo></code> in the current directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>rmdir <foo></code>
</para>
</entry>
<entry>
<para> Remove a directory <code><foo></code> in the current directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd <foo></code>
</para>
</entry>
<entry>
<para> Change directory to the directory <code><foo></code> in the current directory or in the directory listed in the variable <code>CDPATH</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd /</code>
</para>
</entry>
<entry>
<para> Change directory to the root directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd</code>
</para>
</entry>
<entry>
<para> Change directory to the current user's home directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd /<foo></code>
</para>
</entry>
<entry>
<para> Change directory to the absolute path directory <code>/<foo></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd ..</code>
</para>
</entry>
<entry>
<para> Change directory to the parent directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd ~<foo></code>
</para>
</entry>
<entry>
<para> Change directory to the home directory of the user <code><foo></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cd -</code>
</para>
</entry>
<entry>
<para> Change directory to the previous directory.</para>
</entry>
</row>
<row>
<entry>
<para>
<code></etc/motd pager</code>
</para>
</entry>
<entry>
<para> Display contents of <code>/etc/motd</code> using the default pager. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>touch <junkfile></code>
</para>
</entry>
<entry>
<para> Create a empty file <code><junkfile></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>cp <foo> <bar></code>
</para>
</entry>
<entry>
<para> Copy a existing file <code><foo></code> to a new file <code><bar></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>rm <junkfile></code>
</para>
</entry>
<entry>
<para> Remove a file <code><junkfile></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>mv <foo> <bar></code>
</para>
</entry>
<entry>
<para> Rename an existing file <code><foo></code> to a new name <code><bar></code>. The directory <code><bar></code> must not exist. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>mv <foo> <bar></code>
</para>
</entry>
<entry>
<para> Move an existing file <code><foo></code> to a new location <code><bar>/<foo></code>. The directory <code><bar></code> must exist.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>mv <foo> <bar>/<baz></code>
</para>
</entry>
<entry>
<para> Move an existing file <code><foo></code> to a new location with a new name <code><bar>/<baz></code>. The directory <code><bar></code> must exist but the directory <code><bar>/<baz></code> must not exist.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>chmod 600 <foo></code>
</para>
</entry>
<entry>
<para> Make an existing file <code><foo></code> to be non-readable and non-writable by the other people. (non-executable for all)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>chmod 644 <foo></code>
</para>
</entry>
<entry>
<para> Make an existing file <code><foo></code> to be readable but non-writable by the other people. (non-executable for all)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>chmod 755 <foo></code>
</para>
</entry>
<entry>
<para> Make an existing file <code><foo></code> to be readable but non-writable by the other people. (executable for all)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>find . -name <pattern></code>
</para>
</entry>
<entry>
<para> find matching filenames using shell <code><pattern></code>. (slower)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>locate -d . <pattern></code>
</para>
</entry>
<entry>
<para> find matching filenames using shell <code><pattern></code>. (quicker using regularly generated database)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>grep -e "<pattern>" *.html</code>
</para>
</entry>
<entry>
<para> Find a "<pattern>" in all of the files ending with "<code>.html</code>" in current directory and display them all.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>top</code>
</para>
</entry>
<entry>
<para> Display process information using full screen. Type "<code>q</code>" to quit.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ps aux | pager</code>
</para>
</entry>
<entry>
<para> Display information on all the running processes using BSD style output. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ps -ef | pager</code>
</para>
</entry>
<entry>
<para> Display information on all the running processes using Unix system-V style output.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ps aux | grep -e "[e]xim4*"</code>
</para>
</entry>
<entry>
<para> Display all processes running <code>exim</code> or <code>exim4</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ps axf | pager</code>
</para>
</entry>
<entry>
<para> Display information on all the running processes with ASCII art output.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>kill <1234></code>
</para>
</entry>
<entry>
<para> Kill a process identified by the process ID: <1234>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>gzip <foo></code>
</para>
</entry>
<entry>
<para> Compress <code><foo></code> to create <code><foo>.gz</code> using the Lempel-Ziv coding (LZ77).</para>
</entry>
</row>
<row>
<entry>
<para>
<code>gunzip <foo>.gz</code>
</para>
</entry>
<entry>
<para> Decompress <code><foo>.gz</code> to create <code><foo></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>bzip2 <foo></code>
</para>
</entry>
<entry>
<para> Compress <code><foo></code> to create <code><foo>.bz2</code> using the Burrows-Wheeler block sorting text compression algorithm, and Huffman coding. (Better compression than <code>gzip</code>)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>bunzip2 <foo>.bz2</code>
</para>
</entry>
<entry>
<para> Decompress <code><foo>.bz2</code> to create <code><foo></code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -xvf <foo.tar></code>
</para>
</entry>
<entry>
<para> Extract files from <code><foo>.tar</code> archive.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -xvzf <foo>.tar.gz</code>
</para>
</entry>
<entry>
<para> Extract files from gzipped <code><foo>.tar.gz</code> archive.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -xvf -j <foo.tar.bz2></code>
</para>
</entry>
<entry>
<para> Extract files from <code><foo>.tar.bz2</code> archive. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -cvf <foo>.tar <bar>/</code>
</para>
</entry>
<entry>
<para> Archive contents of folder <code><bar>/</code> in <code><foo>.tar</code> archive.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -cvzf <foo>.tar.gz <bar>/</code>
</para>
</entry>
<entry>
<para> Archive contents of folder <code><bar>/</code> in compressed <code><foo>.tar.gz</code> archive.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>tar -cvjf <foo>.tar.bz2 <bar>/</code>
</para>
</entry>
<entry>
<para> Archive contents of folder <code><bar>/</code> in <code><foo>.tar.bz2</code> archive.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>zcat README.gz | pager</code>
</para>
</entry>
<entry>
<para> Display contents of compressed <code>README.gz</code> using the default pager.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>zcat README.gz > foo</code>
</para>
</entry>
<entry>
<para> Create a file <code>foo</code> with the decompressed content of <code>README.gz</code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>zcat README.gz >> foo</code>
</para>
</entry>
<entry>
<para> Append the decompressed content of <code>README.gz</code> to the end of the file <code>foo</code>. (If it does not exist, create it first.)</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Unix has a tradition to hide filenames which start with "<code>.</code>". They are traditionally files that contain configuration information and user preferences. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> For <code>cd</code> command, see manpage of <code>builtins</code>(7). </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The default pager of the bare bone Debian system is <code>more</code> which cannot scroll back. By installing <code>less</code> package using command line "<code>aptitude install less</code>", <code>less</code> becomes default pager and you can scroll back with cursor keys. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The "<code>[</code>" and "<code>]</code>" in the regular expression of the "<code>ps aux | grep -e "[e]xim4*"</code>" command above enable <code>grep</code> to avoid matching itself. The "<code>4*</code>" in the regular expression means 0 or more repeats of character "<code>4</code>" thus enables <code>grep</code> to match both "<code>exim</code>" and "<code>exim4</code>". Although "<code>*</code>" is used in the shell filename glob and the regular expression, their meanings are different in the regular expression. Learn the regular expression from the manpage of <code>grep</code>(1). </para>
<para>Please traverse directories and peek into the system using the above commands as training. If you have questions on any of the console commands, please make sure to read the manual page. For example, these commands are the good start: </para>
<screen><![CDATA[$ man man
$ man bash
$ man builtins
$ man grep
$ man ls
]]></screen>
<para>The style of man pages may be a little hard to get used to, because they are rather terse, particularly the older, very traditional ones. But once you get used to it, you come to appreciate their succinctness. </para>
<para>Please note that many Unix-like commands including ones from GNU and BSD will display brief help information if you invoke them in one of the following ways (or without any arguments in some cases): </para>
<screen><![CDATA[$ <commandname> --help
$ <commandname> -h
]]></screen>
<para/>
</section>
</section>
<section>
<title>The simple shell command</title>
<para>Now you have some feel on how to use the Debian system. Let's look deep into the mechanism of the command execution in the Debian system. Here, I have simplified reality for the newbie. See manpages for <code>bash</code>(1) for the exact explanation. </para>
<para>A simple command is a sequence of </para>
<orderedlist numeration="arabic">
<listitem> variable assignments (optional) </listitem>
<listitem> command name </listitem>
<listitem> arguments (optional) </listitem>
<listitem>
<para> redirections (optional: <code>></code> , <code>>></code> , <code><</code> , <code><<</code> , etc.) </para>
</listitem>
<listitem>
<para> control operator (optional: <code>&&</code> , <code>||</code> ; <newline> , <code>;</code> , <code>&</code> , <code>(</code> , <code>)</code> ) </para>
</listitem>
</orderedlist>
<para/>
<section>
<title>Command execution and environment variable</title>
<para>The values of some <ulink url="http://en.wikipedia.org/wiki/Environment_variable">environment variables</ulink> change the behavior of some Unix commands. </para>
<para>The default values of environment variables are initially set by the PAM system and then some of them may be reset by some application programs: </para>
<itemizedlist>
<listitem>
<para>the display manager such as <code>gdm</code>, and </para>
</listitem>
<listitem>
<para>the shell in its start up codes <code>bash_profile</code> and <code>.bashrc</code>. </para>
</listitem>
</itemizedlist>
<para/>
<section>
<title>LANG variable</title>
<para>The full locale value given to <code>LANG</code> variable consists of 3 parts: <code>xx_YY.ZZZZ</code>. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The 3 parts of locale value. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">locale value</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>xx</code>
</para>
</entry>
<entry>
<para>
<ulink url="http://en.wikipedia.org/wiki/ISO_639">ISO 639 language codes (lower case) such as "en"</ulink>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>YY</code>
</para>
</entry>
<entry>
<para><ulink url="http://en.wikipedia.org/wiki/ISO_3166-3">ISO 3166 country codes (upper case) such as "US"</ulink> </para>
</entry>
</row>
<row>
<entry>
<para>
<code>ZZZZ</code>
</para>
</entry>
<entry>
<para>
<ulink url="http://en.wikipedia.org/wiki/Codeset">codeset, always set to "UTF-8"</ulink>
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>For the language codes and country codes, see pertinent description in the <code>info gettext</code>. </para>
<para>For the codeset on the modern Debian system, you should always set it to <emphasis role="strong"><code>UTF-8</code></emphasis> unless you specifically want to use the historic one with good reason and background knowledge. </para>
<para>For fine details of the locale configuration, see: @{@thelocale@}@ . </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> The "<code>LANG=en_US</code>" is not "<code>LANG=C</code>" nor "<code>LANG=en_US.UTF-8</code>". It is "<code>LANG=en_US.ISO-8859-1</code>" (see: @{@basicsofencoding@}@). </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of locale recommendations. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">Language (area)</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">locale recommendation</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> English(USA) </para>
</entry>
<entry>
<para>
<code>en_US.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> English(Great_Britain) </para>
</entry>
<entry>
<para>
<code>en_GB.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> French(France) </para>
</entry>
<entry>
<para>
<code>fr_FR.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> German(Germany) </para>
</entry>
<entry>
<para>
<code>de_DE.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Italian(Italy) </para>
</entry>
<entry>
<para>
<code>it_IT.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Spanish(Spain) </para>
</entry>
<entry>
<para>
<code>es_ES.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Catalan(Spain) </para>
</entry>
<entry>
<para>
<code>ca_ES.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Swedish(Sweden) </para>
</entry>
<entry>
<para>
<code>sv_SE.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Portuguese(Brasil) </para>
</entry>
<entry>
<para>
<code>pt_BR.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Russian(Russia) </para>
</entry>
<entry>
<para>
<code>ru_RU.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Chinese(P.R._of_China) </para>
</entry>
<entry>
<para>
<code>zh_CN.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Chinese(Taiwan_R.O.C.) </para>
</entry>
<entry>
<para>
<code>zh_TW.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Japanese(Japan) </para>
</entry>
<entry>
<para>
<code>ja_JP.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Korean(Republic_of_Korea) </para>
</entry>
<entry>
<para>
<code>ko_KR.UTF-8</code>
</para>
</entry>
</row>
<row>
<entry>
<para> Vietnamese(Vietnam) </para>
</entry>
<entry>
<para>
<code>vi_VN.UTF-8</code>
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>Typical command execution uses a shell line sequence like the following: </para>
<screen><![CDATA[$ date
Sun Jun 3 10:27:39 JST 2007
$ LANG=fr_FR.UTF-8 date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)
]]></screen>
<para>Here, the program <code>date</code> is executed in the foreground job. The environment variable "<code>LANG</code>" is: </para>
<itemizedlist>
<listitem>
<para>system default <ulink url="http://en.wikipedia.org/wiki/Locale">locale</ulink> (such as "<code>en_US.UTF-8</code>" depending on your configuration) for the first command </para>
</listitem>
<listitem>
<para>set to "<code>fr_FR.UTF-8</code>" (French UTF-8 <ulink url="http://en.wikipedia.org/wiki/Locale">locale</ulink> assuming it is available on your system) for the second command </para>
</listitem>
</itemizedlist>
<para>Most command executions usually do not have preceding environment variable definition. For the above example, you can alternatively execute: </para>
<screen><![CDATA[$ LANG=fr_FR.UTF-8
$ date
dimanche 3 juin 2007, 10:27:33 (UTC+0900)
]]></screen>
<para>As you can see here, the output of command is affected by the environment variable to produce French output. If you want the environment variable to be inherited to the subprocesses (e.g., when calling shell script), you need to "export" it instead by using: </para>
<screen><![CDATA[$ export LANG
]]></screen>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> When filing a bug report, running the command under "<code>LANG=en_US.UTF-8</code>" is good idea if you use non-English environment. </para>
<para>See <code>locale</code>(5) and <code>locale</code>(7) for <code>LANG</code> and related environment variables. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> I recommend you to configure the system environment just by the <code>LANG</code> variable and to stay away from <code>LC_*</code> variables unless it is absolutely needed. </para>
<para/>
</section>
<section>
<title>PATH variable</title>
<para>When you type a command into the shell, the shell searches the command in the list of directories contained in the <code>PATH</code> environment variable. The value of the <code>PATH</code> environment variable is also called the shell's search path. </para>
<para>In the default Debian installation, the <code>PATH</code> environment variable of user accounts may not include <code>/sbin/</code>. For example, The <code>ifconfig</code> command needs to be issued with full path as <code>/sbin/ifconfig</code>. </para>
<para>You can change the <code>PATH</code> environment variable by <code>~/.bash_profile</code> or <code>~/.bashrc</code> files. </para>
<para/>
</section>
<section>
<title>HOME variable</title>
<para>Many commands stores user specific configuration in the home directory and changes their behavior by their contents. The home directory is identified by the environment variable: <code>HOME</code>: </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> List of <code>HOME</code> values. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">situation</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">value of <code>HOME</code></emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para> program run by the init process (daemon) </para>
</entry>
<entry>
<para>
<code>/</code>
</para>
</entry>
</row>
<row>
<entry>
<para> program run from the normal root shell </para>
</entry>
<entry>
<para>
<code>/root</code>
</para>
</entry>
</row>
<row>
<entry>
<para> program run from the normal user shell </para>
</entry>
<entry>
<para>
<code>/home/<normal_user></code>
</para>
</entry>
</row>
<row>
<entry>
<para> program run from the normal user GUI desktop menu </para>
</entry>
<entry>
<para>
<code>/home/<normal_user></code>
</para>
</entry>
</row>
<row>
<entry>
<para> program run as root with "<code>sudo program</code>" </para>
</entry>
<entry>
<para>
<code>/home/<normal_user></code>
</para>
</entry>
</row>
<row>
<entry>
<para> program run as root with "<code>sudo -H program</code>" </para>
</entry>
<entry>
<para>
<code>/root</code>
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para/>
</section>
</section>
<section>
<title>Command line options</title>
<para>Some commands take arguments. Arguments starting with "<code>-</code>" or "<code>--</code>" are called options and control the behavior of the command. </para>
<screen><![CDATA[$ date
Mon Oct 27 23:02:09 CET 2003
$ date -R
Mon, 27 Oct 2003 23:02:40 +0100
]]></screen>
<para>Here the command-line argument "<code>-R</code>" changes the <code>date</code> command behavior to output RFC-2822 compliant date string. </para>
<para/>
</section>
<section>
<title>Shell glob</title>
<para>Often you want a command to work with a group of files without typing all of them. The filename expansion pattern using the shell <emphasis role="strong">glob</emphasis>, (sometimes referred as <emphasis role="strong">wildcards</emphasis>), facilitate this need. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The shell glob patterns. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">shell glob pattern</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">match</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>*</code>
</para>
</entry>
<entry>
<para> This matches filename (segment) not started with "<code>.</code>". </para>
</entry>
</row>
<row>
<entry>
<para>
<code>.*</code>
</para>
</entry>
<entry>
<para> This matches filename (segment) started with "<code>.</code>". </para>
</entry>
</row>
<row>
<entry>
<para>
<code>?</code>
</para>
</entry>
<entry>
<para> This matches exactly one character. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>[...]</code>
</para>
</entry>
<entry>
<para> This matches exactly one character with any character enclosed in brackets. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>[a-z]</code>
</para>
</entry>
<entry>
<para> This matches exactly one character with any character between "<code>a</code>" and "<code>z</code>". </para>
</entry>
</row>
<row>
<entry>
<para>
<code>[^...]</code>
</para>
</entry>
<entry>
<para> This matches exactly one character other than any character enclosed in brackets (excluding "<code>^</code>"). </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>For example, try the following and think for yourself: </para>
<screen><![CDATA[$ mkdir junk; cd junk; .[^.]*touch 1.txt 2.txt 3.c 4.h .5.txt ..6.txt
$ echo *.txt
1.txt 2.txt
$ echo *
1.txt 2.txt 3.c 4.h
$ echo *.[hc]
3.c 4.h
$ echo .*
. .. .5.txt ..6.txt
$ echo .*[^.]*
.5.txt ..6.txt
$ echo [^1-3]*
4.h
$ cd ..; rm -rf junk
]]></screen>
<para>See "<code>man 7 glob</code>" for more. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Unlike normal filename expansion by the shell, the shell pattern "<code>*</code>" tested in the <code>find</code> command with <code>-name</code> test etc., matches the initial "<code>.</code>" of the filename. (New <ulink url="http://en.wikipedia.org/wiki/POSIX">POSIX</ulink> feature) </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> BASH can be tweaked to change its glob behavior with its shopt builtin options such as dotglob, noglob, nocaseglob, nullglob, nocaseglob, extglob, etc. See <code>bash</code>(1). </para>
<para/>
</section>
<section>
<title>Return value of the command</title>
<para>Each command returns its exit status as the return value. </para>
<table>
<caption/>
<tgroup cols="3">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<tbody>
<row>
<entry>
<para> Command exit code. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">command exit state</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">numeric return value</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">logical return value</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para><code>command</code> executed successfully. </para>
</entry>
<entry>
<para><code>$?</code> = 0 </para>
</entry>
<entry>
<para>
<emphasis role="strong">TRUE</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para><code>command</code> exited with error. </para>
</entry>
<entry>
<para><code>$?</code> != 0 </para>
</entry>
<entry>
<para>
<emphasis role="strong">FALSE</emphasis>
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>Thus: </para>
<screen><![CDATA[$ [ 1 = 1 ] ; echo $?
0
$ [ 1 = 2 ] ; echo $?
1
]]></screen>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Please note that, in the logical context for the shell, <emphasis role="strong">success</emphasis> is treated as the logical <emphasis role="strong">TRUE</emphasis> which has 0 (zero) as its value. This is somewhat non-intuitive and needs to be reminded here. </para>
<para/>
</section>
<section>
<title>Typical command sequences and shell redirection</title>
<para>Let's try to remember following shell command idioms. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The shell command idioms. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para><emphasis role="strong">command idiom</emphasis> (type in one line) </para>
</entry>
<entry>
<para>
<emphasis role="strong">effects</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command &</code>
</para>
</entry>
<entry>
<para>The <code>command</code> is executed in the subshell in the <emphasis role="strong">background</emphasis>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>command1 | command2</code>
</para>
</entry>
<entry>
<para> The standard output of <code>command1</code> is piped to the standard input of <code>command2</code> . Both commands may be running <emphasis role="strong">concurrently</emphasis>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command1 2>&1 | command2</code>
</para>
</entry>
<entry>
<para> Both standard output and standard error of <code>command1</code> are piped to the standard input of <code>command2</code>. Both commands may be running <emphasis role="strong">concurrently</emphasis>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command1 ; command2</code>
</para>
</entry>
<entry>
<para> The <code>command1</code> and <code>command2</code> are executed <emphasis role="strong">sequentially</emphasis>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command1 && command2</code>
</para>
</entry>
<entry>
<para> The <code>command1</code> is executed. If successful, <code>command2</code> is also executed <emphasis role="strong">sequentially</emphasis>. Return success if both <code>command1</code> <emphasis role="strong">and</emphasis> <code>command2</code> are successful.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command1 || command2</code>
</para>
</entry>
<entry>
<para> The <code>command1</code> is executed. If not successful, <code>command2</code> is also executed <emphasis role="strong">sequentially</emphasis>. Return success if <code>command1</code> <emphasis role="strong">or</emphasis> <code>command2</code> are successful.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command > foo</code>
</para>
</entry>
<entry>
<para>Redirect standard output of <code>command</code> to a file <code>foo</code>. (overwrite)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command 2> foo</code>
</para>
</entry>
<entry>
<para>Redirect standard error of <code>command</code> to a file <code>foo</code>. (overwrite)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command >> foo</code>
</para>
</entry>
<entry>
<para> Redirect standard output of <code>command</code> to a file <code>foo</code>. (append)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command 2>> foo</code>
</para>
</entry>
<entry>
<para> Redirect standard error of <code>command</code> to a file <code>foo</code>. (append)</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command > foo 2>&1</code>
</para>
</entry>
<entry>
<para> Redirect both standard output and standard error of <code>command</code> to a file <code>foo</code>.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>command < foo</code>
</para>
</entry>
<entry>
<para> Redirect standard input of <code>command</code> to a file <code>foo</code>. </para>
</entry>
</row>
<row>
<entry>
<para>
<code>command << delimiter</code>
</para>
</entry>
<entry>
<para> Redirect standard input of <code>command</code> to the following lines until <code>delimiter</code> is met. (Here documents) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>command <<- delimiter</code>
</para>
</entry>
<entry>
<para> Redirect standard input of <code>command</code> to the following lines until <code>delimiter</code> is met. The leading tab characters are stripped from input lines. (Here documents) </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>The Debian system is a multi-tasking system. Background jobs allow users to run multiple programs in a single shell. The management of the background process involves the shell built-ins: <code>jobs</code>, <code>fg</code>, <code>bg</code>, and <code>kill</code>. Please read the sections of the bash(1) manpage under "SIGNALS", and "JOB CONTROL", and the <code>builtins</code>(1) manpage. </para>
<para>Let's try simple examples of redirection: </para>
<screen><![CDATA[$ </etc/motd pager
]]></screen>
<para/>
<screen><![CDATA[$ pager </etc/motd
]]></screen>
<para/>
<screen><![CDATA[$ pager /etc/motd
]]></screen>
<para/>
<screen><![CDATA[$ cat /etc/motd | pager
]]></screen>
<para>Although all 4 examples display the same thing, the last example runs an extra <code>cat</code> command and wastes resources with no reason. </para>
<para>The shell allows you to open files using the <code>exec</code> built-in with an arbitrary file descriptor. </para>
<screen><![CDATA[$ echo Hello >foo
$ exec 3<foo 4>bar # open files
$ cat <&3 >&4 # redirect stdin to 3, stdout to 4
$ exec 3<&- 4>&- # close files
$ cat bar
Hello
]]></screen>
<para>Here, "n<code><&-</code>" and "n<code>>&-</code>" mean to close the file descriptor "n". </para>
<para>The file descriptor 0-2 are predefined: </para>
<table>
<caption/>
<tgroup cols="3">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<tbody>
<row>
<entry>
<para> The predefined file descriptors. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">device</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">description</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">file descriptor</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>stdin</code>
</para>
</entry>
<entry>
<para> standard input </para>
</entry>
<entry>
<para> 0 </para>
</entry>
</row>
<row>
<entry>
<para>
<code>stdout</code>
</para>
</entry>
<entry>
<para> standard output </para>
</entry>
<entry>
<para> 1 </para>
</entry>
</row>
<row>
<entry>
<para>
<code>stderr</code>
</para>
</entry>
<entry>
<para> standard error </para>
</entry>
<entry>
<para> 2 </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para/>
</section>
<section>
<title>Command alias</title>
<para>You can set an alias for the frequently used command. For example: </para>
<screen><![CDATA[$ alias la='ls -la'
]]></screen>
<para>Now, <code>la</code> works as a short hand for "<code>ls -la</code>" which lists all files in the long listing format. </para>
<para>You can list any existing aliases: </para>
<screen><![CDATA[$ alias
]]></screen>
<para>You can identity exact path or identity of the command using <code>type</code> command. For example: </para>
<screen><![CDATA[$ type ls
ls is hashed (/bin/ls)
$ type la
la is aliased to ls -la
$ type echo
echo is a shell builtin
$ type file
file is /usr/bin/file
]]></screen>
<para>Here <code>ls</code> was recently searched while <code>file</code> was not, thus <code>ls</code> is "hashed", i.e., the shell has an internal record for the quick access to the location of the <code>ls</code> command. </para>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> See @{@colorizedcommands@}@. </para>
<para/>
</section>
</section>
<section>
<title>Unix-like text processing</title>
<para>In Unix-like work environment, text processing is done by piping text through chains of standard text processing tools. This was another crucial Unix innovation. </para>
<para/>
<section>
<title>Unix text tools</title>
<para>There are few standard text processing tools which are used very often on the Unix-like system. </para>
<itemizedlist>
<listitem>No regular expression is used: <itemizedlist><listitem><para><code>cat</code>(1) concatenates files and outputs the whole content. </para></listitem><listitem><para><code>tac</code>(1) concatenates files and outputs in reverse. </para></listitem><listitem><para><code>cut</code>(1) selects parts of lines and outputs. </para></listitem><listitem><para><code>head</code>(1) outputs the first part of files. </para></listitem><listitem><para><code>tail</code>(1) outputs the last part of files. </para></listitem><listitem><para><code>sort</code>(1) sorts lines of text files. </para></listitem><listitem><para><code>uniq</code>(1) removes duplicate lines from a sorted file. </para></listitem><listitem><para><code>tr</code>(1) translates or deletes characters. </para></listitem><listitem><para><code>diff</code>(1) compares files line by line. </para></listitem></itemizedlist></listitem>
<listitem>
<para>Basic regular expression (<emphasis role="strong">BRE</emphasis>) is used: </para>
<itemizedlist>
<listitem>
<para><code>grep</code>(1) matches text with the pattern. </para>
</listitem>
<listitem>
<para><code>ed</code>(1) is a primitive line editor. </para>
</listitem>
<listitem>
<para><code>sed</code>(1) is a stream editor. </para>
</listitem>
<listitem>
<para><code>vim</code>(1) is a screen editor. </para>
</listitem>
<listitem>
<para><code>emacs</code>(1) is a screen editor. (somewhat extended <emphasis role="strong">BRE</emphasis>) </para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>Extended regular expression (<emphasis role="strong">ERE</emphasis>) is used: </para>
<itemizedlist>
<listitem>
<para><code>egrep</code>(1) matches text with pattern. </para>
</listitem>
<listitem>
<para><code>awk</code>(1) does simple text processing. </para>
</listitem>
<listitem>
<para><code>tcl</code> does every conceivable type of text processing: <code>re_syntax</code>(3). Often used with <code>tk</code>. </para>
</listitem>
<listitem>
<para><code>perl</code>(1) does text processing, is capable of network programming, and much more. <code>perlre</code>(1). </para>
</listitem>
<listitem>
<para><code>pcregrep</code>(1) from the <code>pcregrep</code> package matches text with <ulink url="http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions">Perl Compatible Regular Expressions (PCRE)</ulink> pattern. </para>
</listitem>
<listitem>
<para><code>python</code> with <code>re</code> module does every conceivable text processing. See <code>/usr/share/doc/python/html/index.html</code>. </para>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<para>If you are not sure what exactly these commands do, please use "<code>man command</code>" to figure it out by yourself. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Sort order and range expression are locale dependent. If you wish to obtain traditional behavior for a command, use <emphasis role="strong">C</emphasis> locale instead of <emphasis role="strong">UTF-8</emphasis> ones by prepnding command with "<code>LANG=C</code>" (see @{@langvariable@}@ and @{@thelocale@}@). </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject><ulink url="http://en.wikipedia.org/wiki/Perl">Perl</ulink> regular expressions (<code>perlre</code>(1)), <ulink url="http://en.wikipedia.org/wiki/Perl_Compatible_Regular_Expressions">Perl Compatible Regular Expressions (PCRE)</ulink>, and <ulink url="http://en.wikipedia.org/wiki/Python_(programming_language)">Python</ulink> regular expressions offered by <code>re</code> module have many common extensions to the normal <emphasis role="strong">ERE</emphasis>. </para>
<para/>
</section>
<section>
<title>Regular expressions</title>
<para><ulink url="http://en.wikipedia.org/wiki/Regular_expression">Regular expressions</ulink> are used in many text processing tools. They are analogous to the shell globs, but they are both more complicated and more powerful. </para>
<para>The regular expression describes the matching pattern and is made up of text characters and <emphasis role="strong">metacharacters</emphasis>. </para>
<para>The <emphasis role="strong">metacharacter</emphasis> is just a character with a special meaning. There are 2 major styles, <emphasis role="strong">BRE</emphasis> and <emphasis role="strong">ERE</emphasis>, depending on the text tools as described above. </para>
<table>
<caption/>
<tgroup cols="3">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<tbody>
<row>
<entry>
<para> The metacharacters for BRE and ERE. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">BRE</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">ERE</emphasis>
</para>
</entry>
<entry>
<para>The meaning of the regular expression</para>
</entry>
</row>
<row>
<entry>
<para>
<code> \ . [ ] ^ $ * </code>
</para>
</entry>
<entry>
<para>
<code> \ . [ ] ^ $ * </code>
</para>
</entry>
<entry>
<para> common <emphasis role="strong">metacharacters</emphasis> </para>
</entry>
</row>
<row>
<entry>
<para>
<code> \+ \? \( \) \{ \} \| </code>
</para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> BRE only "<code>\</code>" quoted <emphasis role="strong">metacharacters</emphasis></para>
</entry>
</row>
<row>
<entry>
<para> </para>
</entry>
<entry>
<para>
<code> + ? ( ) { } | </code>
</para>
</entry>
<entry>
<para> ERE only non-"<code>\</code>" quoted <emphasis role="strong">metacharacters</emphasis> </para>
</entry>
</row>
<row>
<entry>
<para>
<code>c</code>
</para>
</entry>
<entry>
<para>
<code>c</code>
</para>
</entry>
<entry>
<para> This matches the <emphasis role="strong">non-metacharacter</emphasis> "<code>c</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\c</code>
</para>
</entry>
<entry>
<para>
<code>\c</code>
</para>
</entry>
<entry>
<para> This sequence matches the literal character "<code>c</code>" even if "<code>c</code>" is <emphasis role="strong">metacharacter</emphasis> by itself.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>.</code>
</para>
</entry>
<entry>
<para>
<code>.</code>
</para>
</entry>
<entry>
<para> This matches any character including newline.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>^</code>
</para>
</entry>
<entry>
<para>
<code>^</code>
</para>
</entry>
<entry>
<para> This matches the beginning of a string.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>$</code>
</para>
</entry>
<entry>
<para>
<code>$</code>
</para>
</entry>
<entry>
<para> This matches the end of a string.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\<</code>
</para>
</entry>
<entry>
<para>
<code>\<</code>
</para>
</entry>
<entry>
<para> This matches the beginning of a word.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\></code>
</para>
</entry>
<entry>
<para>
<code>\></code>
</para>
</entry>
<entry>
<para> This matches the end of a word.</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\[abc...\]</code>
</para>
</entry>
<entry>
<para>
<code>[abc...]</code>
</para>
</entry>
<entry>
<para> This character list matches any of the characters "<code>abc...</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\[^abc...\]</code>
</para>
</entry>
<entry>
<para>
<code>[^abc...]</code>
</para>
</entry>
<entry>
<para> This negated character list matches any of the characters except "<code>abc...</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>r*</code>
</para>
</entry>
<entry>
<para>
<code>r*</code>
</para>
</entry>
<entry>
<para> This matches zero or more regular expressions identified by "<code>r</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>r\+</code>
</para>
</entry>
<entry>
<para>
<code>r+</code>
</para>
</entry>
<entry>
<para> This matches one or more regular expressions identified by "<code>r</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>r\?</code>
</para>
</entry>
<entry>
<para>
<code>r?</code>
</para>
</entry>
<entry>
<para> This matches zero or one regular expressions identified by "<code>r</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>r1\|r2</code>
</para>
</entry>
<entry>
<para>
<code>r1|r2</code>
</para>
</entry>
<entry>
<para> This matches one of the regular expressions identified by "<code>r1</code>" or "<code>r2</code>".</para>
</entry>
</row>
<row>
<entry>
<para>
<code>\(r1\|r2\)</code>
</para>
</entry>
<entry>
<para>
<code>(r1|r2)</code>
</para>
</entry>
<entry>
<para> This matches one of the regular expressions identified by "<code>r1</code>" or "<code>r2</code>" and treats it as a <emphasis role="strong">bracketed</emphasis> regular expression.</para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>The regular expression of <emphasis role="strong"><code>emacs</code></emphasis> is basically <emphasis role="strong">BRE</emphasis> but has been extended to treat "<code>+</code>"and "<code>?</code>" as the <emphasis role="strong">metacharacters</emphasis> as in <emphasis role="strong">ERE</emphasis>. Thus, there are no needs to quote them with "<code>\</code>" in the regular expression of <code>emacs</code>. </para>
<para>For example, <code>grep</code> can be used to perform the text search using the regular expression: </para>
<para/>
<screen><![CDATA[$ egrep 'GNU.*LICENSE|Yoyodyne' /usr/share/common-licenses/GPL
GNU GENERAL PUBLIC LICENSE
GNU GENERAL PUBLIC LICENSE
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
]]></screen>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> See @{@colorizedcommands@}@. </para>
<para/>
</section>
<section>
<title>Replacement expressions</title>
<para>For the replacement expression, following characters have special meanings: </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The replacement expression. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">character</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">meaning</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code> & </code>
</para>
</entry>
<entry>
<para> This represents what the regular expression matched. (use <code> \& </code> in <code>emacs</code>) </para>
</entry>
</row>
<row>
<entry>
<para>
<code> \n </code>
</para>
</entry>
<entry>
<para> This represents what the n-th _bracketed_ regular expression matched. ("n" being number) </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>For Perl replacement string, <code> $n </code> is used instead of <code> \n </code> and <code> & </code> has no special meaning. </para>
<para>For example: </para>
<screen><![CDATA[$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/=&=/'
zzz=1abc2efg3hij4=
$ echo zzz1abc2efg3hij4 | \
sed -e 's/\(1[a-z]*\)[0-9]*\(.*\)$/\2===\1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/$2===$1/'
zzzefg3hij4===1abc
$ echo zzz1abc2efg3hij4 | \
perl -pe 's/(1[a-z]*)[0-9]*(.*)$/=&=/'
zzz=&=
]]></screen>
<para>Here please pay extra attention to the style of the <emphasis role="strong">bracketed</emphasis> regular expression and how the matched strings are used in the text replacement process on different tools. </para>
<para>These regular expressions can be used for the cursor movements and the text replacement actions in the editors too. </para>
<para>The back slash "<code>\</code>" at the end of line in the shell commandline escapes newline as a white space character and continues shell command line input to the next line. </para>
<para>Please read all the related manual pages to learn these commands. </para>
<para/>
</section>
<section>
<title>Extract data from text file table</title>
<para>Let's consider a text file called <code>DPL</code> in which some pre-2004 Debian project leader's names and their initiation days are listed in a space-separated format. </para>
<para/>
<screen><![CDATA[Ian Murdock August 1993
Bruce Perens April 1996
Ian Jackson January 1998
Wichert Akkerman January 1999
Ben Collins April 2001
Bdale Garbee April 2002
Martin Michlmayr March 2003
]]></screen>
<para/>
<para/>
<para/>
<para><inlinemediaobject><imageobject><imagedata width="16" fileref="/htdocs/rightsidebar/img/icon-info.png" depth="16"/></imageobject><textobject><phrase>{i}</phrase></textobject></inlinemediaobject> See <ulink url="http://www.debian.org/doc/manuals/project-history/index.en.html">"A Brief History of Debian"</ulink> for the latest <ulink url="http://www.debian.org/doc/manuals/project-history/ch-leaders.en.html">Debian leadership history</ulink>. </para>
<para>Awk is frequently used to extract data from these types of files. </para>
<para/>
<screen><![CDATA[$ awk '{ print $3 }' <DPL # month started
August
April
January
January
April
April
March
$ awk '($1=="Ian") { print }' <DPL # DPL called Ian
Ian Murdock August 1993
Ian Jackson January 1998
$ awk '($2=="Perens") { print $3,$4 }' <DPL # When Perens started
April 1996
]]></screen>
<para>Shells such as Bash can be also used to parse this kind of file: </para>
<para/>
<screen><![CDATA[$ while read first last month year; do
echo $month
done <DPL
]]></screen>
<itemizedlist>
<listitem>same output as the first Awk example. </listitem>
</itemizedlist>
<para>Here, <code>read</code> built-in command uses the characters in $IFS (internal field separators) to split lines into words. </para>
<para>If you change IFS to "<code>:</code>", you can parse <code>/etc/passwd</code> with shell nicely: </para>
<para/>
<screen><![CDATA[$ oldIFS="$IFS" # save old value
$ IFS=":"
$ while read user password uid gid rest_of_line; do
if [ "$user" = "osamu" ]; then
echo "$user's ID is $uid"
fi
done < /etc/passwd
osamu's ID is 1000
$ IFS="$oldIFS" # restore old value
]]></screen>
<para>(If Awk is used to do the equivalent, use "<code>FS=":"</code>" to set the field separator.) </para>
<para>IFS is also used by the shell to split results of parameter expansion, command substitution, and arithmetic expansion. These do not occur within double or single quoted words. The default value of IFS is <space>, <tab>, and <newline> combined. </para>
<para>Be careful about using this shell IFS tricks. Strange things may happen, when shell interprets some parts of the script as its <emphasis role="strong">input</emphasis>. </para>
<para/>
<screen><![CDATA[$ IFS=":," # use ":" and "," as IFS
$ echo IFS=$IFS, IFS="$IFS" # echo is a Bash built-in
IFS= , IFS=:,
$ date -R # just a command output
Sat, 23 Aug 2003 08:30:15 +0200
$ echo $(date -R) # sub shell --> input to main shell
Sat 23 Aug 2003 08 30 36 +0200
$ unset IFS # reset IFS to the default
$ echo $(date -R)
Sat, 23 Aug 2003 08:30:50 +0200
]]></screen>
<para/>
</section>
<section>
<title>Script snippets for piping commands</title>
<para>The following scripts will do nice things as a part of a pipe. </para>
<table>
<caption/>
<tgroup cols="2">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<tbody>
<row>
<entry>
<para> The script snippets for piping commands. </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para><emphasis role="strong">script snippet</emphasis> (type in one line) </para>
</entry>
<entry>
<para>
<emphasis role="strong">effect</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>find /usr -print</code>
</para>
</entry>
<entry>
<para> find all files under <code>/usr</code> (see @{@idiomsfortheselectionoffiles@}@) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>seq 1 100</code>
</para>
</entry>
<entry>
<para> print 1 to 100 </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| xargs -n 1 <command></code>
</para>
</entry>
<entry>
<para> run command repeatedly with each item from pipe as its argument (see @{@repeatingacommandloopingoverfiles@}@) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| xargs -n 1 echo</code>
</para>
</entry>
<entry>
<para> split white-space-separated items from pipe into lines </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| xargs echo</code>
</para>
</entry>
<entry>
<para> merge all lines from pipe into a line </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| grep -e <regex_pattern></code>
</para>
</entry>
<entry>
<para> extract lines from pipe containing <regex_pattern> </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| grep -v -e <regex_pattern></code>
</para>
</entry>
<entry>
<para> extract lines from pipe not containing <regex_pattern> </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| cut -d: -f3 -</code>
</para>
</entry>
<entry>
<para> extract third field from pipe separated by "<code>:</code>" (passwd file etc.) </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| awk '{ print $3 }'</code>
</para>
</entry>
<entry>
<para> extract third field from pipe separated by whitespaces </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| awk -F'\t' '{ print $3 }'</code>
</para>
</entry>
<entry>
<para> extract third field from pipe separated by tab </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| col -bx</code>
</para>
</entry>
<entry>
<para> remove backspace and expand tabs to spaces </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| expand -</code>
</para>
</entry>
<entry>
<para> expand tabs </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| sort| uniq</code>
</para>
</entry>
<entry>
<para> sort and remove duplicates </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| tr 'A-Z' 'a-z'</code>
</para>
</entry>
<entry>
<para> convert uppercase to lowercase </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| tr -d '\n'</code>
</para>
</entry>
<entry>
<para> concatenate lines into one line </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| tr -d '\r'</code>
</para>
</entry>
<entry>
<para> remove CR </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| sed 's/^/# /'</code>
</para>
</entry>
<entry>
<para> add "<code>#</code>" to the start of each line </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| sed 's/\.ext//g'</code>
</para>
</entry>
<entry>
<para> remove "<code>.ext</code>" </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| sed -n -e 2p</code>
</para>
</entry>
<entry>
<para> print the second line </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| head -n 2 -</code>
</para>
</entry>
<entry>
<para> print the first 2 lines </para>
</entry>
</row>
<row>
<entry>
<para>
<code>| tail -n 2 -</code>
</para>
</entry>
<entry>
<para> print the last 2 lines </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>When using the shell interactive mode becomes too complicated, please consider to write a shell script (see: @{@theshellscript@}@). </para>
<para/>
</section>
<section>
<title>Perl one liner for the regular-expression substitution</title>
<para>The following execution of <code>perl</code>(1) one liner command will replace all instances of FROM_REGEX with TO_TEXT in all of the files <target_file> ...: </para>
<para/>
<screen><![CDATA[$ perl -i -p -e 's/FROM_REGEX/TO_TEXT/g;' <target_file> ...
]]></screen>
<para>"<code>-i</code>" is for "in-place editing", "<code>-p</code>" is for "implicit loop over <target_file> ...". If the substitution is complex, you can make recovery from errors easier by using the parameter "<code>-i.bak</code>" instead of "<code>-i</code>"; this will keep each original file, adding "<code>.bak</code>" as a file extension. </para>
<para><inlinemediaobject><imageobject><imagedata width="15" fileref="/htdocs/rightsidebar/img/idea.png" depth="15"/></imageobject><textobject><phrase>(!)</phrase></textobject></inlinemediaobject> Although this is somewhat waste of the resource, this is used frequently to change file contents across the whole directly with minimal typing. </para>
<para>You can do the similar with <code>ed</code>(1) command too. </para>
<screen><![CDATA[$ ed <target_file> <<EOF
,s/FROM_REGEX/TO_TEXT/g
w
q
EOF
]]></screen>
<para>Here, the <code>ed</code> commands are practically the same command as the <code>vi</code> command-mode command. </para>
<table>
<caption/>
<tgroup cols="5">
<colspec colname="xxx1"/>
<colspec colname="xxx2"/>
<colspec colname="xxx3"/>
<colspec colname="xxx4"/>
<colspec colname="xxx5"/>
<tbody>
<row>
<entry>
<para> The comparison of <code>ed</code> vs <code>perl</code> for in-place editing. </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
<entry>
<para> </para>
</entry>
</row>
<row>
<entry>
<para>
<emphasis role="strong">command</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">type</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">argument</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">regex</emphasis>
</para>
</entry>
<entry>
<para>
<emphasis role="strong">script</emphasis>
</para>
</entry>
</row>
<row>
<entry>
<para>
<code>ed</code>
</para>
</entry>
<entry>
<para> lighter and faster </para>
</entry>
<entry>
<para> works on one file </para>
</entry>
<entry>
<para> BRE </para>
</entry>
<entry>
<para> read from <code>stdin</code> </para>
</entry>
</row>
<row>
<entry>
<para>
<code>perl</code>
</para>
</entry>
<entry>
<para> heavier and slower </para>
</entry>
<entry>
<para> works on multiple files </para>
</entry>
<entry>
<para> ERE </para>
</entry>
<entry>
<para> can be as a part of the argument </para>
</entry>
</row>
</tbody>
</tgroup>
</table>
</section>
</section>
</section>
</article>
|