| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | <HTML |
| 3 | ><HEAD |
| 4 | ><TITLE |
| 5 | >External Filters, Programs and Commands</TITLE |
| 6 | ><META |
| 7 | NAME="GENERATOR" |
| 8 | CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ |
| 9 | "><LINK |
| 10 | REL="HOME" |
| 11 | TITLE="Advanced Bash-Scripting Guide" |
| 12 | HREF="index.html"><LINK |
| 13 | REL="UP" |
| 14 | TITLE="Commands" |
| 15 | HREF="part4.html"><LINK |
| 16 | REL="PREVIOUS" |
| 17 | TITLE="Internal Commands and Builtins" |
| 18 | HREF="internal.html"><LINK |
| 19 | REL="NEXT" |
| 20 | TITLE="Complex Commands" |
| 21 | HREF="moreadv.html"><META |
| 22 | HTTP-EQUIV="Content-Style-Type" |
| 23 | CONTENT="text/css"><LINK |
| 24 | REL="stylesheet" |
| 25 | HREF="common/kde-common.css" |
| 26 | TYPE="text/css"><META |
| 27 | HTTP-EQUIV="Content-Type" |
| 28 | CONTENT="text/html; charset=iso-8859-1"><META |
| 29 | HTTP-EQUIV="Content-Language" |
| 30 | CONTENT="en"><LINK |
| 31 | REL="stylesheet" |
| 32 | HREF="common/kde-localised.css" |
| 33 | TYPE="text/css" |
| 34 | TITLE="KDE-English"><LINK |
| 35 | REL="stylesheet" |
| 36 | HREF="common/kde-default.css" |
| 37 | TYPE="text/css" |
| 38 | TITLE="KDE-Default"></HEAD |
| 39 | ><BODY |
| 40 | CLASS="CHAPTER" |
| 41 | BGCOLOR="#FFFFFF" |
| 42 | TEXT="#000000" |
| 43 | LINK="#AA0000" |
| 44 | VLINK="#AA0055" |
| 45 | ALINK="#AA0000" |
| 46 | STYLE="font-family: sans-serif;" |
| 47 | ><DIV |
| 48 | CLASS="NAVHEADER" |
| 49 | ><TABLE |
| 50 | SUMMARY="Header navigation table" |
| 51 | WIDTH="100%" |
| 52 | BORDER="0" |
| 53 | CELLPADDING="0" |
| 54 | CELLSPACING="0" |
| 55 | ><TR |
| 56 | ><TH |
| 57 | COLSPAN="3" |
| 58 | ALIGN="center" |
| 59 | >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH |
| 60 | ></TR |
| 61 | ><TR |
| 62 | ><TD |
| 63 | WIDTH="10%" |
| 64 | ALIGN="left" |
| 65 | VALIGN="bottom" |
| 66 | ><A |
| 67 | HREF="internal.html" |
| 68 | ACCESSKEY="P" |
| 69 | >Prev</A |
| 70 | ></TD |
| 71 | ><TD |
| 72 | WIDTH="80%" |
| 73 | ALIGN="center" |
| 74 | VALIGN="bottom" |
| 75 | ></TD |
| 76 | ><TD |
| 77 | WIDTH="10%" |
| 78 | ALIGN="right" |
| 79 | VALIGN="bottom" |
| 80 | ><A |
| 81 | HREF="moreadv.html" |
| 82 | ACCESSKEY="N" |
| 83 | >Next</A |
| 84 | ></TD |
| 85 | ></TR |
| 86 | ></TABLE |
| 87 | ><HR |
| 88 | ALIGN="LEFT" |
| 89 | WIDTH="100%"></DIV |
| 90 | ><DIV |
| 91 | CLASS="CHAPTER" |
| 92 | ><H1 |
| 93 | ><A |
| 94 | NAME="EXTERNAL" |
| 95 | ></A |
| 96 | >Chapter 16. External Filters, Programs and Commands</H1 |
| 97 | ><P |
| 98 | ><A |
| 99 | NAME="EXTERNALREF" |
| 100 | ></A |
| 101 | ></P |
| 102 | ><P |
| 103 | >Standard UNIX commands make shell scripts more versatile. The |
| 104 | power of scripts comes from coupling system commands and shell |
| 105 | directives with simple programming constructs.</P |
| 106 | ><DIV |
| 107 | CLASS="SECT1" |
| 108 | ><H1 |
| 109 | CLASS="SECT1" |
| 110 | ><A |
| 111 | NAME="BASIC" |
| 112 | ></A |
| 113 | >16.1. Basic Commands</H1 |
| 114 | ><DIV |
| 115 | CLASS="VARIABLELIST" |
| 116 | ><P |
| 117 | ><B |
| 118 | ><A |
| 119 | NAME="BASICCOMMANDS1" |
| 120 | ></A |
| 121 | >The first commands a novice learns</B |
| 122 | ></P |
| 123 | ><DL |
| 124 | ><DT |
| 125 | ><A |
| 126 | NAME="LSREF" |
| 127 | ></A |
| 128 | ><B |
| 129 | CLASS="COMMAND" |
| 130 | >ls</B |
| 131 | ></DT |
| 132 | ><DD |
| 133 | ><P |
| 134 | >The basic file <SPAN |
| 135 | CLASS="QUOTE" |
| 136 | >"list"</SPAN |
| 137 | > command. It is all too easy |
| 138 | to underestimate the power of this humble command. For |
| 139 | example, using the <TT |
| 140 | CLASS="OPTION" |
| 141 | >-R</TT |
| 142 | >, recursive option, |
| 143 | <B |
| 144 | CLASS="COMMAND" |
| 145 | >ls</B |
| 146 | > provides a tree-like listing of |
| 147 | a directory structure. Other useful options are |
| 148 | <TT |
| 149 | CLASS="OPTION" |
| 150 | >-S</TT |
| 151 | >, sort listing by file size, |
| 152 | <TT |
| 153 | CLASS="OPTION" |
| 154 | >-t</TT |
| 155 | >, sort by file modification time, |
| 156 | <TT |
| 157 | CLASS="OPTION" |
| 158 | >-v</TT |
| 159 | >, sort by (numerical) version numbers |
| 160 | embedded in the filenames, |
| 161 | <A |
| 162 | NAME="AEN10025" |
| 163 | HREF="#FTN.AEN10025" |
| 164 | >[1]</A |
| 165 | > |
| 166 | <TT |
| 167 | CLASS="OPTION" |
| 168 | >-b</TT |
| 169 | >, show escape characters, and |
| 170 | <TT |
| 171 | CLASS="OPTION" |
| 172 | >-i</TT |
| 173 | >, show file inodes (see <A |
| 174 | HREF="moreadv.html#IDELETE" |
| 175 | >Example 16-4</A |
| 176 | >).</P |
| 177 | ><P |
| 178 | > <TABLE |
| 179 | BORDER="0" |
| 180 | BGCOLOR="#E0E0E0" |
| 181 | WIDTH="90%" |
| 182 | ><TR |
| 183 | ><TD |
| 184 | ><PRE |
| 185 | CLASS="SCREEN" |
| 186 | > <TT |
| 187 | CLASS="PROMPT" |
| 188 | >bash$ </TT |
| 189 | ><TT |
| 190 | CLASS="USERINPUT" |
| 191 | ><B |
| 192 | >ls -l</B |
| 193 | ></TT |
| 194 | > |
| 195 | <TT |
| 196 | CLASS="COMPUTEROUTPUT" |
| 197 | >-rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt |
| 198 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt |
| 199 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt |
| 200 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt |
| 201 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt |
| 202 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt |
| 203 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt |
| 204 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt</TT |
| 205 | > |
| 206 | |
| 207 | |
| 208 | <TT |
| 209 | CLASS="PROMPT" |
| 210 | >bash$ </TT |
| 211 | ><TT |
| 212 | CLASS="USERINPUT" |
| 213 | ><B |
| 214 | >ls -lv</B |
| 215 | ></TT |
| 216 | > |
| 217 | <TT |
| 218 | CLASS="COMPUTEROUTPUT" |
| 219 | > total 0 |
| 220 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Chapter_headings.txt |
| 221 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:49 Preface.txt |
| 222 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter1.txt |
| 223 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter2.txt |
| 224 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter3.txt |
| 225 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter10.txt |
| 226 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter11.txt |
| 227 | -rw-rw-r-- 1 bozo bozo 0 Sep 14 18:44 chapter12.txt</TT |
| 228 | ></PRE |
| 229 | ></TD |
| 230 | ></TR |
| 231 | ></TABLE |
| 232 | ></P |
| 233 | ><DIV |
| 234 | CLASS="TIP" |
| 235 | ><TABLE |
| 236 | CLASS="TIP" |
| 237 | WIDTH="90%" |
| 238 | BORDER="0" |
| 239 | ><TR |
| 240 | ><TD |
| 241 | WIDTH="25" |
| 242 | ALIGN="CENTER" |
| 243 | VALIGN="TOP" |
| 244 | ><IMG |
| 245 | SRC="common/tip.png" |
| 246 | HSPACE="5" |
| 247 | ALT="Tip"></TD |
| 248 | ><TD |
| 249 | ALIGN="LEFT" |
| 250 | VALIGN="TOP" |
| 251 | ><P |
| 252 | > The <I |
| 253 | CLASS="FIRSTTERM" |
| 254 | >ls</I |
| 255 | > command returns a |
| 256 | non-zero <A |
| 257 | HREF="exit-status.html#EXITSTATUSREF" |
| 258 | >exit status</A |
| 259 | > when |
| 260 | attempting to list a non-existent file. |
| 261 | <TABLE |
| 262 | BORDER="0" |
| 263 | BGCOLOR="#E0E0E0" |
| 264 | WIDTH="90%" |
| 265 | ><TR |
| 266 | ><TD |
| 267 | ><PRE |
| 268 | CLASS="SCREEN" |
| 269 | > <TT |
| 270 | CLASS="PROMPT" |
| 271 | >bash$ </TT |
| 272 | ><TT |
| 273 | CLASS="USERINPUT" |
| 274 | ><B |
| 275 | >ls abc</B |
| 276 | ></TT |
| 277 | > |
| 278 | <TT |
| 279 | CLASS="COMPUTEROUTPUT" |
| 280 | >ls: abc: No such file or directory</TT |
| 281 | > |
| 282 | |
| 283 | |
| 284 | <TT |
| 285 | CLASS="PROMPT" |
| 286 | >bash$ </TT |
| 287 | ><TT |
| 288 | CLASS="USERINPUT" |
| 289 | ><B |
| 290 | >echo $?</B |
| 291 | ></TT |
| 292 | > |
| 293 | <TT |
| 294 | CLASS="COMPUTEROUTPUT" |
| 295 | >2</TT |
| 296 | ></PRE |
| 297 | ></TD |
| 298 | ></TR |
| 299 | ></TABLE |
| 300 | > |
| 301 | </P |
| 302 | ></TD |
| 303 | ></TR |
| 304 | ></TABLE |
| 305 | ></DIV |
| 306 | ><DIV |
| 307 | CLASS="EXAMPLE" |
| 308 | ><HR><A |
| 309 | NAME="EX40" |
| 310 | ></A |
| 311 | ><P |
| 312 | ><B |
| 313 | >Example 16-1. Using <I |
| 314 | CLASS="FIRSTTERM" |
| 315 | >ls</I |
| 316 | > to create a table of contents |
| 317 | for burning a <SPAN |
| 318 | CLASS="ABBREV" |
| 319 | >CDR</SPAN |
| 320 | > disk</B |
| 321 | ></P |
| 322 | ><TABLE |
| 323 | BORDER="0" |
| 324 | BGCOLOR="#E0E0E0" |
| 325 | WIDTH="90%" |
| 326 | ><TR |
| 327 | ><TD |
| 328 | ><PRE |
| 329 | CLASS="PROGRAMLISTING" |
| 330 | > 1 #!/bin/bash |
| 331 | 2 # ex40.sh (burn-cd.sh) |
| 332 | 3 # Script to automate burning a CDR. |
| 333 | 4 |
| 334 | 5 |
| 335 | 6 SPEED=10 # May use higher speed if your hardware supports it. |
| 336 | 7 IMAGEFILE=cdimage.iso |
| 337 | 8 CONTENTSFILE=contents |
| 338 | 9 # DEVICE=/dev/cdrom For older versions of cdrecord |
| 339 | 10 DEVICE="1,0,0" |
| 340 | 11 DEFAULTDIR=/opt # This is the directory containing the data to be burned. |
| 341 | 12 # Make sure it exists. |
| 342 | 13 # Exercise: Add a test for this. |
| 343 | 14 |
| 344 | 15 # Uses Joerg Schilling's "cdrecord" package: |
| 345 | 16 # http://www.fokus.fhg.de/usr/schilling/cdrecord.html |
| 346 | 17 |
| 347 | 18 # If this script invoked as an ordinary user, may need to suid cdrecord |
| 348 | 19 #+ chmod u+s /usr/bin/cdrecord, as root. |
| 349 | 20 # Of course, this creates a security hole, though a relatively minor one. |
| 350 | 21 |
| 351 | 22 if [ -z "$1" ] |
| 352 | 23 then |
| 353 | 24 IMAGE_DIRECTORY=$DEFAULTDIR |
| 354 | 25 # Default directory, if not specified on command-line. |
| 355 | 26 else |
| 356 | 27 IMAGE_DIRECTORY=$1 |
| 357 | 28 fi |
| 358 | 29 |
| 359 | 30 # Create a "table of contents" file. |
| 360 | 31 ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE |
| 361 | 32 # The "l" option gives a "long" file listing. |
| 362 | 33 # The "R" option makes the listing recursive. |
| 363 | 34 # The "F" option marks the file types (directories get a trailing /). |
| 364 | 35 echo "Creating table of contents." |
| 365 | 36 |
| 366 | 37 # Create an image file preparatory to burning it onto the CDR. |
| 367 | 38 mkisofs -r -o $IMAGEFILE $IMAGE_DIRECTORY |
| 368 | 39 echo "Creating ISO9660 file system image ($IMAGEFILE)." |
| 369 | 40 |
| 370 | 41 # Burn the CDR. |
| 371 | 42 echo "Burning the disk." |
| 372 | 43 echo "Please be patient, this will take a while." |
| 373 | 44 wodim -v -isosize dev=$DEVICE $IMAGEFILE |
| 374 | 45 # In newer Linux distros, the "wodim" utility assumes the |
| 375 | 46 #+ functionality of "cdrecord." |
| 376 | 47 exitcode=$? |
| 377 | 48 echo "Exit code = $exitcode" |
| 378 | 49 |
| 379 | 50 exit $exitcode</PRE |
| 380 | ></TD |
| 381 | ></TR |
| 382 | ></TABLE |
| 383 | ><HR></DIV |
| 384 | ></DD |
| 385 | ><DT |
| 386 | ><A |
| 387 | NAME="CATREF" |
| 388 | ></A |
| 389 | ><B |
| 390 | CLASS="COMMAND" |
| 391 | >cat</B |
| 392 | >, <B |
| 393 | CLASS="COMMAND" |
| 394 | >tac</B |
| 395 | ></DT |
| 396 | ><DD |
| 397 | ><P |
| 398 | ><B |
| 399 | CLASS="COMMAND" |
| 400 | >cat</B |
| 401 | >, an acronym for |
| 402 | <I |
| 403 | CLASS="WORDASWORD" |
| 404 | >concatenate</I |
| 405 | >, |
| 406 | lists a file to <TT |
| 407 | CLASS="FILENAME" |
| 408 | >stdout</TT |
| 409 | >. When |
| 410 | combined with redirection (<SPAN |
| 411 | CLASS="TOKEN" |
| 412 | >></SPAN |
| 413 | > or |
| 414 | <SPAN |
| 415 | CLASS="TOKEN" |
| 416 | >>></SPAN |
| 417 | >), it is commonly used to concatenate |
| 418 | files. |
| 419 | |
| 420 | <A |
| 421 | NAME="CATUSES" |
| 422 | ></A |
| 423 | > |
| 424 | <TABLE |
| 425 | BORDER="0" |
| 426 | BGCOLOR="#E0E0E0" |
| 427 | WIDTH="90%" |
| 428 | ><TR |
| 429 | ><TD |
| 430 | ><PRE |
| 431 | CLASS="PROGRAMLISTING" |
| 432 | > 1 # Uses of 'cat' |
| 433 | 2 cat filename # Lists the file. |
| 434 | 3 |
| 435 | 4 cat file.1 file.2 file.3 > file.123 # Combines three files into one.</PRE |
| 436 | ></TD |
| 437 | ></TR |
| 438 | ></TABLE |
| 439 | > |
| 440 | |
| 441 | The <TT |
| 442 | CLASS="OPTION" |
| 443 | >-n</TT |
| 444 | > option to <B |
| 445 | CLASS="COMMAND" |
| 446 | >cat</B |
| 447 | > |
| 448 | inserts consecutive numbers before all lines of the |
| 449 | target file(s). The <TT |
| 450 | CLASS="OPTION" |
| 451 | >-b</TT |
| 452 | > option numbers |
| 453 | only the non-blank lines. The <TT |
| 454 | CLASS="OPTION" |
| 455 | >-v</TT |
| 456 | > option |
| 457 | echoes nonprintable characters, using <SPAN |
| 458 | CLASS="TOKEN" |
| 459 | >^</SPAN |
| 460 | > |
| 461 | notation. The <TT |
| 462 | CLASS="OPTION" |
| 463 | >-s</TT |
| 464 | > option squeezes multiple |
| 465 | consecutive blank lines into a single blank line.</P |
| 466 | ><P |
| 467 | >See also <A |
| 468 | HREF="textproc.html#LNUM" |
| 469 | >Example 16-28</A |
| 470 | > and <A |
| 471 | HREF="textproc.html#ROT13" |
| 472 | >Example 16-24</A |
| 473 | >.</P |
| 474 | ><DIV |
| 475 | CLASS="NOTE" |
| 476 | ><TABLE |
| 477 | CLASS="NOTE" |
| 478 | WIDTH="90%" |
| 479 | BORDER="0" |
| 480 | ><TR |
| 481 | ><TD |
| 482 | WIDTH="25" |
| 483 | ALIGN="CENTER" |
| 484 | VALIGN="TOP" |
| 485 | ><IMG |
| 486 | SRC="common/note.png" |
| 487 | HSPACE="5" |
| 488 | ALT="Note"></TD |
| 489 | ><TD |
| 490 | ALIGN="LEFT" |
| 491 | VALIGN="TOP" |
| 492 | ><P |
| 493 | ><A |
| 494 | NAME="CATLESSEFF" |
| 495 | ></A |
| 496 | > |
| 497 | In a <A |
| 498 | HREF="special-chars.html#PIPEREF" |
| 499 | >pipe</A |
| 500 | >, it may be |
| 501 | more efficient to <A |
| 502 | HREF="io-redirection.html#IOREDIRREF" |
| 503 | >redirect</A |
| 504 | > |
| 505 | the <TT |
| 506 | CLASS="FILENAME" |
| 507 | >stdin</TT |
| 508 | > to a file, rather than to |
| 509 | <B |
| 510 | CLASS="COMMAND" |
| 511 | >cat</B |
| 512 | > the file. |
| 513 | </P |
| 514 | ><P |
| 515 | > <TABLE |
| 516 | BORDER="0" |
| 517 | BGCOLOR="#E0E0E0" |
| 518 | WIDTH="90%" |
| 519 | ><TR |
| 520 | ><TD |
| 521 | ><PRE |
| 522 | CLASS="PROGRAMLISTING" |
| 523 | > 1 cat filename | tr a-z A-Z |
| 524 | 2 |
| 525 | 3 tr a-z A-Z < filename # Same effect, but starts one less process, |
| 526 | 4 #+ and also dispenses with the pipe.</PRE |
| 527 | ></TD |
| 528 | ></TR |
| 529 | ></TABLE |
| 530 | > |
| 531 | </P |
| 532 | ></TD |
| 533 | ></TR |
| 534 | ></TABLE |
| 535 | ></DIV |
| 536 | ><P |
| 537 | ><B |
| 538 | CLASS="COMMAND" |
| 539 | >tac</B |
| 540 | >, is the inverse of |
| 541 | <I |
| 542 | CLASS="WORDASWORD" |
| 543 | >cat</I |
| 544 | >, listing a file backwards from its end.</P |
| 545 | ></DD |
| 546 | ><DT |
| 547 | ><A |
| 548 | NAME="REVREF" |
| 549 | ></A |
| 550 | ><B |
| 551 | CLASS="COMMAND" |
| 552 | >rev</B |
| 553 | ></DT |
| 554 | ><DD |
| 555 | ><P |
| 556 | >reverses each line of a file, and outputs to |
| 557 | <TT |
| 558 | CLASS="FILENAME" |
| 559 | >stdout</TT |
| 560 | >. This does not have the same effect |
| 561 | as <B |
| 562 | CLASS="COMMAND" |
| 563 | >tac</B |
| 564 | >, as it preserves the order of |
| 565 | the lines, but flips each one around (mirror image).</P |
| 566 | ><P |
| 567 | > <TABLE |
| 568 | BORDER="0" |
| 569 | BGCOLOR="#E0E0E0" |
| 570 | WIDTH="90%" |
| 571 | ><TR |
| 572 | ><TD |
| 573 | ><PRE |
| 574 | CLASS="SCREEN" |
| 575 | > <TT |
| 576 | CLASS="PROMPT" |
| 577 | >bash$ </TT |
| 578 | ><TT |
| 579 | CLASS="USERINPUT" |
| 580 | ><B |
| 581 | >cat file1.txt</B |
| 582 | ></TT |
| 583 | > |
| 584 | <TT |
| 585 | CLASS="COMPUTEROUTPUT" |
| 586 | >This is line 1. |
| 587 | This is line 2.</TT |
| 588 | > |
| 589 | |
| 590 | |
| 591 | <TT |
| 592 | CLASS="PROMPT" |
| 593 | >bash$ </TT |
| 594 | ><TT |
| 595 | CLASS="USERINPUT" |
| 596 | ><B |
| 597 | >tac file1.txt</B |
| 598 | ></TT |
| 599 | > |
| 600 | <TT |
| 601 | CLASS="COMPUTEROUTPUT" |
| 602 | >This is line 2. |
| 603 | This is line 1.</TT |
| 604 | > |
| 605 | |
| 606 | |
| 607 | <TT |
| 608 | CLASS="PROMPT" |
| 609 | >bash$ </TT |
| 610 | ><TT |
| 611 | CLASS="USERINPUT" |
| 612 | ><B |
| 613 | >rev file1.txt</B |
| 614 | ></TT |
| 615 | > |
| 616 | <TT |
| 617 | CLASS="COMPUTEROUTPUT" |
| 618 | >.1 enil si sihT |
| 619 | .2 enil si sihT</TT |
| 620 | > |
| 621 | </PRE |
| 622 | ></TD |
| 623 | ></TR |
| 624 | ></TABLE |
| 625 | > |
| 626 | </P |
| 627 | ></DD |
| 628 | ><DT |
| 629 | ><A |
| 630 | NAME="CPREF" |
| 631 | ></A |
| 632 | ><B |
| 633 | CLASS="COMMAND" |
| 634 | >cp</B |
| 635 | ></DT |
| 636 | ><DD |
| 637 | ><P |
| 638 | >This is the file copy command. <TT |
| 639 | CLASS="USERINPUT" |
| 640 | ><B |
| 641 | >cp file1 |
| 642 | file2</B |
| 643 | ></TT |
| 644 | > copies <TT |
| 645 | CLASS="FILENAME" |
| 646 | >file1</TT |
| 647 | > |
| 648 | to <TT |
| 649 | CLASS="FILENAME" |
| 650 | >file2</TT |
| 651 | >, overwriting |
| 652 | <TT |
| 653 | CLASS="FILENAME" |
| 654 | >file2</TT |
| 655 | > if it already exists (see <A |
| 656 | HREF="moreadv.html#EX42" |
| 657 | >Example 16-6</A |
| 658 | >).</P |
| 659 | ><DIV |
| 660 | CLASS="TIP" |
| 661 | ><TABLE |
| 662 | CLASS="TIP" |
| 663 | WIDTH="90%" |
| 664 | BORDER="0" |
| 665 | ><TR |
| 666 | ><TD |
| 667 | WIDTH="25" |
| 668 | ALIGN="CENTER" |
| 669 | VALIGN="TOP" |
| 670 | ><IMG |
| 671 | SRC="common/tip.png" |
| 672 | HSPACE="5" |
| 673 | ALT="Tip"></TD |
| 674 | ><TD |
| 675 | ALIGN="LEFT" |
| 676 | VALIGN="TOP" |
| 677 | ><P |
| 678 | >Particularly useful are the <TT |
| 679 | CLASS="OPTION" |
| 680 | >-a</TT |
| 681 | > |
| 682 | archive flag (for copying an entire directory tree), |
| 683 | the <TT |
| 684 | CLASS="OPTION" |
| 685 | >-u</TT |
| 686 | > update flag (which prevents |
| 687 | overwriting identically-named newer files), and the |
| 688 | <TT |
| 689 | CLASS="OPTION" |
| 690 | >-r</TT |
| 691 | > and <TT |
| 692 | CLASS="OPTION" |
| 693 | >-R</TT |
| 694 | > recursive |
| 695 | flags.</P |
| 696 | ><P |
| 697 | ><TABLE |
| 698 | BORDER="0" |
| 699 | BGCOLOR="#E0E0E0" |
| 700 | WIDTH="90%" |
| 701 | ><TR |
| 702 | ><TD |
| 703 | ><PRE |
| 704 | CLASS="PROGRAMLISTING" |
| 705 | > 1 cp -u source_dir/* dest_dir |
| 706 | 2 # "Synchronize" dest_dir to source_dir |
| 707 | 3 #+ by copying over all newer and not previously existing files.</PRE |
| 708 | ></TD |
| 709 | ></TR |
| 710 | ></TABLE |
| 711 | ></P |
| 712 | ></TD |
| 713 | ></TR |
| 714 | ></TABLE |
| 715 | ></DIV |
| 716 | ></DD |
| 717 | ><DT |
| 718 | ><A |
| 719 | NAME="MVREF" |
| 720 | ></A |
| 721 | ><B |
| 722 | CLASS="COMMAND" |
| 723 | >mv</B |
| 724 | ></DT |
| 725 | ><DD |
| 726 | ><P |
| 727 | >This is the file <I |
| 728 | CLASS="FIRSTTERM" |
| 729 | >move</I |
| 730 | > command. |
| 731 | It is equivalent to a combination of <B |
| 732 | CLASS="COMMAND" |
| 733 | >cp</B |
| 734 | > |
| 735 | and <B |
| 736 | CLASS="COMMAND" |
| 737 | >rm</B |
| 738 | >. It may be used to move multiple |
| 739 | files to a directory, or even to rename a directory. For |
| 740 | some examples of using <B |
| 741 | CLASS="COMMAND" |
| 742 | >mv</B |
| 743 | > in a script, |
| 744 | see <A |
| 745 | HREF="parameter-substitution.html#RFE" |
| 746 | >Example 10-11</A |
| 747 | > and <A |
| 748 | HREF="contributed-scripts.html#RN" |
| 749 | >Example A-2</A |
| 750 | >.</P |
| 751 | ><DIV |
| 752 | CLASS="NOTE" |
| 753 | ><TABLE |
| 754 | CLASS="NOTE" |
| 755 | WIDTH="90%" |
| 756 | BORDER="0" |
| 757 | ><TR |
| 758 | ><TD |
| 759 | WIDTH="25" |
| 760 | ALIGN="CENTER" |
| 761 | VALIGN="TOP" |
| 762 | ><IMG |
| 763 | SRC="common/note.png" |
| 764 | HSPACE="5" |
| 765 | ALT="Note"></TD |
| 766 | ><TD |
| 767 | ALIGN="LEFT" |
| 768 | VALIGN="TOP" |
| 769 | ><P |
| 770 | >When used in a non-interactive script, |
| 771 | <B |
| 772 | CLASS="COMMAND" |
| 773 | >mv</B |
| 774 | > takes the <TT |
| 775 | CLASS="OPTION" |
| 776 | >-f</TT |
| 777 | > |
| 778 | (<I |
| 779 | CLASS="FIRSTTERM" |
| 780 | >force</I |
| 781 | >) option to bypass user |
| 782 | input.</P |
| 783 | ><P |
| 784 | >When a directory is moved to a preexisting directory, |
| 785 | it becomes a subdirectory of the destination directory.</P |
| 786 | ><P |
| 787 | > <TABLE |
| 788 | BORDER="0" |
| 789 | BGCOLOR="#E0E0E0" |
| 790 | WIDTH="90%" |
| 791 | ><TR |
| 792 | ><TD |
| 793 | ><PRE |
| 794 | CLASS="SCREEN" |
| 795 | > <TT |
| 796 | CLASS="PROMPT" |
| 797 | >bash$ </TT |
| 798 | ><TT |
| 799 | CLASS="USERINPUT" |
| 800 | ><B |
| 801 | >mv source_directory target_directory</B |
| 802 | ></TT |
| 803 | > |
| 804 | |
| 805 | <TT |
| 806 | CLASS="PROMPT" |
| 807 | >bash$ </TT |
| 808 | ><TT |
| 809 | CLASS="USERINPUT" |
| 810 | ><B |
| 811 | >ls -lF target_directory</B |
| 812 | ></TT |
| 813 | > |
| 814 | <TT |
| 815 | CLASS="COMPUTEROUTPUT" |
| 816 | >total 1 |
| 817 | drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/</TT |
| 818 | > |
| 819 | </PRE |
| 820 | ></TD |
| 821 | ></TR |
| 822 | ></TABLE |
| 823 | > |
| 824 | </P |
| 825 | ></TD |
| 826 | ></TR |
| 827 | ></TABLE |
| 828 | ></DIV |
| 829 | ></DD |
| 830 | ><DT |
| 831 | ><A |
| 832 | NAME="RMREF" |
| 833 | ></A |
| 834 | ><B |
| 835 | CLASS="COMMAND" |
| 836 | >rm</B |
| 837 | ></DT |
| 838 | ><DD |
| 839 | ><P |
| 840 | >Delete (remove) a file or files. The <TT |
| 841 | CLASS="OPTION" |
| 842 | >-f</TT |
| 843 | > |
| 844 | option forces removal of even readonly files, and is useful |
| 845 | for bypassing user input in a script.</P |
| 846 | ><DIV |
| 847 | CLASS="NOTE" |
| 848 | ><TABLE |
| 849 | CLASS="NOTE" |
| 850 | WIDTH="90%" |
| 851 | BORDER="0" |
| 852 | ><TR |
| 853 | ><TD |
| 854 | WIDTH="25" |
| 855 | ALIGN="CENTER" |
| 856 | VALIGN="TOP" |
| 857 | ><IMG |
| 858 | SRC="common/note.png" |
| 859 | HSPACE="5" |
| 860 | ALT="Note"></TD |
| 861 | ><TD |
| 862 | ALIGN="LEFT" |
| 863 | VALIGN="TOP" |
| 864 | ><P |
| 865 | ><A |
| 866 | NAME="DASHREM" |
| 867 | ></A |
| 868 | ></P |
| 869 | ><P |
| 870 | >The <I |
| 871 | CLASS="FIRSTTERM" |
| 872 | >rm</I |
| 873 | > command will, by |
| 874 | itself, fail to remove filenames beginning with |
| 875 | a dash. Why? Because <I |
| 876 | CLASS="FIRSTTERM" |
| 877 | >rm</I |
| 878 | > |
| 879 | sees a dash-prefixed filename as an |
| 880 | <I |
| 881 | CLASS="FIRSTTERM" |
| 882 | >option</I |
| 883 | >.</P |
| 884 | ><P |
| 885 | > <TABLE |
| 886 | BORDER="0" |
| 887 | BGCOLOR="#E0E0E0" |
| 888 | WIDTH="90%" |
| 889 | ><TR |
| 890 | ><TD |
| 891 | ><PRE |
| 892 | CLASS="SCREEN" |
| 893 | > <TT |
| 894 | CLASS="PROMPT" |
| 895 | >bash$ </TT |
| 896 | ><TT |
| 897 | CLASS="USERINPUT" |
| 898 | ><B |
| 899 | >rm -badname</B |
| 900 | ></TT |
| 901 | > |
| 902 | <TT |
| 903 | CLASS="COMPUTEROUTPUT" |
| 904 | >rm: invalid option -- b |
| 905 | Try `rm --help' for more information.</TT |
| 906 | ></PRE |
| 907 | ></TD |
| 908 | ></TR |
| 909 | ></TABLE |
| 910 | > |
| 911 | </P |
| 912 | ><P |
| 913 | > One clever workaround is to precede |
| 914 | the filename with a <SPAN |
| 915 | CLASS="QUOTE" |
| 916 | >" -- "</SPAN |
| 917 | > (the |
| 918 | <I |
| 919 | CLASS="FIRSTTERM" |
| 920 | >end-of-options</I |
| 921 | > flag). |
| 922 | |
| 923 | <TABLE |
| 924 | BORDER="0" |
| 925 | BGCOLOR="#E0E0E0" |
| 926 | WIDTH="90%" |
| 927 | ><TR |
| 928 | ><TD |
| 929 | ><PRE |
| 930 | CLASS="SCREEN" |
| 931 | > <TT |
| 932 | CLASS="PROMPT" |
| 933 | >bash$ </TT |
| 934 | ><TT |
| 935 | CLASS="USERINPUT" |
| 936 | ><B |
| 937 | >rm -- -badname</B |
| 938 | ></TT |
| 939 | ></PRE |
| 940 | ></TD |
| 941 | ></TR |
| 942 | ></TABLE |
| 943 | > |
| 944 | </P |
| 945 | ><P |
| 946 | > Another method to is to preface the filename to be removed |
| 947 | with a <TT |
| 948 | CLASS="FILENAME" |
| 949 | >dot-slash</TT |
| 950 | > . |
| 951 | |
| 952 | <TABLE |
| 953 | BORDER="0" |
| 954 | BGCOLOR="#E0E0E0" |
| 955 | WIDTH="90%" |
| 956 | ><TR |
| 957 | ><TD |
| 958 | ><PRE |
| 959 | CLASS="SCREEN" |
| 960 | > <TT |
| 961 | CLASS="PROMPT" |
| 962 | >bash$ </TT |
| 963 | ><TT |
| 964 | CLASS="USERINPUT" |
| 965 | ><B |
| 966 | >rm ./-badname</B |
| 967 | ></TT |
| 968 | ></PRE |
| 969 | ></TD |
| 970 | ></TR |
| 971 | ></TABLE |
| 972 | > |
| 973 | </P |
| 974 | ></TD |
| 975 | ></TR |
| 976 | ></TABLE |
| 977 | ></DIV |
| 978 | ><DIV |
| 979 | CLASS="WARNING" |
| 980 | ><TABLE |
| 981 | CLASS="WARNING" |
| 982 | WIDTH="90%" |
| 983 | BORDER="0" |
| 984 | ><TR |
| 985 | ><TD |
| 986 | WIDTH="25" |
| 987 | ALIGN="CENTER" |
| 988 | VALIGN="TOP" |
| 989 | ><IMG |
| 990 | SRC="common/warning.png" |
| 991 | HSPACE="5" |
| 992 | ALT="Warning"></TD |
| 993 | ><TD |
| 994 | ALIGN="LEFT" |
| 995 | VALIGN="TOP" |
| 996 | ><P |
| 997 | ><A |
| 998 | NAME="RMRECURS" |
| 999 | ></A |
| 1000 | >When used with the |
| 1001 | recursive flag <TT |
| 1002 | CLASS="OPTION" |
| 1003 | >-r</TT |
| 1004 | >, this command removes |
| 1005 | files all the way down the directory tree from the current |
| 1006 | directory. A careless <B |
| 1007 | CLASS="COMMAND" |
| 1008 | >rm -rf *</B |
| 1009 | > can wipe |
| 1010 | out a big chunk of a directory structure.</P |
| 1011 | ></TD |
| 1012 | ></TR |
| 1013 | ></TABLE |
| 1014 | ></DIV |
| 1015 | ></DD |
| 1016 | ><DT |
| 1017 | ><A |
| 1018 | NAME="RMDIRREF" |
| 1019 | ></A |
| 1020 | ><B |
| 1021 | CLASS="COMMAND" |
| 1022 | >rmdir</B |
| 1023 | ></DT |
| 1024 | ><DD |
| 1025 | ><P |
| 1026 | >Remove directory. The directory must be empty of |
| 1027 | all files -- including <SPAN |
| 1028 | CLASS="QUOTE" |
| 1029 | >"invisible"</SPAN |
| 1030 | > |
| 1031 | <I |
| 1032 | CLASS="FIRSTTERM" |
| 1033 | >dotfiles</I |
| 1034 | > |
| 1035 | |
| 1036 | <A |
| 1037 | NAME="AEN10228" |
| 1038 | HREF="#FTN.AEN10228" |
| 1039 | >[2]</A |
| 1040 | > |
| 1041 | |
| 1042 | -- for this command to succeed.</P |
| 1043 | ></DD |
| 1044 | ><DT |
| 1045 | ><A |
| 1046 | NAME="MKDIRREF" |
| 1047 | ></A |
| 1048 | ><B |
| 1049 | CLASS="COMMAND" |
| 1050 | >mkdir</B |
| 1051 | ></DT |
| 1052 | ><DD |
| 1053 | ><P |
| 1054 | >Make directory, creates a new directory. For example, |
| 1055 | <TT |
| 1056 | CLASS="USERINPUT" |
| 1057 | ><B |
| 1058 | >mkdir -p project/programs/December</B |
| 1059 | ></TT |
| 1060 | > |
| 1061 | creates the named directory. The |
| 1062 | <TT |
| 1063 | CLASS="REPLACEABLE" |
| 1064 | ><I |
| 1065 | >-p</I |
| 1066 | ></TT |
| 1067 | > option automatically creates |
| 1068 | any necessary parent directories.</P |
| 1069 | ></DD |
| 1070 | ><DT |
| 1071 | ><A |
| 1072 | NAME="CHMODREF" |
| 1073 | ></A |
| 1074 | ><B |
| 1075 | CLASS="COMMAND" |
| 1076 | >chmod</B |
| 1077 | ></DT |
| 1078 | ><DD |
| 1079 | ><P |
| 1080 | >Changes the attributes of an existing file or directory |
| 1081 | (see <A |
| 1082 | HREF="internal.html#EX44" |
| 1083 | >Example 15-14</A |
| 1084 | >).</P |
| 1085 | ><P |
| 1086 | ><TABLE |
| 1087 | BORDER="0" |
| 1088 | BGCOLOR="#E0E0E0" |
| 1089 | WIDTH="90%" |
| 1090 | ><TR |
| 1091 | ><TD |
| 1092 | ><PRE |
| 1093 | CLASS="PROGRAMLISTING" |
| 1094 | > 1 chmod +x filename |
| 1095 | 2 # Makes "filename" executable for all users. |
| 1096 | 3 |
| 1097 | 4 chmod u+s filename |
| 1098 | 5 # Sets "suid" bit on "filename" permissions. |
| 1099 | 6 # An ordinary user may execute "filename" with same privileges as the file's owner. |
| 1100 | 7 # (This does not apply to shell scripts.)</PRE |
| 1101 | ></TD |
| 1102 | ></TR |
| 1103 | ></TABLE |
| 1104 | ></P |
| 1105 | ><P |
| 1106 | ><TABLE |
| 1107 | BORDER="0" |
| 1108 | BGCOLOR="#E0E0E0" |
| 1109 | WIDTH="90%" |
| 1110 | ><TR |
| 1111 | ><TD |
| 1112 | ><PRE |
| 1113 | CLASS="PROGRAMLISTING" |
| 1114 | > 1 chmod 644 filename |
| 1115 | 2 # Makes "filename" readable/writable to owner, readable to others |
| 1116 | 3 #+ (octal mode). |
| 1117 | 4 |
| 1118 | 5 chmod 444 filename |
| 1119 | 6 # Makes "filename" read-only for all. |
| 1120 | 7 # Modifying the file (for example, with a text editor) |
| 1121 | 8 #+ not allowed for a user who does not own the file (except for root), |
| 1122 | 9 #+ and even the file owner must force a file-save |
| 1123 | 10 #+ if she modifies the file. |
| 1124 | 11 # Same restrictions apply for deleting the file.</PRE |
| 1125 | ></TD |
| 1126 | ></TR |
| 1127 | ></TABLE |
| 1128 | ></P |
| 1129 | ><P |
| 1130 | ><TABLE |
| 1131 | BORDER="0" |
| 1132 | BGCOLOR="#E0E0E0" |
| 1133 | WIDTH="90%" |
| 1134 | ><TR |
| 1135 | ><TD |
| 1136 | ><PRE |
| 1137 | CLASS="PROGRAMLISTING" |
| 1138 | > 1 chmod 1777 directory-name |
| 1139 | 2 # Gives everyone read, write, and execute permission in directory, |
| 1140 | 3 #+ however also sets the "sticky bit". |
| 1141 | 4 # This means that only the owner of the directory, |
| 1142 | 5 #+ owner of the file, and, of course, root |
| 1143 | 6 #+ can delete any particular file in that directory. |
| 1144 | 7 |
| 1145 | 8 chmod 111 directory-name |
| 1146 | 9 # Gives everyone execute-only permission in a directory. |
| 1147 | 10 # This means that you can execute and READ the files in that directory |
| 1148 | 11 #+ (execute permission necessarily includes read permission |
| 1149 | 12 #+ because you can't execute a file without being able to read it). |
| 1150 | 13 # But you can't list the files or search for them with the "find" command. |
| 1151 | 14 # These restrictions do not apply to root. |
| 1152 | 15 |
| 1153 | 16 chmod 000 directory-name |
| 1154 | 17 # No permissions at all for that directory. |
| 1155 | 18 # Can't read, write, or execute files in it. |
| 1156 | 19 # Can't even list files in it or "cd" to it. |
| 1157 | 20 # But, you can rename (mv) the directory |
| 1158 | 21 #+ or delete it (rmdir) if it is empty. |
| 1159 | 22 # You can even symlink to files in the directory, |
| 1160 | 23 #+ but you can't read, write, or execute the symlinks. |
| 1161 | 24 # These restrictions do not apply to root.</PRE |
| 1162 | ></TD |
| 1163 | ></TR |
| 1164 | ></TABLE |
| 1165 | ></P |
| 1166 | ></DD |
| 1167 | ><DT |
| 1168 | ><A |
| 1169 | NAME="CHATTRREF" |
| 1170 | ></A |
| 1171 | ><B |
| 1172 | CLASS="COMMAND" |
| 1173 | >chattr</B |
| 1174 | ></DT |
| 1175 | ><DD |
| 1176 | ><P |
| 1177 | ><B |
| 1178 | CLASS="COMMAND" |
| 1179 | >Ch</B |
| 1180 | >ange file |
| 1181 | <B |
| 1182 | CLASS="COMMAND" |
| 1183 | >attr</B |
| 1184 | >ibutes. This is analogous to |
| 1185 | <B |
| 1186 | CLASS="COMMAND" |
| 1187 | >chmod</B |
| 1188 | > above, but with different options |
| 1189 | and a different invocation syntax, and it works only on |
| 1190 | <I |
| 1191 | CLASS="FIRSTTERM" |
| 1192 | >ext2/ext3</I |
| 1193 | > filesystems.</P |
| 1194 | ><P |
| 1195 | >One particularly interesting <B |
| 1196 | CLASS="COMMAND" |
| 1197 | >chattr</B |
| 1198 | > |
| 1199 | option is <TT |
| 1200 | CLASS="OPTION" |
| 1201 | >i</TT |
| 1202 | >. A <B |
| 1203 | CLASS="COMMAND" |
| 1204 | >chattr +i |
| 1205 | <TT |
| 1206 | CLASS="FILENAME" |
| 1207 | >filename</TT |
| 1208 | ></B |
| 1209 | > marks the file |
| 1210 | as immutable. The file cannot be modified, linked to, or |
| 1211 | deleted, <SPAN |
| 1212 | CLASS="emphasis" |
| 1213 | ><I |
| 1214 | CLASS="EMPHASIS" |
| 1215 | >not even by root</I |
| 1216 | ></SPAN |
| 1217 | >. This |
| 1218 | file attribute can be set or removed only by |
| 1219 | <I |
| 1220 | CLASS="FIRSTTERM" |
| 1221 | >root</I |
| 1222 | >. In a similar fashion, |
| 1223 | the <TT |
| 1224 | CLASS="OPTION" |
| 1225 | >a</TT |
| 1226 | > option marks the file as append |
| 1227 | only.</P |
| 1228 | ><P |
| 1229 | > <TABLE |
| 1230 | BORDER="0" |
| 1231 | BGCOLOR="#E0E0E0" |
| 1232 | WIDTH="90%" |
| 1233 | ><TR |
| 1234 | ><TD |
| 1235 | ><PRE |
| 1236 | CLASS="SCREEN" |
| 1237 | > <TT |
| 1238 | CLASS="PROMPT" |
| 1239 | >root# </TT |
| 1240 | ><TT |
| 1241 | CLASS="USERINPUT" |
| 1242 | ><B |
| 1243 | >chattr +i file1.txt</B |
| 1244 | ></TT |
| 1245 | > |
| 1246 | |
| 1247 | |
| 1248 | <TT |
| 1249 | CLASS="PROMPT" |
| 1250 | >root# </TT |
| 1251 | ><TT |
| 1252 | CLASS="USERINPUT" |
| 1253 | ><B |
| 1254 | >rm file1.txt</B |
| 1255 | ></TT |
| 1256 | > |
| 1257 | |
| 1258 | <TT |
| 1259 | CLASS="COMPUTEROUTPUT" |
| 1260 | >rm: remove write-protected regular file `file1.txt'? y |
| 1261 | rm: cannot remove `file1.txt': Operation not permitted</TT |
| 1262 | > |
| 1263 | </PRE |
| 1264 | ></TD |
| 1265 | ></TR |
| 1266 | ></TABLE |
| 1267 | > |
| 1268 | </P |
| 1269 | ><P |
| 1270 | >If a file has the <TT |
| 1271 | CLASS="OPTION" |
| 1272 | >s</TT |
| 1273 | > (secure) |
| 1274 | attribute set, then when it is deleted its block is |
| 1275 | overwritten with binary zeroes. |
| 1276 | <A |
| 1277 | NAME="AEN10301" |
| 1278 | HREF="#FTN.AEN10301" |
| 1279 | >[3]</A |
| 1280 | > |
| 1281 | </P |
| 1282 | ><P |
| 1283 | >If a file has the <TT |
| 1284 | CLASS="OPTION" |
| 1285 | >u</TT |
| 1286 | > (undelete) |
| 1287 | attribute set, then when it is deleted, its contents can still |
| 1288 | be retrieved (undeleted).</P |
| 1289 | ><P |
| 1290 | >If a file has the <TT |
| 1291 | CLASS="OPTION" |
| 1292 | >c</TT |
| 1293 | > (compress) |
| 1294 | attribute set, then it will automatically be compressed |
| 1295 | on writes to disk, and uncompressed on reads.</P |
| 1296 | ><DIV |
| 1297 | CLASS="NOTE" |
| 1298 | ><TABLE |
| 1299 | CLASS="NOTE" |
| 1300 | WIDTH="90%" |
| 1301 | BORDER="0" |
| 1302 | ><TR |
| 1303 | ><TD |
| 1304 | WIDTH="25" |
| 1305 | ALIGN="CENTER" |
| 1306 | VALIGN="TOP" |
| 1307 | ><IMG |
| 1308 | SRC="common/note.png" |
| 1309 | HSPACE="5" |
| 1310 | ALT="Note"></TD |
| 1311 | ><TD |
| 1312 | ALIGN="LEFT" |
| 1313 | VALIGN="TOP" |
| 1314 | ><P |
| 1315 | >The file attributes set with |
| 1316 | <B |
| 1317 | CLASS="COMMAND" |
| 1318 | >chattr</B |
| 1319 | > do not show in a file listing |
| 1320 | (<B |
| 1321 | CLASS="COMMAND" |
| 1322 | >ls -l</B |
| 1323 | >).</P |
| 1324 | ></TD |
| 1325 | ></TR |
| 1326 | ></TABLE |
| 1327 | ></DIV |
| 1328 | ></DD |
| 1329 | ><DT |
| 1330 | ><A |
| 1331 | NAME="LINKREF" |
| 1332 | ></A |
| 1333 | ><B |
| 1334 | CLASS="COMMAND" |
| 1335 | >ln</B |
| 1336 | ></DT |
| 1337 | ><DD |
| 1338 | ><P |
| 1339 | >Creates links to pre-existings files. A <SPAN |
| 1340 | CLASS="QUOTE" |
| 1341 | >"link"</SPAN |
| 1342 | > |
| 1343 | is a reference to a file, an alternate name for it. |
| 1344 | The <B |
| 1345 | CLASS="COMMAND" |
| 1346 | >ln</B |
| 1347 | > command permits referencing |
| 1348 | the linked file by more than one name and is a superior |
| 1349 | alternative to aliasing (see <A |
| 1350 | HREF="othertypesv.html#EX18" |
| 1351 | >Example 4-6</A |
| 1352 | >).</P |
| 1353 | ><P |
| 1354 | >The <B |
| 1355 | CLASS="COMMAND" |
| 1356 | >ln</B |
| 1357 | > creates only a reference, a |
| 1358 | pointer to the file only a few bytes in size.</P |
| 1359 | ><P |
| 1360 | ><A |
| 1361 | NAME="SYMLINKREF" |
| 1362 | ></A |
| 1363 | ></P |
| 1364 | ><P |
| 1365 | >The <B |
| 1366 | CLASS="COMMAND" |
| 1367 | >ln</B |
| 1368 | > command is most often used |
| 1369 | with the <TT |
| 1370 | CLASS="OPTION" |
| 1371 | >-s</TT |
| 1372 | >, symbolic or |
| 1373 | <SPAN |
| 1374 | CLASS="QUOTE" |
| 1375 | >"soft"</SPAN |
| 1376 | > link flag. Advantages of using the |
| 1377 | <TT |
| 1378 | CLASS="OPTION" |
| 1379 | >-s</TT |
| 1380 | > flag are that it permits linking across |
| 1381 | file systems or to directories.</P |
| 1382 | ><P |
| 1383 | >The syntax of the command is a bit tricky. For example: |
| 1384 | <TT |
| 1385 | CLASS="USERINPUT" |
| 1386 | ><B |
| 1387 | >ln -s oldfile newfile</B |
| 1388 | ></TT |
| 1389 | > links the |
| 1390 | previously existing <TT |
| 1391 | CLASS="FILENAME" |
| 1392 | >oldfile</TT |
| 1393 | > to the |
| 1394 | newly created link, <TT |
| 1395 | CLASS="FILENAME" |
| 1396 | >newfile</TT |
| 1397 | >.</P |
| 1398 | ><DIV |
| 1399 | CLASS="CAUTION" |
| 1400 | ><TABLE |
| 1401 | CLASS="CAUTION" |
| 1402 | WIDTH="90%" |
| 1403 | BORDER="0" |
| 1404 | ><TR |
| 1405 | ><TD |
| 1406 | WIDTH="25" |
| 1407 | ALIGN="CENTER" |
| 1408 | VALIGN="TOP" |
| 1409 | ><IMG |
| 1410 | SRC="common/caution.png" |
| 1411 | HSPACE="5" |
| 1412 | ALT="Caution"></TD |
| 1413 | ><TD |
| 1414 | ALIGN="LEFT" |
| 1415 | VALIGN="TOP" |
| 1416 | ><P |
| 1417 | >If a file named <TT |
| 1418 | CLASS="FILENAME" |
| 1419 | >newfile</TT |
| 1420 | > has |
| 1421 | previously existed, an error message will |
| 1422 | result.</P |
| 1423 | ></TD |
| 1424 | ></TR |
| 1425 | ></TABLE |
| 1426 | ></DIV |
| 1427 | ><TABLE |
| 1428 | CLASS="SIDEBAR" |
| 1429 | BORDER="1" |
| 1430 | CELLPADDING="5" |
| 1431 | ><TR |
| 1432 | ><TD |
| 1433 | ><DIV |
| 1434 | CLASS="SIDEBAR" |
| 1435 | ><A |
| 1436 | NAME="AEN10336" |
| 1437 | ></A |
| 1438 | ><P |
| 1439 | ><B |
| 1440 | >Which type of link to use?</B |
| 1441 | ></P |
| 1442 | ><P |
| 1443 | >As John Macdonald explains it:</P |
| 1444 | ><P |
| 1445 | >Both of these [types of links] provide a certain measure of dual reference |
| 1446 | -- if you edit the contents of the file using any name, |
| 1447 | your changes will affect both the original name and either |
| 1448 | a hard or soft new name. The differences between them |
| 1449 | occurs when you work at a higher level. The advantage of |
| 1450 | a hard link is that the new name is totally independent |
| 1451 | of the old name -- if you remove or rename the old name, |
| 1452 | that does not affect the hard link, which continues |
| 1453 | to point to the data while it would leave a soft link |
| 1454 | hanging pointing to the old name which is no longer |
| 1455 | there. The advantage of a soft link is that it can refer |
| 1456 | to a different file system (since it is just a reference |
| 1457 | to a file name, not to actual data). And, unlike a hard |
| 1458 | link, a symbolic link can refer to a directory.</P |
| 1459 | ></DIV |
| 1460 | ></TD |
| 1461 | ></TR |
| 1462 | ></TABLE |
| 1463 | ><P |
| 1464 | ><A |
| 1465 | NAME="LINKMINVOK" |
| 1466 | ></A |
| 1467 | ></P |
| 1468 | ><P |
| 1469 | >Links give the ability to invoke a script (or any other type |
| 1470 | of executable) with multiple names, and having that script |
| 1471 | behave according to how it was invoked.</P |
| 1472 | ><DIV |
| 1473 | CLASS="EXAMPLE" |
| 1474 | ><HR><A |
| 1475 | NAME="HELLOL" |
| 1476 | ></A |
| 1477 | ><P |
| 1478 | ><B |
| 1479 | >Example 16-2. Hello or Good-bye</B |
| 1480 | ></P |
| 1481 | ><TABLE |
| 1482 | BORDER="0" |
| 1483 | BGCOLOR="#E0E0E0" |
| 1484 | WIDTH="90%" |
| 1485 | ><TR |
| 1486 | ><TD |
| 1487 | ><PRE |
| 1488 | CLASS="PROGRAMLISTING" |
| 1489 | > 1 #!/bin/bash |
| 1490 | 2 # hello.sh: Saying "hello" or "goodbye" |
| 1491 | 3 #+ depending on how script is invoked. |
| 1492 | 4 |
| 1493 | 5 # Make a link in current working directory ($PWD) to this script: |
| 1494 | 6 # ln -s hello.sh goodbye |
| 1495 | 7 # Now, try invoking this script both ways: |
| 1496 | 8 # ./hello.sh |
| 1497 | 9 # ./goodbye |
| 1498 | 10 |
| 1499 | 11 |
| 1500 | 12 HELLO_CALL=65 |
| 1501 | 13 GOODBYE_CALL=66 |
| 1502 | 14 |
| 1503 | 15 if [ $0 = "./goodbye" ] |
| 1504 | 16 then |
| 1505 | 17 echo "Good-bye!" |
| 1506 | 18 # Some other goodbye-type commands, as appropriate. |
| 1507 | 19 exit $GOODBYE_CALL |
| 1508 | 20 fi |
| 1509 | 21 |
| 1510 | 22 echo "Hello!" |
| 1511 | 23 # Some other hello-type commands, as appropriate. |
| 1512 | 24 exit $HELLO_CALL</PRE |
| 1513 | ></TD |
| 1514 | ></TR |
| 1515 | ></TABLE |
| 1516 | ><HR></DIV |
| 1517 | ></DD |
| 1518 | ><DT |
| 1519 | ><A |
| 1520 | NAME="MANREF" |
| 1521 | ></A |
| 1522 | ><B |
| 1523 | CLASS="COMMAND" |
| 1524 | >man</B |
| 1525 | >, <A |
| 1526 | NAME="INFOREF" |
| 1527 | ></A |
| 1528 | ><B |
| 1529 | CLASS="COMMAND" |
| 1530 | >info</B |
| 1531 | ></DT |
| 1532 | ><DD |
| 1533 | ><P |
| 1534 | >These commands access the manual and information pages on |
| 1535 | system commands and installed utilities. When available, the |
| 1536 | <I |
| 1537 | CLASS="FIRSTTERM" |
| 1538 | >info</I |
| 1539 | > pages usually contain more detailed |
| 1540 | descriptions than do the <I |
| 1541 | CLASS="FIRSTTERM" |
| 1542 | >man</I |
| 1543 | > pages.</P |
| 1544 | ><P |
| 1545 | >There have been various attempts at |
| 1546 | <SPAN |
| 1547 | CLASS="QUOTE" |
| 1548 | >"automating"</SPAN |
| 1549 | > the writing of <I |
| 1550 | CLASS="FIRSTTERM" |
| 1551 | >man |
| 1552 | pages</I |
| 1553 | >. For a script that makes a tentative first |
| 1554 | step in that direction, see <A |
| 1555 | HREF="contributed-scripts.html#MANED" |
| 1556 | >Example A-39</A |
| 1557 | >.</P |
| 1558 | ></DD |
| 1559 | ></DL |
| 1560 | ></DIV |
| 1561 | ></DIV |
| 1562 | ></DIV |
| 1563 | ><H3 |
| 1564 | CLASS="FOOTNOTES" |
| 1565 | >Notes</H3 |
| 1566 | ><TABLE |
| 1567 | BORDER="0" |
| 1568 | CLASS="FOOTNOTES" |
| 1569 | WIDTH="100%" |
| 1570 | ><TR |
| 1571 | ><TD |
| 1572 | ALIGN="LEFT" |
| 1573 | VALIGN="TOP" |
| 1574 | WIDTH="5%" |
| 1575 | ><A |
| 1576 | NAME="FTN.AEN10025" |
| 1577 | HREF="external.html#AEN10025" |
| 1578 | >[1]</A |
| 1579 | ></TD |
| 1580 | ><TD |
| 1581 | ALIGN="LEFT" |
| 1582 | VALIGN="TOP" |
| 1583 | WIDTH="95%" |
| 1584 | ><P |
| 1585 | >The <TT |
| 1586 | CLASS="OPTION" |
| 1587 | >-v</TT |
| 1588 | > option also orders the |
| 1589 | sort by <SPAN |
| 1590 | CLASS="emphasis" |
| 1591 | ><I |
| 1592 | CLASS="EMPHASIS" |
| 1593 | >upper- and lowercase prefixed</I |
| 1594 | ></SPAN |
| 1595 | > |
| 1596 | filenames.</P |
| 1597 | ></TD |
| 1598 | ></TR |
| 1599 | ><TR |
| 1600 | ><TD |
| 1601 | ALIGN="LEFT" |
| 1602 | VALIGN="TOP" |
| 1603 | WIDTH="5%" |
| 1604 | ><A |
| 1605 | NAME="FTN.AEN10228" |
| 1606 | HREF="external.html#AEN10228" |
| 1607 | >[2]</A |
| 1608 | ></TD |
| 1609 | ><TD |
| 1610 | ALIGN="LEFT" |
| 1611 | VALIGN="TOP" |
| 1612 | WIDTH="95%" |
| 1613 | ><P |
| 1614 | ><A |
| 1615 | NAME="DOTFILESREF" |
| 1616 | ></A |
| 1617 | ></P |
| 1618 | ><P |
| 1619 | ><I |
| 1620 | CLASS="FIRSTTERM" |
| 1621 | >Dotfiles</I |
| 1622 | > are files whose |
| 1623 | names begin with a <I |
| 1624 | CLASS="FIRSTTERM" |
| 1625 | >dot</I |
| 1626 | >, such as |
| 1627 | <TT |
| 1628 | CLASS="FILENAME" |
| 1629 | >~/.Xdefaults</TT |
| 1630 | >. Such filenames do |
| 1631 | not appear in a normal <B |
| 1632 | CLASS="COMMAND" |
| 1633 | >ls</B |
| 1634 | > listing |
| 1635 | (although an <B |
| 1636 | CLASS="COMMAND" |
| 1637 | >ls -a</B |
| 1638 | > will show |
| 1639 | them), and they cannot be deleted by an accidental |
| 1640 | <B |
| 1641 | CLASS="COMMAND" |
| 1642 | >rm -rf *</B |
| 1643 | >. Dotfiles are generally |
| 1644 | used as setup and configuration files in a user's |
| 1645 | home directory.</P |
| 1646 | ></TD |
| 1647 | ></TR |
| 1648 | ><TR |
| 1649 | ><TD |
| 1650 | ALIGN="LEFT" |
| 1651 | VALIGN="TOP" |
| 1652 | WIDTH="5%" |
| 1653 | ><A |
| 1654 | NAME="FTN.AEN10301" |
| 1655 | HREF="external.html#AEN10301" |
| 1656 | >[3]</A |
| 1657 | ></TD |
| 1658 | ><TD |
| 1659 | ALIGN="LEFT" |
| 1660 | VALIGN="TOP" |
| 1661 | WIDTH="95%" |
| 1662 | ><P |
| 1663 | >This particular feature may not yet be |
| 1664 | implemented in the version of the ext2/ext3 filesystem |
| 1665 | installed on your system. Check the documentation for |
| 1666 | your Linux distro.</P |
| 1667 | ></TD |
| 1668 | ></TR |
| 1669 | ></TABLE |
| 1670 | ><DIV |
| 1671 | CLASS="NAVFOOTER" |
| 1672 | ><HR |
| 1673 | ALIGN="LEFT" |
| 1674 | WIDTH="100%"><TABLE |
| 1675 | SUMMARY="Footer navigation table" |
| 1676 | WIDTH="100%" |
| 1677 | BORDER="0" |
| 1678 | CELLPADDING="0" |
| 1679 | CELLSPACING="0" |
| 1680 | ><TR |
| 1681 | ><TD |
| 1682 | WIDTH="33%" |
| 1683 | ALIGN="left" |
| 1684 | VALIGN="top" |
| 1685 | ><A |
| 1686 | HREF="internal.html" |
| 1687 | ACCESSKEY="P" |
| 1688 | >Prev</A |
| 1689 | ></TD |
| 1690 | ><TD |
| 1691 | WIDTH="34%" |
| 1692 | ALIGN="center" |
| 1693 | VALIGN="top" |
| 1694 | ><A |
| 1695 | HREF="index.html" |
| 1696 | ACCESSKEY="H" |
| 1697 | >Home</A |
| 1698 | ></TD |
| 1699 | ><TD |
| 1700 | WIDTH="33%" |
| 1701 | ALIGN="right" |
| 1702 | VALIGN="top" |
| 1703 | ><A |
| 1704 | HREF="moreadv.html" |
| 1705 | ACCESSKEY="N" |
| 1706 | >Next</A |
| 1707 | ></TD |
| 1708 | ></TR |
| 1709 | ><TR |
| 1710 | ><TD |
| 1711 | WIDTH="33%" |
| 1712 | ALIGN="left" |
| 1713 | VALIGN="top" |
| 1714 | >Internal Commands and Builtins</TD |
| 1715 | ><TD |
| 1716 | WIDTH="34%" |
| 1717 | ALIGN="center" |
| 1718 | VALIGN="top" |
| 1719 | ><A |
| 1720 | HREF="part4.html" |
| 1721 | ACCESSKEY="U" |
| 1722 | >Up</A |
| 1723 | ></TD |
| 1724 | ><TD |
| 1725 | WIDTH="33%" |
| 1726 | ALIGN="right" |
| 1727 | VALIGN="top" |
| 1728 | >Complex Commands</TD |
| 1729 | ></TR |
| 1730 | ></TABLE |
| 1731 | ></DIV |
| 1732 | ></BODY |
| 1733 | ></HTML |
| 1734 | > |