445 </b> |
225 </b> |
446 </p> |
226 </p> |
447 <dl> |
227 <dl> |
448 <dt> |
228 <dt> |
449 <span class="section"> |
229 <span class="section"> |
450 <a href="#id2704191"> |
230 <a href="#id2615138"> |
451 1. Introducing Linux |
231 1. Module Objectives: |
|
232 </a> |
|
233 </span> |
|
234 </dt> |
|
235 <dt> |
|
236 <span class="section"> |
|
237 <a href="#id2803399"> |
|
238 2. tar: |
452 </a> |
239 </a> |
453 </span> |
240 </span> |
454 </dt> |
241 </dt> |
455 <dd> |
242 <dd> |
456 <dl> |
243 <dl> |
457 <dt> |
244 <dt> |
458 <span class="section"> |
245 <span class="section"> |
459 <a href="#id2891886"> |
246 <a href="#id2803952"> |
460 1.1. Historical Background |
247 2.1. Introduction: |
461 </a> |
248 </a> |
462 </span> |
249 </span> |
463 </dt> |
250 </dt> |
464 <dt> |
251 <dt> |
465 <span class="section"> |
252 <span class="section"> |
466 <a href="#id2945127"> |
253 <a href="#id2856019"> |
467 1.2. Design and Implications |
254 2.2. Getting Started(go go go!): |
468 </a> |
255 </a> |
469 </span> |
256 </span> |
470 </dt> |
257 </dt> |
471 <dt> |
258 <dt> |
472 <span class="section"> |
259 <span class="section"> |
473 <a href="#id2945399"> |
260 <a href="#id2856298"> |
474 1.3. Reasons for Using Linux |
261 2.3. Further Reading for this section: |
475 </a> |
262 </a> |
476 </span> |
263 </span> |
477 </dt> |
264 </dt> |
478 </dl> |
265 </dl> |
479 </dd> |
266 </dd> |
480 <dt> |
267 <dt> |
481 <span class="section"> |
268 <span class="section"> |
482 <a href="#id2945531"> |
269 <a href="#id2856343"> |
483 2. Getting Started |
270 3. GZip: |
484 </a> |
271 </a> |
485 </span> |
272 </span> |
486 </dt> |
273 </dt> |
487 <dd> |
274 <dd> |
488 <dl> |
275 <dl> |
489 <dt> |
276 <dt> |
490 <span class="section"> |
277 <span class="section"> |
491 <a href="#id2945540"> |
278 <a href="#id2856368"> |
492 2.1. Logging in, activating the user interface and logging out |
279 3.1. Get going: |
|
280 </a> |
|
281 </span> |
|
282 </dt> |
|
283 <dt> |
|
284 <span class="section"> |
|
285 <a href="#id2856622"> |
|
286 3.2. Further Reading for this section: |
493 </a> |
287 </a> |
494 </span> |
288 </span> |
495 </dt> |
289 </dt> |
496 </dl> |
290 </dl> |
497 </dd> |
291 </dd> |
498 <dt> |
292 <dt> |
499 <span class="section"> |
293 <span class="section"> |
500 <a href="#id2945817"> |
294 <a href="#id2856676"> |
501 3. Basic Commands |
295 4. File Comparisons: |
502 </a> |
296 </a> |
503 </span> |
297 </span> |
504 </dt> |
298 </dt> |
505 <dd> |
299 <dd> |
506 <dl> |
300 <dl> |
507 <dt> |
301 <dt> |
508 <span class="section"> |
302 <span class="section"> |
509 <a href="#id2945826"> |
303 <a href="#id2856693"> |
510 3.1. ls |
304 4.1. cmp: |
511 </a> |
305 </a> |
512 </span> |
306 </span> |
513 </dt> |
307 </dt> |
514 <dt> |
308 <dt> |
515 <span class="section"> |
309 <span class="section"> |
516 <a href="#id2945987"> |
310 <a href="#id2856735"> |
517 3.2. date |
311 4.2. diff: |
518 </a> |
312 </a> |
519 </span> |
313 </span> |
520 </dt> |
314 </dt> |
521 <dt> |
315 <dt> |
522 <span class="section"> |
316 <span class="section"> |
523 <a href="#id2946047"> |
317 <a href="#id2856817"> |
524 3.3. cd |
318 4.3. comm: |
525 </a> |
|
526 </span> |
|
527 </dt> |
|
528 <dt> |
|
529 <span class="section"> |
|
530 <a href="#id2946147"> |
|
531 3.4. who |
|
532 </a> |
|
533 </span> |
|
534 </dt> |
|
535 <dt> |
|
536 <span class="section"> |
|
537 <a href="#id2946229"> |
|
538 3.5. mkdir |
|
539 </a> |
319 </a> |
540 </span> |
320 </span> |
541 </dt> |
321 </dt> |
542 </dl> |
322 </dl> |
543 </dd> |
323 </dd> |
544 <dt> |
324 <dt> |
545 <span class="section"> |
325 <span class="section"> |
546 <a href="#id2946395"> |
326 <a href="#id2856847"> |
547 4. Getting Help |
327 5. Environment Variables: |
548 </a> |
328 </a> |
549 </span> |
329 </span> |
550 </dt> |
330 </dt> |
551 <dd> |
331 <dd> |
552 <dl> |
332 <dl> |
553 <dt> |
333 <dt> |
554 <span class="section"> |
334 <span class="section"> |
555 <a href="#id2946403"> |
335 <a href="#id2856946"> |
556 4.1. apropos and whatis |
336 5.1. Further Reading: |
557 </a> |
|
558 </span> |
|
559 </dt> |
|
560 <dt> |
|
561 <span class="section"> |
|
562 <a href="#id2946487"> |
|
563 4.2. man |
|
564 </a> |
|
565 </span> |
|
566 </dt> |
|
567 <dt> |
|
568 <span class="section"> |
|
569 <a href="#id2946679"> |
|
570 4.3. info |
|
571 </a> |
|
572 </span> |
|
573 </dt> |
|
574 <dt> |
|
575 <span class="section"> |
|
576 <a href="#id2946765"> |
|
577 4.4. --help |
|
578 </a> |
337 </a> |
579 </span> |
338 </span> |
580 </dt> |
339 </dt> |
581 </dl> |
340 </dl> |
582 </dd> |
341 </dd> |
583 <dt> |
342 <dt> |
584 <span class="section"> |
343 <span class="section"> |
585 <a href="#id2946817"> |
344 <a href="#id2857001"> |
586 5. Basic file handling |
345 6. Shell Scripting: |
587 </a> |
346 </a> |
588 </span> |
347 </span> |
589 </dt> |
348 </dt> |
590 <dd> |
349 <dd> |
591 <dl> |
350 <dl> |
592 <dt> |
351 <dt> |
593 <span class="section"> |
352 <span class="section"> |
594 <a href="#id2946826"> |
353 <a href="#id2857009"> |
595 5.1. cp |
354 6.1. Basics: |
596 </a> |
355 </a> |
597 </span> |
356 </span> |
598 </dt> |
357 </dt> |
599 <dt> |
358 <dt> |
600 <span class="section"> |
359 <span class="section"> |
601 <a href="#id2947135"> |
360 <a href="#id2855367"> |
602 5.2. mv |
361 6.2. Shell Arithmetic: |
603 </a> |
362 </a> |
604 </span> |
363 </span> |
605 </dt> |
364 </dt> |
606 <dt> |
365 <dt> |
607 <span class="section"> |
366 <span class="section"> |
608 <a href="#id2944921"> |
367 <a href="#id2857386"> |
609 5.3. rm |
368 6.3. if else construct: |
|
369 </a> |
|
370 </span> |
|
371 </dt> |
|
372 <dt> |
|
373 <span class="section"> |
|
374 <a href="#id2857492"> |
|
375 6.4. Loops |
|
376 </a> |
|
377 </span> |
|
378 </dt> |
|
379 <dt> |
|
380 <span class="section"> |
|
381 <a href="#id2857738"> |
|
382 6.5. Functions |
|
383 </a> |
|
384 </span> |
|
385 </dt> |
|
386 <dt> |
|
387 <span class="section"> |
|
388 <a href="#id2857809"> |
|
389 6.6. Further Reading: |
610 </a> |
390 </a> |
611 </span> |
391 </span> |
612 </dt> |
392 </dt> |
613 </dl> |
393 </dl> |
614 </dd> |
394 </dd> |
615 <dt> |
|
616 <span class="section"> |
|
617 <a href="#id2947569"> |
|
618 6. Command Line Arguments |
|
619 </a> |
|
620 </span> |
|
621 </dt> |
|
622 <dt> |
|
623 <span class="section"> |
|
624 <a href="#id2947661"> |
|
625 7. Basic Text Processing |
|
626 </a> |
|
627 </span> |
|
628 </dt> |
|
629 <dd> |
|
630 <dl> |
|
631 <dt> |
|
632 <span class="section"> |
|
633 <a href="#id2947670"> |
|
634 7.1. head |
|
635 </a> |
|
636 </span> |
|
637 </dt> |
|
638 <dt> |
|
639 <span class="section"> |
|
640 <a href="#id2947756"> |
|
641 7.2. tail |
|
642 </a> |
|
643 </span> |
|
644 </dt> |
|
645 <dt> |
|
646 <span class="section"> |
|
647 <a href="#id2947883"> |
|
648 7.3. cut |
|
649 </a> |
|
650 </span> |
|
651 </dt> |
|
652 <dt> |
|
653 <span class="section"> |
|
654 <a href="#id2947969"> |
|
655 7.4. paste |
|
656 </a> |
|
657 </span> |
|
658 </dt> |
|
659 </dl> |
|
660 </dd> |
|
661 <dt> |
|
662 <span class="section"> |
|
663 <a href="#id2948101"> |
|
664 8. Shell Meta Characters |
|
665 </a> |
|
666 </span> |
|
667 </dt> |
|
668 <dt> |
|
669 <span class="section"> |
|
670 <a href="#id2948205"> |
|
671 9. Looking At Files |
|
672 </a> |
|
673 </span> |
|
674 </dt> |
|
675 <dd> |
|
676 <dl> |
|
677 <dt> |
|
678 <span class="section"> |
|
679 <a href="#id2948213"> |
|
680 9.1. cat |
|
681 </a> |
|
682 </span> |
|
683 </dt> |
|
684 <dt> |
|
685 <span class="section"> |
|
686 <a href="#id2948400"> |
|
687 9.2. more |
|
688 </a> |
|
689 </span> |
|
690 </dt> |
|
691 <dt> |
|
692 <span class="section"> |
|
693 <a href="#id2948510"> |
|
694 9.3. less |
|
695 </a> |
|
696 </span> |
|
697 </dt> |
|
698 </dl> |
|
699 </dd> |
|
700 <dt> |
|
701 <span class="section"> |
|
702 <a href="#id2948748"> |
|
703 10. Directory Structure |
|
704 </a> |
|
705 </span> |
|
706 </dt> |
|
707 <dd> |
|
708 <dl> |
|
709 <dt> |
|
710 <span class="section"> |
|
711 <a href="#id2949017"> |
|
712 10.1. man hier |
|
713 </a> |
|
714 </span> |
|
715 </dt> |
|
716 <dt> |
|
717 <span class="section"> |
|
718 <a href="#id2949033"> |
|
719 10.2. ls -l |
|
720 </a> |
|
721 </span> |
|
722 </dt> |
|
723 </dl> |
|
724 </dd> |
|
725 <dt> |
|
726 <span class="section"> |
|
727 <a href="#id2949056"> |
|
728 11. Permissions and Ownership |
|
729 </a> |
|
730 </span> |
|
731 </dt> |
|
732 <dd> |
|
733 <dl> |
|
734 <dt> |
|
735 <span class="section"> |
|
736 <a href="#id2949065"> |
|
737 11.1. chmod |
|
738 </a> |
|
739 </span> |
|
740 </dt> |
|
741 </dl> |
|
742 </dd> |
|
743 <dt> |
|
744 <span class="section"> |
|
745 <a href="#id2949829"> |
|
746 12. Redirection and Piping |
|
747 </a> |
|
748 </span> |
|
749 </dt> |
|
750 <dd> |
|
751 <dl> |
|
752 <dt> |
|
753 <span class="section"> |
|
754 <a href="#id2949866"> |
|
755 12.1. Redirecting standard input and standard output |
|
756 </a> |
|
757 </span> |
|
758 </dt> |
|
759 <dt> |
|
760 <span class="section"> |
|
761 <a href="#id2949936"> |
|
762 12.2. Piping |
|
763 </a> |
|
764 </span> |
|
765 </dt> |
|
766 <dt> |
|
767 <span class="section"> |
|
768 <a href="#id2949997"> |
|
769 12.3. Redirecting to and from the standard file handles |
|
770 </a> |
|
771 </span> |
|
772 </dt> |
|
773 <dt> |
|
774 <span class="section"> |
|
775 <a href="#id2950201"> |
|
776 12.4. Chained pipelines |
|
777 </a> |
|
778 </span> |
|
779 </dt> |
|
780 <dt> |
|
781 <span class="section"> |
|
782 <a href="#id2950235"> |
|
783 12.5. Redirect to multiple outputs |
|
784 </a> |
|
785 </span> |
|
786 </dt> |
|
787 </dl> |
|
788 </dd> |
|
789 <dt> |
|
790 <span class="section"> |
|
791 <a href="#id2950266"> |
|
792 13. More Text Processing |
|
793 </a> |
|
794 </span> |
|
795 </dt> |
|
796 <dd> |
|
797 <dl> |
|
798 <dt> |
|
799 <span class="section"> |
|
800 <a href="#id2950274"> |
|
801 13.1. grep |
|
802 </a> |
|
803 </span> |
|
804 </dt> |
|
805 <dt> |
|
806 <span class="section"> |
|
807 <a href="#id2950463"> |
|
808 13.2. tr |
|
809 </a> |
|
810 </span> |
|
811 </dt> |
|
812 </dl> |
|
813 </dd> |
|
814 <dt> |
|
815 <span class="section"> |
|
816 <a href="#id2950613"> |
|
817 14. Elementary Regex |
|
818 </a> |
|
819 </span> |
|
820 </dt> |
|
821 <dd> |
|
822 <dl> |
|
823 <dt> |
|
824 <span class="section"> |
|
825 <a href="#id2950823"> |
|
826 14.1. Lazy quantification |
|
827 </a> |
|
828 </span> |
|
829 </dt> |
|
830 </dl> |
|
831 </dd> |
|
832 <dt> |
|
833 <span class="section"> |
|
834 <a href="#id2950898"> |
|
835 15. One Liners |
|
836 </a> |
|
837 </span> |
|
838 </dt> |
|
839 </dl> |
395 </dl> |
840 </div> |
396 </div> |
841 <div class="section" title="1.Introducing Linux"> |
397 <div class="section" title="1.Module Objectives:"> |
842 <div class="titlepage"> |
398 <div class="titlepage"> |
843 <div> |
399 <div> |
844 <div> |
400 <div> |
845 <h2 class="title" style="clear: both"> |
401 <h2 class="title" style="clear: both"> |
846 <a name="id2704191"> |
402 <a name="id2615138"> |
847 </a> |
403 </a> |
848 1.Introducing Linux |
404 1.Module Objectives: |
849 </h2> |
405 </h2> |
850 </div> |
406 </div> |
851 </div> |
407 </div> |
852 </div> |
408 </div> |
853 <p id="ch11Section_5_1"> |
409 <p id="ch11Section_5_1"> |
854 (Attribution : A significant chunk of the content under this section is based on data from Wikipedia and the Linux Documentation Project) |
410 After successfully completing this module a participant will be able to: |
855 </p> |
411 </p> |
856 <p id="ch11Section_5_2"> |
412 <pre class="programlisting"> |
857 Linux (usually pronounced lnks') is a generic term referring to Unix-like computer operating systems based on the Linux kernel, where a kernel is the intermediate layer between the hardware and the applications. The kernel is, on an abstract level, the core of (most) operating systems, that manages the various system resources. The development of the Linux OS is considered the basis for Free and Open Source Software (FOSS) collaboration since typically the underlying source code can be used, modified freely, and redistributed by anyone under the terms of the GNU (a recursive acronym for "GNU's Not Unix!") Global Public License (GPL) and other free software licences. This freedom to access and reuse various components of a system, is one of the primary reasons for the popularity of Linux. |
413 - Understand |
858 </p> |
414 * What are archives and zipped files U |
859 <p id="ch11Section_5_3"> |
415 * What are environment variables U |
860 Linux is installed on a variety of computer hardware, that include mobile phones, embedded devices and supercomputers, but is infamous for its use in servers. |
416 * What are Shell Scripts U |
861 </p> |
417 - Able to use file comparison commands like Ap |
862 <p id="ch11Section_5_4"> |
418 diff, cmp, comm |
863 The name "Linux" comes from the Linux kernel, originally written in 1991 by Linus Torvalds. The rest of the system usually comprises components such as the Apache HTTP Server, the X Window System, the GNOME and KDE desktop environments, and utilities and libraries from the GNU Project (announced in 1983 by Richard Stallman). Commonly-used applications with desktop Linux systems include the Mozilla Firefox web-browser and the OpenOffice.org office application suite. The GNU contribution is the basis for the Free Software Foundation's preferred name GNU/Linux. The kernel's mascot is a penguin named "Tux". Mozilla Firefox and OpenOffice.org are open-source projects which can be run on most Operating Systems, including proprietary ones. |
419 - Create and extract archives(.tar files) and zipped files(.gz) Ap |
864 </p> |
420 - Set/Modify environment as per need Ap |
865 <div class="section" title="1.1.Historical Background"> |
421 - Create shell scripts to automate tasks. Ap |
|
422 </pre> |
|
423 </div> |
|
424 <div class="section" title="2.tar:"> |
|
425 <div class="titlepage"> |
|
426 <div> |
|
427 <div> |
|
428 <h2 class="title" style="clear: both"> |
|
429 <a name="id2803399"> |
|
430 </a> |
|
431 2.tar: |
|
432 </h2> |
|
433 </div> |
|
434 </div> |
|
435 </div> |
|
436 <div class="section" title="2.1.Introduction:"> |
866 <div class="titlepage"> |
437 <div class="titlepage"> |
867 <div> |
438 <div> |
868 <div> |
439 <div> |
869 <h3 class="title"> |
440 <h3 class="title"> |
870 <a name="id2891886"> |
441 <a name="id2803952"> |
871 </a> |
442 </a> |
872 1.1.Historical Background |
443 2.1.Introduction: |
873 </h3> |
444 </h3> |
874 </div> |
445 </div> |
875 </div> |
446 </div> |
876 </div> |
447 </div> |
877 <div class="section" title="1.1.1.Events leading to the creation"> |
448 <p id="ch11Section_5_2"> |
|
449 In world of Linux based distribution, |
|
450 <span class="emphasis"> |
|
451 <em> |
|
452 tarballs |
|
453 </em> |
|
454 </span> |
|
455 is the term which pops up very often. It is part of the GNU project and comes as part of every distribution of GNU/Linux. Tarball is like defacto standard for releasing source code for free software. Some of common use of |
|
456 <span class="emphasis"> |
|
457 <em> |
|
458 tar |
|
459 </em> |
|
460 </span> |
|
461 archives is to: |
|
462 <span class="emphasis"> |
|
463 <em> |
|
464 Store, backup, and transport |
|
465 </em> |
|
466 </span> |
|
467 . |
|
468 </p> |
|
469 <p id="ch11Section_5_3"> |
|
470 GNU tar creates and manipulates archives which are actually collections of many other files; the program provides users with an organized and systematic method for controlling a large amount of data. It is basically form of creating archive by concatenating one or more files. |
|
471 </p> |
|
472 </div> |
|
473 <div class="section" title="2.2.Getting Started(go go go!):"> |
|
474 <div class="titlepage"> |
|
475 <div> |
|
476 <div> |
|
477 <h3 class="title"> |
|
478 <a name="id2856019"> |
|
479 </a> |
|
480 2.2.Getting Started(go go go!): |
|
481 </h3> |
|
482 </div> |
|
483 </div> |
|
484 </div> |
|
485 <p id="ch11Section_5_4"> |
|
486 As mentioned previously and if not, |
|
487 <span class="emphasis"> |
|
488 <em> |
|
489 The best way to get started with any command line tool of Linux is to use "man". |
|
490 </em> |
|
491 </span> |
|
492 </p> |
|
493 <pre class="programlisting"> |
|
494 $ man tar |
|
495 </pre> |
|
496 <p id="ch11Section_5_5"> |
|
497 or try these commands(the output may vary with different installations): |
|
498 </p> |
|
499 <pre class="programlisting"> |
|
500 $ tar --version |
|
501 tar (GNU tar) 1.20 |
|
502 Copyright (C) 2008 Free Software Foundation, Inc. |
|
503 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> |
|
504 This is free software: you are free to change and redistribute it. |
|
505 There is NO WARRANTY, to the extent permitted by law. |
|
506 |
|
507 Written by John Gilmore and Jay Fenlason. |
|
508 |
|
509 $ tar --help |
|
510 Usage: tar [OPTION...] [FILE]... |
|
511 GNU `tar' saves many files together into a single tape or disk archive, and can |
|
512 restore individual files from the archive. |
|
513 Examples: |
|
514 tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. |
|
515 tar -tvf archive.tar # List all files in archive.tar verbosely. |
|
516 tar -xf archive.tar # Extract all files from archive.tar. |
|
517 ____________ |
|
518 </pre> |
|
519 <div class="section" title="2.2.1.Creating a tar archive:"> |
878 <div class="titlepage"> |
520 <div class="titlepage"> |
879 <div> |
521 <div> |
880 <div> |
522 <div> |
881 <h4 class="title"> |
523 <h4 class="title"> |
882 <a name="id2891852"> |
524 <a name="id2856073"> |
883 </a> |
525 </a> |
884 1.1.1.Events leading to the creation |
526 2.2.1.Creating a tar archive: |
885 </h4> |
527 </h4> |
886 </div> |
528 </div> |
887 </div> |
529 </div> |
888 </div> |
530 </div> |
|
531 <p id="ch11Section_5_6"> |
|
532 We will do some off-the road activity for this exercise. We will use an interesting command |
|
533 <span class="emphasis"> |
|
534 <em> |
|
535 fortune |
|
536 </em> |
|
537 </span> |
|
538 for creating our practice files and then performing archives of those files and directories. Content of the files would vary for users, as fortune works like that. |
|
539 </p> |
|
540 <pre class="programlisting"> |
|
541 $ mkdir fortune-files |
|
542 $ cd fortune-files/ |
|
543 $ fortune > first.txt |
|
544 $ cat first.txt |
|
545 Expect the worst, it's the least you can do. |
|
546 $ fortune > second.txt |
|
547 $ fortune > third.txt |
|
548 $ ls |
|
549 first.txt second.txt third.txt |
|
550 </pre> |
|
551 <p id="ch11Section_5_7"> |
|
552 By now we have three txt files, with some random fortune content. To create a tar archive of these files we can use any of following commands according to ones convenience: |
|
553 </p> |
|
554 <pre class="programlisting"> |
|
555 $ tar --create --verbose --file=allfiles.tar first.txt second.txt third.txt |
|
556 first.txt |
|
557 second.txt |
|
558 third.txt |
|
559 $ ls |
|
560 allfiles.tar first.txt second.txt third.txt |
|
561 </pre> |
|
562 <p id="ch11Section_5_8"> |
|
563 allfiles.tar is our required tar archive of all the rest of files(or archive of files mentioned in command line). Other form of the previous command are: |
|
564 </p> |
|
565 <pre class="programlisting"> |
|
566 $ tar -c -v -f allfiles.tar first.txt second.txt third.txt |
|
567 </pre> |
|
568 <p id="ch11Section_5_9"> |
|
569 or |
|
570 </p> |
|
571 <pre class="programlisting"> |
|
572 $ tar -cvf allfiles.tar first.txt second.txt third.txt |
|
573 </pre> |
|
574 <p id="ch11Section_5_a"> |
|
575 The general format for creating a tar archive is: |
|
576 </p> |
|
577 <pre class="programlisting"> |
|
578 tar [OPTION...] [FILE]... |
|
579 </pre> |
|
580 <p id="ch11Section_5_b"> |
|
581 For our command are using these options: |
|
582 </p> |
889 <div class="itemizedlist"> |
583 <div class="itemizedlist"> |
890 <ul class="itemizedlist" type="*"> |
584 <ul class="itemizedlist" type="*"> |
891 <li class="listitem" style="list-style-type: *"> |
585 <li class="listitem" style="list-style-type: *"> |
892 <p id="ch11Section_5_5"> |
586 <p id="ch11Section_5_c"> |
893 The Unix operating system was developed in the 1960s and released for public use in 1970. Its accessibility and portability caused it to be widely adopted, copied and modified by academic institutions and businesses. Its design became influential to authors of other systems. Other free operating systems include the Berkeley Software Distribution (BSD), developed at the University of California at Berkeley, and MINIX which was released by Andrew S. Tanenbaum. The development and adoption of BSD and MINIX were limited due to various reasons, and this lack of a widely-adopted and free kernel triggered Linus Torvalds into starting his project. |
587 -c to Create the archive. |
894 </p> |
588 </p> |
895 </li> |
589 </li> |
896 <li class="listitem" style="list-style-type: *"> |
590 <li class="listitem" style="list-style-type: *"> |
897 <p id="ch11Section_5_6"> |
591 <p id="ch11Section_5_d"> |
898 In 1983, Richard Stallman started the GNU project with the goal of creating a free UNIX-like operating system. As part of this work, he wrote the GNU General Public License (GPL). By the early 1990s there was almost enough available software to create a full operating system. However, the GNU kernel, called Hurd, failed to attract enough attention from developers leaving GNU incomplete. |
592 -v for Verbose mode, to get the names of the files as they are archived. |
|
593 </p> |
|
594 </li> |
|
595 <li class="listitem" style="list-style-type: *"> |
|
596 <p id="ch11Section_5_e"> |
|
597 -f mentioning the file name of the resulting tar archive. |
899 </p> |
598 </p> |
900 </li> |
599 </li> |
901 </ul> |
600 </ul> |
902 </div> |
601 </div> |
903 </div> |
602 <p id="ch11Section_5_f"> |
904 <div class="section" title="1.1.2.The Creation of Linux"> |
603 To create archive of folder itself try this: |
|
604 </p> |
|
605 <pre class="programlisting"> |
|
606 $ tar -cvf fortune.tar fortune/ |
|
607 </pre> |
|
608 <p id="ch11Section_5_10"> |
|
609 To add files to existing tar archive, option |
|
610 <span class="emphasis"> |
|
611 <em> |
|
612 `r` |
|
613 </em> |
|
614 </span> |
|
615 is used: |
|
616 </p> |
|
617 <pre class="programlisting"> |
|
618 $ fortune > fourth.txt |
|
619 $ tar -r fourth.txt -vf allfiles.tar |
|
620 fourth.txt |
|
621 </pre> |
|
622 <p id="ch11Section_5_11"> |
|
623 There are other options too available for explicitly mentioning the position of archive, use |
|
624 <span class="emphasis"> |
|
625 <em> |
|
626 tar --help |
|
627 </em> |
|
628 </span> |
|
629 for getting all the details. |
|
630 </p> |
|
631 <p id="ch11Section_5_12"> |
|
632 Similarly to remove file from archive use |
|
633 <span class="emphasis"> |
|
634 <em> |
|
635 --delete |
|
636 </em> |
|
637 </span> |
|
638 option: |
|
639 </p> |
|
640 <pre class="programlisting"> |
|
641 $ tar --delete second.txt -f allfiles.tar |
|
642 $ tar -tf allfiles.tar |
|
643 first.txt |
|
644 third.txt |
|
645 fourth.txt |
|
646 </pre> |
|
647 </div> |
|
648 <div class="section" title="2.2.2.Listing the files of archive:"> |
905 <div class="titlepage"> |
649 <div class="titlepage"> |
906 <div> |
650 <div> |
907 <div> |
651 <div> |
908 <h4 class="title"> |
652 <h4 class="title"> |
909 <a name="id2945082"> |
653 <a name="id2856213"> |
910 </a> |
654 </a> |
911 1.1.2.The Creation of Linux |
655 2.2.2.Listing the files of archive: |
912 </h4> |
656 </h4> |
913 </div> |
657 </div> |
914 </div> |
658 </div> |
915 </div> |
659 </div> |
916 <p id="ch11Section_5_7"> |
660 <p id="ch11Section_5_13"> |
917 In 1991, Linus Torvalds began a project at the University of Helsinki that later became the Linux kernel. It was initially a terminal (command-line) emulator, which Torvalds used to access the large UNIX servers of the university. He wrote the program targeting just the hardware he was using and independent of an operating system because he wanted to use the functions of his computer with an 80386 processor. Development was done on Minix using the GNU C compiler. This application is still the main choice for compiling Linux today (although the code can be built with other compilers, such as the Intel C Compiler). |
661 Once files are archived, tar command have the |
918 </p> |
662 <span class="emphasis"> |
919 <p id="ch11Section_5_8"> |
663 <em> |
920 Torvalds continues to direct the development of the kernel. Stallman heads the Free Software Foundation, which in turn supports the GNU components. Finally, individuals and corporations develop third-party non-GNU components, which constitute a vast body of work and including kernel modules, and user applications and libraries. Linux vendors and communities combine and distribute the kernel, GNU components, and non-GNU components, with additional package management software in the form of Linux distributions. |
664 `t` |
921 </p> |
665 </em> |
|
666 </span> |
|
667 option, for Listing all files in the tar file: |
|
668 </p> |
|
669 <pre class="programlisting"> |
|
670 $ tar tf allfiles.tar |
|
671 first.txt |
|
672 second.txt |
|
673 third.txt |
|
674 </pre> |
|
675 <p id="ch11Section_5_14"> |
|
676 <span class="strong"> |
|
677 <strong> |
|
678 //this is not working for me in some cases :( |
|
679 </strong> |
|
680 </span> |
|
681 </p> |
|
682 <p id="ch11Section_5_15"> |
|
683 To locate a particular file among the archive mention its name after |
|
684 <span class="emphasis"> |
|
685 <em> |
|
686 t |
|
687 </em> |
|
688 </span> |
|
689 option. |
|
690 </p> |
|
691 <pre class="programlisting"> |
|
692 $ tar t second.txt allfiles.tar |
|
693 second.txt |
|
694 </pre> |
|
695 <p id="ch11Section_5_16"> |
|
696 one can also use elementary regex for locating the file, so in previous case even second.* will also return the same result. |
|
697 </p> |
|
698 </div> |
|
699 <div class="section" title="2.2.3.Extracting files from archive:"> |
|
700 <div class="titlepage"> |
|
701 <div> |
|
702 <div> |
|
703 <h4 class="title"> |
|
704 <a name="id2856263"> |
|
705 </a> |
|
706 2.2.3.Extracting files from archive: |
|
707 </h4> |
|
708 </div> |
|
709 </div> |
|
710 </div> |
|
711 <p id="ch11Section_5_17"> |
|
712 To extract the content from a archive, use |
|
713 <span class="emphasis"> |
|
714 <em> |
|
715 `x` |
|
716 </em> |
|
717 </span> |
|
718 option: |
|
719 </p> |
|
720 <pre class="programlisting"> |
|
721 $ mkdir extract |
|
722 $ cp allfiles.tar extract/ |
|
723 $ cd extract |
|
724 $ tar -xvf allfiles.tar |
|
725 first.txt |
|
726 second.txt |
|
727 third.txt |
|
728 </pre> |
|
729 <p id="ch11Section_5_18"> |
|
730 To extract any particular file from archive, mention the name of file after |
|
731 <span class="emphasis"> |
|
732 <em> |
|
733 x |
|
734 </em> |
|
735 </span> |
|
736 option: |
|
737 </p> |
|
738 <pre class="programlisting"> |
|
739 $ tar -x second.txt -vf allfiles.tar |
|
740 second.txt |
|
741 </pre> |
922 </div> |
742 </div> |
923 </div> |
743 </div> |
924 <div class="section" title="1.2.Design and Implications"> |
744 <div class="section" title="2.3.Further Reading for this section:"> |
925 <div class="titlepage"> |
745 <div class="titlepage"> |
926 <div> |
746 <div> |
927 <div> |
747 <div> |
928 <h3 class="title"> |
748 <h3 class="title"> |
929 <a name="id2945127"> |
749 <a name="id2856298"> |
930 </a> |
750 </a> |
931 1.2.Design and Implications |
751 2.3.Further Reading for this section: |
932 </h3> |
752 </h3> |
933 </div> |
753 </div> |
934 </div> |
754 </div> |
935 </div> |
755 </div> |
936 <p id="ch11Section_5_9"> |
756 <div class="itemizedlist"> |
937 A Linux-based system is a modular Unix-like operating system, deriving much of its basic design from principles established in Unix earlier. Such a system uses a monolithic kernel, called the Linux kernel, which handles process control, networking, and peripheral and file system access. Device drivers are integrated directly with the kernel. Separate projects that interface with the kernel provide much of the system's higher-level functionality. The GNU userland is an important part of most Linux-based systems, providing the most common implementation of the C library, a popular shell, and many of the common Unix tools which carry out many basic operating system tasks. The graphical user interface (or GUI) used by most Linux systems is based on the "X Window System". |
757 <ul class="itemizedlist" type="*"> |
938 </p> |
758 <li class="listitem" style="list-style-type: *"> |
939 <div class="section" title="1.2.1.User Interface"> |
759 <p id="ch11Section_5_19"> |
|
760 </p> |
|
761 <div class="reference"> |
|
762 <div class="titlepage"> |
|
763 <hr /> |
|
764 </div> |
|
765 http://en.wikipedia.org/wiki/Tar_(file_format |
|
766 </div> |
|
767 <p> |
|
768 ) |
|
769 </p> |
|
770 </li> |
|
771 <li class="listitem" style="list-style-type: *"> |
|
772 <p id="ch11Section_5_1a"> |
|
773 </p> |
|
774 <div class="reference"> |
|
775 <div class="titlepage"> |
|
776 <hr /> |
|
777 </div> |
|
778 http://www.gnu.org/software/tar/manual/tar.html |
|
779 </div> |
|
780 </li> |
|
781 <li class="listitem" style="list-style-type: *"> |
|
782 <p id="ch11Section_5_1b"> |
|
783 </p> |
|
784 <div class="reference"> |
|
785 <div class="titlepage"> |
|
786 <hr /> |
|
787 </div> |
|
788 http://linuxreviews.org/beginner/ |
|
789 </div> |
|
790 </li> |
|
791 </ul> |
|
792 </div> |
|
793 </div> |
|
794 </div> |
|
795 <div class="section" title="3.GZip:"> |
|
796 <div class="titlepage"> |
|
797 <div> |
|
798 <div> |
|
799 <h2 class="title" style="clear: both"> |
|
800 <a name="id2856343"> |
|
801 </a> |
|
802 3.GZip: |
|
803 </h2> |
|
804 </div> |
|
805 </div> |
|
806 </div> |
|
807 <p id="ch11Section_5_1c"> |
|
808 Tar creates archives but it does not compress data by itself unless specified explicitly. Hence all the archive we create using tar command, is simply of the size of total size of all individual files. With Linux there is a compression tool known as |
|
809 <span class="emphasis"> |
|
810 <em> |
|
811 gzip |
|
812 </em> |
|
813 </span> |
|
814 which is used to reduce the size of files mentioned. Whenever possible, each file is replaced by one with the extension .gz', so unlike `tar this command would |
|
815 <span class="emphasis"> |
|
816 <em> |
|
817 replace the existing file |
|
818 </em> |
|
819 </span> |
|
820 . |
|
821 </p> |
|
822 <div class="section" title="3.1.Get going:"> |
|
823 <div class="titlepage"> |
|
824 <div> |
|
825 <div> |
|
826 <h3 class="title"> |
|
827 <a name="id2856368"> |
|
828 </a> |
|
829 3.1.Get going: |
|
830 </h3> |
|
831 </div> |
|
832 </div> |
|
833 </div> |
|
834 <p id="ch11Section_5_1d"> |
|
835 As usual first commands to check out with gzip are |
|
836 <span class="emphasis"> |
|
837 <em> |
|
838 man |
|
839 </em> |
|
840 </span> |
|
841 and |
|
842 <span class="emphasis"> |
|
843 <em> |
|
844 help |
|
845 </em> |
|
846 </span> |
|
847 , |
|
848 </p> |
|
849 <pre class="programlisting"> |
|
850 $ man gzip |
|
851 $ gzip --help |
|
852 </pre> |
|
853 <p id="ch11Section_5_1e"> |
|
854 Creating a zip of a bunch of files is fairly trivial, it can be done simply via: |
|
855 </p> |
|
856 <pre class="programlisting"> |
|
857 $ gzip [OPTION]... [FILE]... |
|
858 </pre> |
|
859 <div class="section" title="3.1.1.Creating zip files:"> |
940 <div class="titlepage"> |
860 <div class="titlepage"> |
941 <div> |
861 <div> |
942 <div> |
862 <div> |
943 <h4 class="title"> |
863 <h4 class="title"> |
944 <a name="id2945151"> |
864 <a name="id2856399"> |
945 </a> |
865 </a> |
946 1.2.1.User Interface |
866 3.1.1.Creating zip files: |
947 </h4> |
867 </h4> |
948 </div> |
868 </div> |
949 </div> |
869 </div> |
950 </div> |
870 </div> |
951 <p id="ch11Section_5_a"> |
871 <p id="ch11Section_5_1f"> |
952 Users can control a Linux-based system through a command line interface (or CLI), a graphical user interface (or GUI), or through controls attached to the associated hardware (this is common for embedded systems). For desktop systems, the default mode is usually the GUI. On desktop machines, "KDE", "GNOME" and "Xfce" are the most popular user interfaces,though a variety of additional user interfaces exist. Most popular user interfaces run on top of the "X Window System" (or X), which enables a graphical application running on one machine to be displayed and controlled from another in a network. |
872 Continuing from previous set of files and setup, we will like to zip them and hence the command would be: |
953 </p> |
873 </p> |
954 <p id="ch11Section_5_b"> |
874 <pre class="programlisting"> |
955 A Linux system also provides a CLI of some sort through a shell, which is the traditional way of interacting with a Unix system. A Linux distribution specialized for servers may use the CLI as its only interface. A headless system (system run without even a monitor) can be controlled by the command line via a remote-control protocol such as SSH or telnet. The CLI is particularly suited for automation of repetitive or delayed tasks, and provides very simple inter-process communication. A graphical terminal emulator program is often used to access the CLI from a Linux desktop. |
875 $ gzip first.txt fourth.txt second.txt third.txt |
956 </p> |
876 $ ls |
957 </div> |
877 allfiles.tar first.txt.gz fourth.txt.gz second.txt.gz third.txt.gz zipped.tar.gz |
958 <div class="section" title="1.2.2.Development"> |
878 </pre> |
|
879 <p id="ch11Section_5_20"> |
|
880 Hence, as mentioned above, all original files are replaced by .gz extension. The above task can also be restated and made easy with help of some common regex expression: |
|
881 </p> |
|
882 <pre class="programlisting"> |
|
883 $ gzip *.txt |
|
884 </pre> |
|
885 <p id="ch11Section_5_21"> |
|
886 Similar to |
|
887 <span class="emphasis"> |
|
888 <em> |
|
889 tar |
|
890 </em> |
|
891 </span> |
|
892 command, one can also use |
|
893 <span class="emphasis"> |
|
894 <em> |
|
895 `-v` |
|
896 </em> |
|
897 </span> |
|
898 option here to see the output in |
|
899 <span class="emphasis"> |
|
900 <em> |
|
901 verbose |
|
902 </em> |
|
903 </span> |
|
904 form. For the previous example, if we enable this option the result would be something like this: |
|
905 </p> |
|
906 <pre class="programlisting"> |
|
907 $ gzip -v *.txt |
|
908 first.txt: 4.4% -- replaced with first.txt.gz |
|
909 fourth.txt: -7.1% -- replaced with fourth.txt.gz |
|
910 second.txt: -4.8% -- replaced with second.txt.gz |
|
911 third.txt: 3.8% -- replaced with third.txt.gz |
|
912 </pre> |
|
913 <p id="ch11Section_5_22"> |
|
914 For files of very small sizes and some other cases, one might end up with a zipped file whose size is greater then original file, but compression is always performed(so don't be disheartened in the above case, as files are larger :P). So unlike tar, here all files are zipped separately by default, to make them part of one single chunk one can use some |
|
915 <span class="emphasis"> |
|
916 <em> |
|
917 pipes |
|
918 </em> |
|
919 </span> |
|
920 and |
|
921 <span class="emphasis"> |
|
922 <em> |
|
923 redirections |
|
924 </em> |
|
925 </span> |
|
926 </p> |
|
927 <pre class="programlisting"> |
|
928 $ gzip -c *.txt > all.gz |
|
929 </pre> |
|
930 <p id="ch11Section_5_23"> |
|
931 Now in this case, all files would be zipped, concatenated and then the output would be written to a file all.gz leaving back all the original files. In the command above |
|
932 <span class="emphasis"> |
|
933 <em> |
|
934 `-c` |
|
935 </em> |
|
936 </span> |
|
937 option states to print the output to standard output(stdout) and following |
|
938 <span class="emphasis"> |
|
939 <em> |
|
940 `>` |
|
941 </em> |
|
942 </span> |
|
943 would redirect the output to file all.gz. So when we decompress this file, we will get a single file named 'all' with all the content of each files concatenated one after the another. |
|
944 </p> |
|
945 <p id="ch11Section_5_24"> |
|
946 For creating a zip archive of a complete directory, one has to use |
|
947 <span class="emphasis"> |
|
948 <em> |
|
949 `-r` |
|
950 </em> |
|
951 </span> |
|
952 options which means recursive, it makes gzip to traverse through all directory tree/structure. By default it will create zip files of each file inside the directory, that is even with the -r flag, gzip still compresses one file at a time : |
|
953 </p> |
|
954 <pre class="programlisting"> |
|
955 $ gzip -r fortune-files/ |
|
956 $ gzip -rv . |
|
957 ./first.txt: 4.4% -- replaced with ./first.txt.gz |
|
958 ./second.txt: -4.8% -- replaced with ./second.txt.gz |
|
959 ./third.txt: 3.8% -- replaced with ./third.txt.gz |
|
960 ./allfiles.tar: 96.6% -- replaced with ./allfiles.tar.gz |
|
961 ./fourth.txt: -7.1% -- replaced with ./fourth.txt.gz |
|
962 </pre> |
|
963 <p id="ch11Section_5_25"> |
|
964 Hence one always sees files like xxxxx.tar.gz, to create a zip of whole directory in a single file, first archive everything inside a folder and then use gzip on that. For zipping the files using tar itself, one has to use the option |
|
965 <span class="emphasis"> |
|
966 <em> |
|
967 `g` |
|
968 </em> |
|
969 </span> |
|
970 . |
|
971 </p> |
|
972 <pre class="programlisting"> |
|
973 $ tar -cvzf zipped.tar.gz *.txt |
|
974 first.txt |
|
975 fourth.txt |
|
976 second.txt |
|
977 third.txt |
|
978 </pre> |
|
979 <p id="ch11Section_5_26"> |
|
980 <span class="emphasis"> |
|
981 <em> |
|
982 Thats why gzip is designed as a complement to tar, not as a replacement. |
|
983 </em> |
|
984 </span> |
|
985 </p> |
|
986 <p id="ch11Section_5_27"> |
|
987 gzip command comes with a option |
|
988 <span class="emphasis"> |
|
989 <em> |
|
990 `-l` |
|
991 </em> |
|
992 </span> |
|
993 to view the compressed file contents: |
|
994 </p> |
|
995 <pre class="programlisting"> |
|
996 $ gzip -l zipped.tar.gz |
|
997 compressed uncompressed ratio uncompressed_name |
|
998 332 10240 97.0% zipped.tar |
|
999 </pre> |
|
1000 <p id="ch11Section_5_28"> |
|
1001 Other feature of gzip is option for mentioning the kind of compression one wants. There is a option of |
|
1002 <span class="emphasis"> |
|
1003 <em> |
|
1004 `-n` |
|
1005 </em> |
|
1006 </span> |
|
1007 where |
|
1008 <span class="emphasis"> |
|
1009 <em> |
|
1010 n varies from 0 to 9 |
|
1011 </em> |
|
1012 </span> |
|
1013 which regulate the speed/quality of compression. With |
|
1014 <span class="emphasis"> |
|
1015 <em> |
|
1016 `-1` |
|
1017 </em> |
|
1018 </span> |
|
1019 or |
|
1020 <span class="emphasis"> |
|
1021 <em> |
|
1022 `--fast` |
|
1023 </em> |
|
1024 </span> |
|
1025 option it means the fastest compression method (less compression) and |
|
1026 <span class="emphasis"> |
|
1027 <em> |
|
1028 `--best` |
|
1029 </em> |
|
1030 </span> |
|
1031 or |
|
1032 <span class="emphasis"> |
|
1033 <em> |
|
1034 `-9` |
|
1035 </em> |
|
1036 </span> |
|
1037 indicates the slowest compression method, default compression level is |
|
1038 <span class="emphasis"> |
|
1039 <em> |
|
1040 `-6` |
|
1041 </em> |
|
1042 </span> |
|
1043 . |
|
1044 </p> |
|
1045 <p id="ch11Section_5_29"> |
|
1046 To decompress a already compressed file there are two options, either use |
|
1047 <span class="emphasis"> |
|
1048 <em> |
|
1049 `gunzip` |
|
1050 </em> |
|
1051 </span> |
|
1052 command or use |
|
1053 <span class="emphasis"> |
|
1054 <em> |
|
1055 `-d` |
|
1056 </em> |
|
1057 </span> |
|
1058 option with gzip command: |
|
1059 </p> |
|
1060 <pre class="programlisting"> |
|
1061 $ gzip -dv *.gz |
|
1062 all.gz: -440.4% -- replaced with all |
|
1063 first.txt.gz: 4.4% -- replaced with first.txt |
|
1064 fourth.txt.gz: -7.1% -- replaced with fourth.txt |
|
1065 second.txt.gz: -4.8% -- replaced with second.txt |
|
1066 third.txt.gz: 3.8% -- replaced with third.txt |
|
1067 zipped.tar.gz: 97.0% -- replaced with zipped.tar |
|
1068 </pre> |
|
1069 <p id="ch11Section_5_2a"> |
|
1070 or: |
|
1071 </p> |
|
1072 <pre class="programlisting"> |
|
1073 $ gunzip -v *.gz |
|
1074 </pre> |
|
1075 <p id="ch11Section_5_2b"> |
|
1076 Both of those commands will give the same result. So here one can notice the content of file "all" which we created earlier, it will have content of all the rest of four files concatenated one after another, but "zipped.tar.gz" is zip of tar of all files, will effectively have zip of archives of all files separately, and hence the usage and importance of |
|
1077 <span class="emphasis"> |
|
1078 <em> |
|
1079 tar |
|
1080 </em> |
|
1081 </span> |
|
1082 . |
|
1083 </p> |
|
1084 </div> |
|
1085 </div> |
|
1086 <div class="section" title="3.2.Further Reading for this section:"> |
|
1087 <div class="titlepage"> |
|
1088 <div> |
|
1089 <div> |
|
1090 <h3 class="title"> |
|
1091 <a name="id2856622"> |
|
1092 </a> |
|
1093 3.2.Further Reading for this section: |
|
1094 </h3> |
|
1095 </div> |
|
1096 </div> |
|
1097 </div> |
|
1098 <div class="itemizedlist"> |
|
1099 <ul class="itemizedlist" type="*"> |
|
1100 <li class="listitem" style="list-style-type: *"> |
|
1101 <p id="ch11Section_5_2c"> |
|
1102 </p> |
|
1103 <div class="reference"> |
|
1104 <div class="titlepage"> |
|
1105 <hr /> |
|
1106 </div> |
|
1107 http://linuxreviews.org/beginner/ |
|
1108 </div> |
|
1109 </li> |
|
1110 <li class="listitem" style="list-style-type: *"> |
|
1111 <p id="ch11Section_5_2d"> |
|
1112 </p> |
|
1113 <div class="reference"> |
|
1114 <div class="titlepage"> |
|
1115 <hr /> |
|
1116 </div> |
|
1117 http://lowfatlinux.com/linux-gzip-gunzip.html |
|
1118 </div> |
|
1119 </li> |
|
1120 <li class="listitem" style="list-style-type: *"> |
|
1121 <p id="ch11Section_5_2e"> |
|
1122 </p> |
|
1123 <div class="reference"> |
|
1124 <div class="titlepage"> |
|
1125 <hr /> |
|
1126 </div> |
|
1127 http://www.gnu.org/software/gzip/manual/gzip.html |
|
1128 </div> |
|
1129 </li> |
|
1130 <li class="listitem" style="list-style-type: *"> |
|
1131 <p id="ch11Section_5_2f"> |
|
1132 </p> |
|
1133 <div class="reference"> |
|
1134 <div class="titlepage"> |
|
1135 <hr /> |
|
1136 </div> |
|
1137 http://en.wikipedia.org/wiki/ZIP_(file_format |
|
1138 </div> |
|
1139 <p> |
|
1140 ) |
|
1141 </p> |
|
1142 </li> |
|
1143 </ul> |
|
1144 </div> |
|
1145 </div> |
|
1146 </div> |
|
1147 <div class="section" title="4.File Comparisons:"> |
|
1148 <div class="titlepage"> |
|
1149 <div> |
|
1150 <div> |
|
1151 <h2 class="title" style="clear: both"> |
|
1152 <a name="id2856676"> |
|
1153 </a> |
|
1154 4.File Comparisons: |
|
1155 </h2> |
|
1156 </div> |
|
1157 </div> |
|
1158 </div> |
|
1159 <p id="ch11Section_5_30"> |
|
1160 Linux based distributions also have some utilities for checking the content of files, comparing them very quickly to other files. These operations can be looking for differences/similarities. Some of the commands which prove handy are: |
|
1161 </p> |
|
1162 <div class="section" title="4.1.cmp:"> |
|
1163 <div class="titlepage"> |
|
1164 <div> |
|
1165 <div> |
|
1166 <h3 class="title"> |
|
1167 <a name="id2856693"> |
|
1168 </a> |
|
1169 4.1.cmp: |
|
1170 </h3> |
|
1171 </div> |
|
1172 </div> |
|
1173 </div> |
|
1174 <p id="ch11Section_5_31"> |
|
1175 If one wants to compare two files whether they are same or not, one can use this handy tool. Let us consider some situation, we run find/locate command to locate some file, and it turns out that we have a file with same name in different location, and in case we want to run a quick check on there content, cmp is the right tool. For my system I perform these tasks to illustrate the use of this command: |
|
1176 </p> |
|
1177 <pre class="programlisting"> |
|
1178 $ find . -name quick.c |
|
1179 ./Desktop/programs/quick.c |
|
1180 ./c-folder/quick.c |
|
1181 $ cmp Desktop/programs/quick.c c-folder/quick.c |
|
1182 $ |
|
1183 </pre> |
|
1184 <p id="ch11Section_5_32"> |
|
1185 For me it returns nothing, hence that means both the files are exact copy of each other, by default, cmp is silent if the files are the same. Make some changes in one of the file and rerun the command. For me it works like this: |
|
1186 </p> |
|
1187 <pre class="programlisting"> |
|
1188 $ cmp Desktop/programs/quick.c c-folder/quick.c |
|
1189 Desktop/programs/quick.c c-folder/quick.c differ: byte 339, line 24 |
|
1190 </pre> |
|
1191 <p id="ch11Section_5_33"> |
|
1192 That is, if files differ, the byte and line number at which the first difference occurred is reported. |
|
1193 </p> |
|
1194 </div> |
|
1195 <div class="section" title="4.2.diff:"> |
|
1196 <div class="titlepage"> |
|
1197 <div> |
|
1198 <div> |
|
1199 <h3 class="title"> |
|
1200 <a name="id2856735"> |
|
1201 </a> |
|
1202 4.2.diff: |
|
1203 </h3> |
|
1204 </div> |
|
1205 </div> |
|
1206 </div> |
|
1207 <p id="ch11Section_5_34"> |
|
1208 Now there are situations when one wants to exactly know the differences among two files, for them, GNU diff can show whether files are different without detailing the differences. For simple and basic usage of this programs, consider following example: |
|
1209 </p> |
|
1210 <pre class="programlisting"> |
|
1211 $ echo -e "quick nbrown nfox njumped nover nthe nlazy ndog" > allcharacters.txt |
|
1212 $ echo -e "quick nbrown nfox njmuped nover nteh nlzay ndog" > problem.txt |
|
1213 $ diff problem.txt allcharacters.txt |
|
1214 4c4 |
|
1215 < jmuped |
|
1216 --- |
|
1217 > jumped |
|
1218 6,7c6,7 |
|
1219 < teh |
|
1220 < lzay |
|
1221 --- |
|
1222 > the |
|
1223 > lazy |
|
1224 </pre> |
|
1225 <p id="ch11Section_5_35"> |
|
1226 Looking at results above mentioned it is very trivial to deduce that, diff if used on two separate text files will result in line by line results for all the lines which are different. So most common use case scenario can be, got some files in various location of system with same name and size, just run diff through them and remove all the redundant files. Other similar command which one can find more effective for this can be |
|
1227 <span class="emphasis"> |
|
1228 <em> |
|
1229 sdiff |
|
1230 </em> |
|
1231 </span> |
|
1232 , for the same files using sdiff will result in: |
|
1233 </p> |
|
1234 <pre class="programlisting"> |
|
1235 $ sdiff problem.txt allcharacters.txt |
|
1236 quick quick |
|
1237 brown brown |
|
1238 fox fox |
|
1239 jmuped | jumped |
|
1240 over over |
|
1241 teh | the |
|
1242 lzay | lazy |
|
1243 dog dog |
|
1244 </pre> |
|
1245 <p id="ch11Section_5_36"> |
|
1246 Some exercise for a change: |
|
1247 </p> |
|
1248 <div class="itemizedlist"> |
|
1249 <ul class="itemizedlist" type="*"> |
|
1250 <li class="listitem" style="list-style-type: *"> |
|
1251 <p id="ch11Section_5_37"> |
|
1252 Try using diff for any binary file, does it work? |
|
1253 </p> |
|
1254 </li> |
|
1255 <li class="listitem" style="list-style-type: *"> |
|
1256 <p id="ch11Section_5_38"> |
|
1257 What are other equivalent for diff command based on needs/requirements? |
|
1258 </p> |
|
1259 </li> |
|
1260 <li class="listitem" style="list-style-type: *"> |
|
1261 <p id="ch11Section_5_39"> |
|
1262 Can we use diff to compare two directories? If yes how? |
|
1263 </p> |
|
1264 </li> |
|
1265 </ul> |
|
1266 </div> |
|
1267 </div> |
|
1268 <div class="section" title="4.3.comm:"> |
|
1269 <div class="titlepage"> |
|
1270 <div> |
|
1271 <div> |
|
1272 <h3 class="title"> |
|
1273 <a name="id2856817"> |
|
1274 </a> |
|
1275 4.3.comm: |
|
1276 </h3> |
|
1277 </div> |
|
1278 </div> |
|
1279 </div> |
|
1280 <p id="ch11Section_5_3a"> |
|
1281 This is one more command which proves handy at times, the short and sweet man page states "compare two sorted files line by line". Or this it compares sorted files and selects or rejects lines common to two files. For ex: |
|
1282 </p> |
|
1283 <pre class="programlisting"> |
|
1284 $ sort allcharacters.txt>sortedcharac.txt; sort problem.txt>sortedprob.txt |
|
1285 $ comm sortedcharac.txt sortedprob.txt |
|
1286 brown |
|
1287 dog |
|
1288 fox |
|
1289 jmuped |
|
1290 jumped |
|
1291 lazy |
|
1292 lzay |
|
1293 over |
|
1294 quick |
|
1295 teh |
|
1296 the |
|
1297 </pre> |
|
1298 </div> |
|
1299 </div> |
|
1300 <div class="section" title="5.Environment Variables:"> |
|
1301 <div class="titlepage"> |
|
1302 <div> |
|
1303 <div> |
|
1304 <h2 class="title" style="clear: both"> |
|
1305 <a name="id2856847"> |
|
1306 </a> |
|
1307 5.Environment Variables: |
|
1308 </h2> |
|
1309 </div> |
|
1310 </div> |
|
1311 </div> |
|
1312 <p id="ch11Section_5_3b"> |
|
1313 These variables like HOME, OSTYPE,Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.By convention, environment variables have UPPER CASE and shell variables have lower case names. |
|
1314 </p> |
|
1315 <p id="ch11Section_5_3c"> |
|
1316 Some of examples of Environment variables are(result may vary!): |
|
1317 </p> |
|
1318 <pre class="programlisting"> |
|
1319 $ echo $OSTYPE |
|
1320 linux-gnu |
|
1321 $ echo $HOME |
|
1322 /home/baali |
|
1323 </pre> |
|
1324 <p id="ch11Section_5_3d"> |
|
1325 To see all the variables and there values use any of following commands: |
|
1326 </p> |
|
1327 <pre class="programlisting"> |
|
1328 $ printenv | less |
|
1329 $ env |
|
1330 </pre> |
|
1331 <p id="ch11Section_5_3e"> |
|
1332 The most commonly used environment variable is "PATH", it defines a list of directories to search through when looking for a command to execute. If you decide to put your own programs in a bin directory under your home directory, you'll have to modify the path to include that directory, or the system will never find your programs (unless you happen to be in that directory when you enter the command). Here's how to change your PATH variable so it includes your personal bin directory: |
|
1333 </p> |
|
1334 <pre class="programlisting"> |
|
1335 $ set PATH=$PATH:$HOME/bin |
|
1336 </pre> |
|
1337 <p id="ch11Section_5_3f"> |
|
1338 See the difference in value of PATH variable before and after modifying it. One can also create its own variable to make things easier: |
|
1339 </p> |
|
1340 <pre class="programlisting"> |
|
1341 $ set repo = $HOME/Desktop/random/code |
|
1342 $ cd $repo |
|
1343 </pre> |
|
1344 <p id="ch11Section_5_40"> |
|
1345 <span class="emphasis"> |
|
1346 <em> |
|
1347 set |
|
1348 </em> |
|
1349 </span> |
|
1350 command is used to define a variable for the current shell. Try opening a new shell and use the above mentioned command, it wont work as expected. The other child process wont be able to see these variables unless we |
|
1351 <span class="emphasis"> |
|
1352 <em> |
|
1353 export |
|
1354 </em> |
|
1355 </span> |
|
1356 them. Repeat the above mentioned activity with |
|
1357 <span class="emphasis"> |
|
1358 <em> |
|
1359 export |
|
1360 </em> |
|
1361 </span> |
|
1362 command. Now with all new shells, |
|
1363 <span class="emphasis"> |
|
1364 <em> |
|
1365 $repo |
|
1366 </em> |
|
1367 </span> |
|
1368 will work. |
|
1369 </p> |
|
1370 <p id="ch11Section_5_41"> |
|
1371 Again these changes are limited to current session. To make them permanent or get loaded each time you log in, just add those lines to |
|
1372 <span class="emphasis"> |
|
1373 <em> |
|
1374 .bashrc |
|
1375 </em> |
|
1376 </span> |
|
1377 file. |
|
1378 </p> |
|
1379 <div class="section" title="5.1.Further Reading:"> |
|
1380 <div class="titlepage"> |
|
1381 <div> |
|
1382 <div> |
|
1383 <h3 class="title"> |
|
1384 <a name="id2856946"> |
|
1385 </a> |
|
1386 5.1.Further Reading: |
|
1387 </h3> |
|
1388 </div> |
|
1389 </div> |
|
1390 </div> |
|
1391 <div class="itemizedlist"> |
|
1392 <ul class="itemizedlist" type="*"> |
|
1393 <li class="listitem" style="list-style-type: *"> |
|
1394 <p id="ch11Section_5_42"> |
|
1395 </p> |
|
1396 <div class="reference"> |
|
1397 <div class="titlepage"> |
|
1398 <hr /> |
|
1399 </div> |
|
1400 http://lowfatlinux.com/linux-environment-variables.html |
|
1401 </div> |
|
1402 </li> |
|
1403 <li class="listitem" style="list-style-type: *"> |
|
1404 <p id="ch11Section_5_43"> |
|
1405 </p> |
|
1406 <div class="reference"> |
|
1407 <div class="titlepage"> |
|
1408 <hr /> |
|
1409 </div> |
|
1410 http://www.codecoffee.com/tipsforlinux/articles/030.html |
|
1411 </div> |
|
1412 </li> |
|
1413 <li class="listitem" style="list-style-type: *"> |
|
1414 <p id="ch11Section_5_44"> |
|
1415 </p> |
|
1416 <div class="reference"> |
|
1417 <div class="titlepage"> |
|
1418 <hr /> |
|
1419 </div> |
|
1420 http://www.ee.surrey.ac.uk/Teaching/Unix/unix8.html |
|
1421 </div> |
|
1422 </li> |
|
1423 <li class="listitem" style="list-style-type: *"> |
|
1424 <p id="ch11Section_5_45"> |
|
1425 </p> |
|
1426 <div class="reference"> |
|
1427 <div class="titlepage"> |
|
1428 <hr /> |
|
1429 </div> |
|
1430 http://en.wikipedia.org/wiki/Environment_variable |
|
1431 </div> |
|
1432 </li> |
|
1433 </ul> |
|
1434 </div> |
|
1435 </div> |
|
1436 </div> |
|
1437 <div class="section" title="6.Shell Scripting:"> |
|
1438 <div class="titlepage"> |
|
1439 <div> |
|
1440 <div> |
|
1441 <h2 class="title" style="clear: both"> |
|
1442 <a name="id2857001"> |
|
1443 </a> |
|
1444 6.Shell Scripting: |
|
1445 </h2> |
|
1446 </div> |
|
1447 </div> |
|
1448 </div> |
|
1449 <div class="section" title="6.1.Basics:"> |
|
1450 <div class="titlepage"> |
|
1451 <div> |
|
1452 <div> |
|
1453 <h3 class="title"> |
|
1454 <a name="id2857009"> |
|
1455 </a> |
|
1456 6.1.Basics: |
|
1457 </h3> |
|
1458 </div> |
|
1459 </div> |
|
1460 </div> |
|
1461 <p id="ch11Section_5_46"> |
|
1462 Shell program or shell script,a sequence of commands to a text file and tell the shell to execute the text file instead of entering the commands. The first |
|
1463 <span class="emphasis"> |
|
1464 <em> |
|
1465 "Hello World" |
|
1466 </em> |
|
1467 </span> |
|
1468 sample for shell scripting is as easy as it sounds: |
|
1469 </p> |
|
1470 <pre class="programlisting"> |
|
1471 $ echo '#!/bin/sh' > my-script.sh |
|
1472 $ clear >> my-script.sh |
|
1473 $ echo 'echo Hello World' >> my-script.sh |
|
1474 $ chmod 755 my-script.sh |
|
1475 $ ./my-script.sh |
|
1476 Hello World |
|
1477 </pre> |
|
1478 <p id="ch11Section_5_47"> |
|
1479 The #! syntax(also known as shebang) is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The chmod is required to make the script executable. This script will just execute two commands, |
|
1480 <span class="emphasis"> |
|
1481 <em> |
|
1482 clear |
|
1483 </em> |
|
1484 </span> |
|
1485 and |
|
1486 <span class="emphasis"> |
|
1487 <em> |
|
1488 echo |
|
1489 </em> |
|
1490 </span> |
|
1491 one after another. One can also do the same task using a one liner command |
|
1492 <span class="emphasis"> |
|
1493 <em> |
|
1494 clear; echo 'Hello World'; |
|
1495 </em> |
|
1496 </span> |
|
1497 but as number of lines grows using a script file is helpful. |
|
1498 </p> |
|
1499 <p id="ch11Section_5_48"> |
|
1500 So lets create a script which gives us all the filenames for given initial alphabet or string in a directory. Let the name of script be |
|
1501 <span class="emphasis"> |
|
1502 <em> |
|
1503 initial.sh |
|
1504 </em> |
|
1505 </span> |
|
1506 , open it with text editor, and write: |
|
1507 </p> |
|
1508 <pre class="programlisting"> |
|
1509 #!/bin/sh |
|
1510 ls > temp |
|
1511 grep ^$1 < temp |
|
1512 rm temp |
|
1513 $ chmod a+x initial.sh |
|
1514 $ ./initial.sh s |
|
1515 </pre> |
|
1516 <p id="ch11Section_5_49"> |
|
1517 The $1 in the script is pertaining to command line argument. All arguments passed via command line are accessed via |
|
1518 <span class="emphasis"> |
|
1519 <em> |
|
1520 $# |
|
1521 </em> |
|
1522 </span> |
|
1523 with name of script being first member, that is $0. Now lets write a script for finding a file, and then checking when was it last modified: |
|
1524 </p> |
|
1525 <pre class="programlisting"> |
|
1526 #!/bin/sh |
|
1527 name=`find . -name $1 -print` |
|
1528 echo $name |
|
1529 last_modified=`stat -c %y $name| cut -f 1 -d " "` |
|
1530 echo "Last modified: $last_modified" |
|
1531 $ ./search.sh fname |
|
1532 </pre> |
|
1533 <p id="ch11Section_5_4a"> |
|
1534 Try giving some file you want to search in place of fname. Please note in second line |
|
1535 <span class="emphasis"> |
|
1536 <em> |
|
1537 ` |
|
1538 </em> |
|
1539 </span> |
|
1540 its a back-quote(other key mapped with tilda), it is specifically used to get the output of one command into a variable. In this particular case name is a User defined variables (UDV) which stores the value. We access value stored in any variable using |
|
1541 <span class="emphasis"> |
|
1542 <em> |
|
1543 $ |
|
1544 </em> |
|
1545 </span> |
|
1546 symbol before name of variable. |
|
1547 </p> |
|
1548 <p id="ch11Section_5_4b"> |
|
1549 naming conventions for variables?? do we need them?? |
|
1550 </p> |
|
1551 </div> |
|
1552 <div class="section" title="6.2.Shell Arithmetic:"> |
|
1553 <div class="titlepage"> |
|
1554 <div> |
|
1555 <div> |
|
1556 <h3 class="title"> |
|
1557 <a name="id2855367"> |
|
1558 </a> |
|
1559 6.2.Shell Arithmetic: |
|
1560 </h3> |
|
1561 </div> |
|
1562 </div> |
|
1563 </div> |
|
1564 <p id="ch11Section_5_4c"> |
|
1565 Shell also provides support for basic arithmetic operations. The syntax is: |
|
1566 </p> |
|
1567 <pre class="programlisting"> |
|
1568 $ expr op1 math-operator op2 |
|
1569 </pre> |
|
1570 <p id="ch11Section_5_4d"> |
|
1571 Some of example which can be tried handily: |
|
1572 </p> |
|
1573 <pre class="programlisting"> |
|
1574 $ expr -3 + 5 |
|
1575 2 |
|
1576 $ expr 10 % 3 |
|
1577 1 |
|
1578 </pre> |
|
1579 <p id="ch11Section_5_4e"> |
|
1580 These spaces in between operator and operands is important, without them shell interpreter will raise the syntax error. |
|
1581 </p> |
|
1582 <pre class="programlisting"> |
|
1583 $ expr 2*3 |
|
1584 expr: syntax error |
|
1585 </pre> |
|
1586 <p id="ch11Section_5_4f"> |
|
1587 One can use back-quotes(`) also to get value of expr. |
|
1588 </p> |
|
1589 <pre class="programlisting"> |
|
1590 $ echo `expr 6 + 3` |
|
1591 9 |
|
1592 $ result=`expr 6 + 3` |
|
1593 $ echo $result |
|
1594 9 |
|
1595 </pre> |
|
1596 <p id="ch11Section_5_50"> |
|
1597 Shell uses three kinds of quotes. Double quotes("), anything enclosed among them except from variable trailing after $, and characters after would be printed as it is. Single quotes('), anything enclosed within them is just same, no formulation/interpretation. Back quotes(`), anything inclosed is considered as command, or is executed. |
|
1598 </p> |
|
1599 <pre class="programlisting"> |
|
1600 $ echo "Today is date" |
|
1601 Today is date |
|
1602 $ echo "Today is `date`" |
|
1603 Today is Wed Sep 16 17:32:22 IST 2009 |
|
1604 $ echo 'Today is `date`' |
|
1605 Today is `date` |
|
1606 $ echo "Today is n `date`" |
|
1607 Today is n Wed Sep 16 17:40:13 IST 2009 |
|
1608 $ echo -e "Today is n `date`" |
|
1609 Today is |
|
1610 Wed Sep 16 17:41:13 IST 2009 |
|
1611 </pre> |
|
1612 </div> |
|
1613 <div class="section" title="6.3.if else construct:"> |
|
1614 <div class="titlepage"> |
|
1615 <div> |
|
1616 <div> |
|
1617 <h3 class="title"> |
|
1618 <a name="id2857386"> |
|
1619 </a> |
|
1620 6.3.if else construct: |
|
1621 </h3> |
|
1622 </div> |
|
1623 </div> |
|
1624 </div> |
|
1625 <p id="ch11Section_5_51"> |
|
1626 One can have simple |
|
1627 <span class="emphasis"> |
|
1628 <em> |
|
1629 if else if |
|
1630 </em> |
|
1631 </span> |
|
1632 constructs in shell scripts to check conditions. Lets take simple example of writing a script which returns back whether the argument passed is positive or not: |
|
1633 </p> |
|
1634 <pre class="programlisting"> |
|
1635 #!/bin/sh |
|
1636 if test $1 -gt 0 |
|
1637 then |
|
1638 echo "number is positive" |
|
1639 else |
|
1640 echo "number is negative" |
|
1641 fi |
|
1642 $ ./sign.sh -11 |
|
1643 number is negative |
|
1644 </pre> |
|
1645 <p id="ch11Section_5_52"> |
|
1646 This script will compare the first value passed as argument with 0 |
|
1647 <span class="emphasis"> |
|
1648 <em> |
|
1649 if test var -gt val |
|
1650 </em> |
|
1651 </span> |
|
1652 , var being $1 and val being 0, gt meaning greater then. Now this program has some flaw, it will give same result for following input: (-11) and (-1, 5), as we are checking just $1 which is first argument and hence the result. For handling such situation we can include |
|
1653 <span class="emphasis"> |
|
1654 <em> |
|
1655 if-else |
|
1656 </em> |
|
1657 </span> |
|
1658 clause which will warn user of correct usage of script. |
|
1659 </p> |
|
1660 <pre class="programlisting"> |
|
1661 #this is the case when no argument is passed |
|
1662 if [ $# -eq 0 ] |
|
1663 then |
|
1664 echo "$0 : You must give/supply one integers" |
|
1665 exit 1 |
|
1666 else |
|
1667 if [ $# -gt 1 ] |
|
1668 then |
|
1669 echo "$0 : You must give one integer" |
|
1670 exit 1 |
|
1671 fi |
|
1672 fi |
|
1673 </pre> |
|
1674 <p id="ch11Section_5_53"> |
|
1675 One important thing to not in shell script is spacing, with many comparison and evaluation operation a wrongly placed space will spoil all the fun. So in previous example the expression |
|
1676 <span class="emphasis"> |
|
1677 <em> |
|
1678 [ $# -eq 0 ] |
|
1679 </em> |
|
1680 </span> |
|
1681 will work properly, but if we remove those leading or trailing spaces like |
|
1682 <span class="emphasis"> |
|
1683 <em> |
|
1684 [ $# -eq 0] |
|
1685 </em> |
|
1686 </span> |
|
1687 , it wont work as expected, or rather throw a warning. Both |
|
1688 <span class="emphasis"> |
|
1689 <em> |
|
1690 test |
|
1691 </em> |
|
1692 </span> |
|
1693 and |
|
1694 <span class="emphasis"> |
|
1695 <em> |
|
1696 [] |
|
1697 </em> |
|
1698 </span> |
|
1699 do the same task of testing a expression and returning true or false. |
|
1700 </p> |
|
1701 <p id="ch11Section_5_54"> |
|
1702 Lets create something interesting using these if-else clause. Now we will create a script which will greet the user when he opens the shell. We will create the script, change the permission to make it executable and append the |
|
1703 <span class="emphasis"> |
|
1704 <em> |
|
1705 .bashrc |
|
1706 </em> |
|
1707 </span> |
|
1708 file with |
|
1709 <span class="emphasis"> |
|
1710 <em> |
|
1711 ./greet.sh |
|
1712 </em> |
|
1713 </span> |
|
1714 line and we are done. The script is: |
|
1715 </p> |
|
1716 <pre class="programlisting"> |
|
1717 #!/bin/sh |
|
1718 #Script to greet the user according to time of day |
|
1719 temph=`date | cut -c12-13` |
|
1720 dat=`date +"%A %d in %B of %Y (%r)"` |
|
1721 if [ $temph -lt 12 ] |
|
1722 then |
|
1723 mess="Good Morning $LOGNAME, Have a nice day!" |
|
1724 fi |
|
1725 |
|
1726 if [ $temph -gt 12 -a $temph -le 16 ] |
|
1727 then |
|
1728 mess="Good Afternoon $LOGNAME" |
|
1729 fi |
|
1730 |
|
1731 if [ $temph -gt 16 -a $temph -le 18 ] |
|
1732 then |
|
1733 mess="Good Evening $LOGNAME" |
|
1734 fi |
|
1735 echo -e "$mess nThis is $dat" |
|
1736 </pre> |
|
1737 <p id="ch11Section_5_55"> |
|
1738 For me when I open the shell the output is something like: |
|
1739 </p> |
|
1740 <pre class="programlisting"> |
|
1741 Good Morning baali, Have a nice day! |
|
1742 This is Wednesday 16 in September of 2009 (11:54:47 AM IST) |
|
1743 </pre> |
|
1744 </div> |
|
1745 <div class="section" title="6.4.Loops"> |
|
1746 <div class="titlepage"> |
|
1747 <div> |
|
1748 <div> |
|
1749 <h3 class="title"> |
|
1750 <a name="id2857492"> |
|
1751 </a> |
|
1752 6.4.Loops |
|
1753 </h3> |
|
1754 </div> |
|
1755 </div> |
|
1756 </div> |
|
1757 <p id="ch11Section_5_56"> |
|
1758 Bash has three different commands for looping -- |
|
1759 <code class="literal"> |
|
1760 for |
|
1761 </code> |
|
1762 , |
|
1763 <code class="literal"> |
|
1764 while |
|
1765 </code> |
|
1766 and |
|
1767 <code class="literal"> |
|
1768 until |
|
1769 </code> |
|
1770 . |
|
1771 </p> |
|
1772 <div class="section" title="6.4.1.for loop"> |
959 <div class="titlepage"> |
1773 <div class="titlepage"> |
960 <div> |
1774 <div> |
961 <div> |
1775 <div> |
962 <h4 class="title"> |
1776 <h4 class="title"> |
963 <a name="id2945205"> |
1777 <a name="id2857514"> |
964 </a> |
1778 </a> |
965 1.2.2.Development |
1779 6.4.1. |
|
1780 <code class="literal"> |
|
1781 for |
|
1782 </code> |
|
1783 loop |
966 </h4> |
1784 </h4> |
967 </div> |
1785 </div> |
968 </div> |
1786 </div> |
969 </div> |
1787 </div> |
970 <p id="ch11Section_5_c"> |
1788 <p id="ch11Section_5_57"> |
971 The primary difference between Linux and many other popular contemporary operating systems is that the Linux kernel and other components are free and open source software. Linux is not the only such operating system, although it is by far the most widely used. Some free and open source software licenses are based on the principle of "copyleft", a kind of reciprocity: any work derived from a copyleft piece of software must also be copyleft itself. The most common free software license, the GNU GPL, is a form of copyleft, and is used for the Linux kernel and many of the components from the GNU project. |
1789 Suppose we have a set of files, that have names beginning with numbers followed by their names - |
972 </p> |
1790 <code class="literal"> |
973 <p id="ch11Section_5_d"> |
1791 08 - Society.mp3 |
974 Linux based distributions are intended by developers for interoperability with other operating systems and established computing standards. Linux systems adhere to POSIX, SUS, ISO and ANSI standards where possible, although to date only one Linux distribution has been POSIX.1 certified, Linux-FT.Free software projects, although developed in a collaborative fashion, are often produced independently of each other. The fact that the software licenses explicitly permit redistribution, however, provides a basis for larger scale projects that collect the software produced by stand-alone projects and make it available all at once in the form of a Linux distribution. |
1792 </code> |
975 </p> |
1793 . We would like to rename these files to remove the numbering. How would we go about doing that? It is clear from the problem statement that we could use a |
976 <p id="ch11Section_5_e"> |
1794 <code class="literal"> |
977 A Linux distribution, commonly called a "distro", is a project that manages a remote collection of system software and application software packages available for download and installation through a network connection. This allows the user to adapt the operating system to his/her specific needs. Distributions are maintained by individuals, loose-knit teams, volunteer organizations, and commercial entities. A distribution can be installed using a CD that contains distribution-specific software for initial system installation and configuration. A package manager such as Synaptic or YAST allows later package upgrades and installations. A distribution is responsible for the default configuration of the installed Linux kernel, general system security, and more generally integration of the different software packages into a coherent whole. |
1795 for |
978 </p> |
1796 </code> |
979 </div> |
1797 loop, to loop through the list of files and rename each of the files. |
980 <div class="section" title="1.2.3.Community"> |
1798 </p> |
|
1799 <p id="ch11Section_5_58"> |
|
1800 Let's first look at a simple |
|
1801 <code class="literal"> |
|
1802 for |
|
1803 </code> |
|
1804 loop, to understand how it works. |
|
1805 </p> |
|
1806 <pre class="programlisting"> |
|
1807 for animal in rat cat dog man |
|
1808 do |
|
1809 echo $animal |
|
1810 done |
|
1811 </pre> |
|
1812 <p id="ch11Section_5_59"> |
|
1813 We just wrote a list of animals, each animal's name separated by a space and printed each name on a separate line. The variable |
|
1814 <code class="literal"> |
|
1815 animal |
|
1816 </code> |
|
1817 is a dummy variable and has no significance. You could use something as lame as |
|
1818 <code class="literal"> |
|
1819 i |
|
1820 </code> |
|
1821 in place of |
|
1822 <code class="literal"> |
|
1823 animal |
|
1824 </code> |
|
1825 . |
|
1826 </p> |
|
1827 <p id="ch11Section_5_5a"> |
|
1828 Now, we use a simple |
|
1829 <code class="literal"> |
|
1830 for |
|
1831 </code> |
|
1832 loop to list the files that we are interested in. |
|
1833 </p> |
|
1834 <pre class="programlisting"> |
|
1835 ls *.mp3 > list |
|
1836 for i in `cat list` |
|
1837 do |
|
1838 echo "$i" |
|
1839 done |
|
1840 </pre> |
|
1841 <p id="ch11Section_5_5b"> |
|
1842 If your filenames contain spaces, |
|
1843 <code class="literal"> |
|
1844 for |
|
1845 </code> |
|
1846 assumes each space separated word to be a single item in the list and prints it in a separate line. We could change the script slightly to overcome this problem. |
|
1847 </p> |
|
1848 <pre class="programlisting"> |
|
1849 for i in *.mp3 |
|
1850 do |
|
1851 echo "$i" |
|
1852 done |
|
1853 </pre> |
|
1854 <p id="ch11Section_5_5c"> |
|
1855 Now, we have each file printed on a separate line. Depending on the files that we have we could use grep to get the relevant portion of the filenames and rename the files. |
|
1856 </p> |
|
1857 <pre class="programlisting"> |
|
1858 for i in *.mp3 |
|
1859 do |
|
1860 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
|
1861 echo "$i -> $j" |
|
1862 done |
|
1863 </pre> |
|
1864 <p id="ch11Section_5_5d"> |
|
1865 Now we just replace the echo command with a |
|
1866 <code class="literal"> |
|
1867 mv |
|
1868 </code> |
|
1869 or a |
|
1870 <code class="literal"> |
|
1871 cp |
|
1872 </code> |
|
1873 command. |
|
1874 </p> |
|
1875 <pre class="programlisting"> |
|
1876 for i in *.mp3 |
|
1877 do |
|
1878 j=$(echo "$i"|grep -o "[A-Za-z'&. ]*.mp3") |
|
1879 cp "$i" "$j" |
|
1880 done |
|
1881 </pre> |
|
1882 <p id="ch11Section_5_5e"> |
|
1883 As an exercise, you could try sorting the files in reverse alphabetical order and then prefix numbers to each of the filenames. |
|
1884 </p> |
|
1885 </div> |
|
1886 <div class="section" title="6.4.2.while"> |
981 <div class="titlepage"> |
1887 <div class="titlepage"> |
982 <div> |
1888 <div> |
983 <div> |
1889 <div> |
984 <h4 class="title"> |
1890 <h4 class="title"> |
985 <a name="id2945262"> |
1891 <a name="id2857633"> |
986 </a> |
1892 </a> |
987 1.2.3.Community |
1893 6.4.2. |
|
1894 <code class="literal"> |
|
1895 while |
|
1896 </code> |
988 </h4> |
1897 </h4> |
989 </div> |
1898 </div> |
990 </div> |
1899 </div> |
991 </div> |
1900 </div> |
992 <p id="ch11Section_5_f"> |
1901 <p id="ch11Section_5_5f"> |
993 A distribution is largely driven by its developer and user communities. Some vendors develop and fund their distributions on a volunteer basis. Examples include Debian and the Debian-based, Ubuntu. Others maintain a community version of their commercial distributions, as Red Hat does with Fedora. |
1902 The |
994 </p> |
1903 <code class="literal"> |
995 <p id="ch11Section_5_10"> |
1904 while |
996 In many cities and regions, local associations known as Linux Users Groups (LUGs) seek to promote their preferred distribution and by extension free software. They hold meetings and provide free demonstrations, training, technical support, and operating system installation to new users. Many Internet communities also provide support to Linux users and developers. Most distributions and free software / open source projects have IRC (Internet Relay Chat) chatrooms or newsgroups. Online forums are another means for support. Linux distributions host mailing lists; commonly there will be a specific topic such as usage or development for a given list. All these can be found simply by running an appropriate search on Google. |
1905 </code> |
997 </p> |
1906 command allows us to continuously execute a block of commands until the command that is controlling the loop is executing successfully. |
998 <p id="ch11Section_5_11"> |
1907 </p> |
999 Although Linux distributions are generally available without charge, several large corporations sell, support, and contribute to the development of the components of the system and of free software. These include Dell, IBM, HP, Oracle, Sun Microsystems, Novell, Nokia. A number of corporations, notably Red Hat, have built their entire business around Linux distributions. |
1908 <p id="ch11Section_5_60"> |
1000 </p> |
1909 Let's start with the lamest example of a while loop. |
1001 </div> |
1910 </p> |
1002 <div class="section" title="1.2.4.Can I make a profit out of running a business involving Linux?"> |
1911 <pre class="programlisting"> |
|
1912 while true |
|
1913 do |
|
1914 echo "True" |
|
1915 done |
|
1916 </pre> |
|
1917 <p id="ch11Section_5_61"> |
|
1918 This, as you can see, is an infinite loop that prints the |
|
1919 <code class="literal"> |
|
1920 True |
|
1921 </code> |
|
1922 . |
|
1923 </p> |
|
1924 <p id="ch11Section_5_62"> |
|
1925 Say we wish to write a simple program that takes user input and prints it back, until the input is |
|
1926 <code class="literal"> |
|
1927 quit |
|
1928 </code> |
|
1929 , which quits the program. |
|
1930 </p> |
|
1931 <pre class="programlisting"> |
|
1932 while [ "$variable" != "quit" ] |
|
1933 do |
|
1934 read variable |
|
1935 echo "Input - $variable" |
|
1936 done |
|
1937 exit 0 |
|
1938 </pre> |
|
1939 </div> |
|
1940 <div class="section" title="6.4.3.until"> |
1003 <div class="titlepage"> |
1941 <div class="titlepage"> |
1004 <div> |
1942 <div> |
1005 <div> |
1943 <div> |
1006 <h4 class="title"> |
1944 <h4 class="title"> |
1007 <a name="id2945306"> |
1945 <a name="id2857683"> |
1008 </a> |
1946 </a> |
1009 1.2.4.Can I make a profit out of running a business involving Linux? |
1947 6.4.3. |
|
1948 <code class="literal"> |
|
1949 until |
|
1950 </code> |
1010 </h4> |
1951 </h4> |
1011 </div> |
1952 </div> |
1012 </div> |
1953 </div> |
1013 </div> |
1954 </div> |
1014 <p id="ch11Section_5_12"> |
1955 <p id="ch11Section_5_63"> |
1015 The answer is, "Yes!". The free software licenses, on which the various software packages of a distribution built on the Linux kernel are based, explicitly accommodate and encourage commercialization; the relationship between a Linux distribution as a whole and individual vendors may be seen as symbiotic. One common business model of commercial suppliers is charging for support, especially for business users. A number of companies also offer a specialized business version of their distribution, which adds proprietary support packages and tools to administer higher numbers of installations or to simplify administrative tasks. Another business model is to give away the software in order to sell hardware. Examples of corporations that are extensively (and sometimes exclusively) open-source and Linux-powered , with successful revenue generation models involving these, are Google, SUN, Mozilla, etc. |
1956 The |
1016 </p> |
1957 <code class="literal"> |
1017 </div> |
1958 until |
1018 <div class="section" title="1.2.5.Programming on Linux"> |
1959 </code> |
1019 <div class="titlepage"> |
1960 loop is similar to the |
1020 <div> |
1961 <code class="literal"> |
1021 <div> |
1962 while |
1022 <h4 class="title"> |
1963 </code> |
1023 <a name="id2945352"> |
1964 loop, except that it executes until the conditional command does not execute properly. |
1024 </a> |
1965 </p> |
1025 1.2.5.Programming on Linux |
1966 <p id="ch11Section_5_64"> |
1026 </h4> |
1967 The infinite loop changes to the following, when |
1027 </div> |
1968 <code class="literal"> |
1028 </div> |
1969 until |
1029 </div> |
1970 </code> |
1030 <p id="ch11Section_5_13"> |
1971 is used. |
1031 Most Linux distributions support dozens of programming languages. The most common collection of utilities for building both Linux applications and operating system programs is found within the GNU toolchain, which includes the GNU Compiler Collection (GCC) and the GNU build system. Amongst others, GCC provides compilers for Ada, C, C++, Java, and Fortran. The Linux kernel itself is written to be compiled with GCC. Proprietary compilers for Linux include the Intel C++ Compiler, Sun Studio, and IBM XL C/C++ Compiler. |
1972 </p> |
1032 </p> |
1973 <pre class="programlisting"> |
1033 <p id="ch11Section_5_14"> |
1974 until false |
1034 Most distributions also include support for PHP, Perl, Ruby, Python and other dynamic languages. Examples of languages that are less common, but still supported, are C# via the Mono project, sponsored by Novell, and Scheme. A number of Java Virtual Machines and development kits run on Linux, including the original Sun Microsystems JVM (HotSpot), and IBM's J2SE RE, as well as many open-source projects like Kaffe. |
1975 do |
1035 </p> |
1976 echo "True" |
1036 <p id="ch11Section_5_15"> |
1977 done |
1037 The two main frameworks for developing graphical applications are those of GNOME and KDE. These projects are based on the GTK+ and Qt widget toolkits, respectively, which can also be used independently of the larger framework. Both support a wide variety of languages. There are a number of Integrated Development Environments (IDEs) available including Anjuta, Code::Blocks, Eclipse, KDevelop, Lazarus, MonoDevelop, NetBeans, and Omnis Studio while the long-established editors Vim and Emacs remain popular. |
1978 </pre> |
1038 </p> |
1979 <p id="ch11Section_5_65"> |
|
1980 Now lets try and use these above mentioned options provided by shell to write a utility. Until now, when we try find or locate it looks through directories and files for result. But they wont search through tar archives and zipped files. Lets create a shell script for especially looking through these files |
|
1981 </p> |
|
1982 <pre class="programlisting"> |
|
1983 #!/bin/sh |
|
1984 |
|
1985 #To check number of arguments being passed. |
|
1986 if [ $# -eq 0 ] ; then |
|
1987 echo "Correct usage: $0 tar-archive filename nOr $0 filename" |
|
1988 exit 1 |
|
1989 else |
|
1990 if [ $# -eq 1 ] ; then |
|
1991 tar_archive=`find $PWD -name "*.tar*"` |
|
1992 else |
|
1993 tar_archive=`find $PWD -name $1` |
|
1994 fi |
|
1995 fi |
|
1996 |
|
1997 #Search of particular file inside archives. |
|
1998 for archive in $tar_archive |
|
1999 do |
|
2000 echo $archive |
|
2001 variable=`tar -tf $archive` |
|
2002 for word in $variable |
|
2003 do |
|
2004 if [ $# -eq 1 ] ; then |
|
2005 echo "$word" | grep -q ".*$1" |
|
2006 else |
|
2007 echo "$word" | grep -q ".*$2" |
|
2008 fi |
|
2009 if [ $? -eq 0 ] ; then |
|
2010 echo "File present in $archive!" |
|
2011 fi |
|
2012 done |
|
2013 done |
|
2014 </pre> |
1039 </div> |
2015 </div> |
1040 </div> |
2016 </div> |
1041 <div class="section" title="1.3.Reasons for Using Linux"> |
2017 <div class="section" title="6.5.Functions"> |
1042 <div class="titlepage"> |
2018 <div class="titlepage"> |
1043 <div> |
2019 <div> |
1044 <div> |
2020 <div> |
1045 <h3 class="title"> |
2021 <h3 class="title"> |
1046 <a name="id2945399"> |
2022 <a name="id2857738"> |
1047 </a> |
2023 </a> |
1048 1.3.Reasons for Using Linux |
2024 6.5.Functions |
1049 </h3> |
2025 </h3> |
1050 </div> |
2026 </div> |
1051 </div> |
2027 </div> |
1052 </div> |
2028 </div> |
1053 <div class="itemizedlist"> |
2029 <p id="ch11Section_5_66"> |
1054 <ul class="itemizedlist" type="*"> |
2030 When a group of commands are repeatedly being used within a script, it is convenient to group them as a function. This saves a lot of time and you can avoid retyping the code again and again. Also, it will help you maintain your code easily. Let's see how we can define a simple function, |
1055 <li class="listitem" style="list-style-type: *"> |
2031 <code class="literal"> |
1056 <p id="ch11Section_5_16"> |
2032 hello-world |
1057 Linux is free: |
2033 </code> |
1058 </p> |
2034 . Functions can be defined in bash, either using the |
1059 </li> |
2035 <code class="literal"> |
1060 </ul> |
2036 function |
1061 </div> |
2037 </code> |
1062 <p id="ch11Section_5_17"> |
2038 built-in followed by the function name or just the function name followed by a pair of parentheses. |
1063 As in "free beer". Linux can be downloaded in its entirety from the Internet completely for free. No registration fees, no costs per user, free updates, and freely available source code in case you want to change the behavior of your system. |
2039 </p> |
1064 Most of all, Linux is free as in "free speech": |
2040 <pre class="programlisting"> |
1065 The license commonly used is the GNU Public License (GPL). The license says that anybody who may want to do so, has the right to change Linux and eventually to redistribute a changed version, on the one condition that the code is still available after redistribution. In practice, you are free to grab a kernel image and sell the new code, as long as your customers can still have a copy of that code. |
2041 function hello-world |
1066 </p> |
2042 { |
1067 <div class="itemizedlist"> |
2043 echo "Hello, World."; |
1068 <ul class="itemizedlist" type="*"> |
2044 } |
1069 <li class="listitem" style="list-style-type: *"> |
2045 |
1070 <p id="ch11Section_5_18"> |
2046 hello-world () { |
1071 Linux is portable to any hardware platform: |
2047 echo "Hello, World."; |
1072 </p> |
2048 } |
1073 </li> |
2049 |
1074 </ul> |
2050 $ hello-world |
1075 </div> |
2051 Hello, World. |
1076 <p id="ch11Section_5_19"> |
2052 </pre> |
1077 A vendor, who wants to sell a new type of computer and who does not know what kind of OS his/her new machine will run, can take a Linux kernel and make it work on his/her hardware, because documentation related to this activity is freely available. |
2053 <p id="ch11Section_5_67"> |
1078 </p> |
2054 Passing parameters to functions is similar to passing them to scripts. |
1079 <div class="itemizedlist"> |
2055 </p> |
1080 <ul class="itemizedlist" type="*"> |
2056 <pre class="programlisting"> |
1081 <li class="listitem" style="list-style-type: *"> |
2057 function hello-name |
1082 <p id="ch11Section_5_1a"> |
2058 { |
1083 Linux was made to keep on running: |
2059 echo "Hello, $1."; |
1084 </p> |
2060 } |
1085 </li> |
2061 |
1086 </ul> |
2062 $ hello-name 9 |
1087 </div> |
2063 Hello, 9. |
1088 <p id="ch11Section_5_1b"> |
2064 </pre> |
1089 As with UNIX, a Linux system expects to run without rebooting all the time. That is why a lot of tasks are being executed at night or scheduled automatically for other times, resulting in higher availability during busier periods and a more balanced use of the hardware. This property allows for Linux to be applicable to environments where people do not have the time or the possibility to control their systems constantly. |
2065 <p id="ch11Section_5_68"> |
1090 </p> |
2066 Any variables that you define within a function, will be added to the global namespace. If you wish to define variables that are restricted to the scope of the function, define a variable using the |
1091 <div class="itemizedlist"> |
2067 <code class="literal"> |
1092 <ul class="itemizedlist" type="*"> |
2068 local |
1093 <li class="listitem" style="list-style-type: *"> |
2069 </code> |
1094 <p id="ch11Section_5_1c"> |
2070 built-in command of bash. |
1095 Linux is secure and versatile: |
2071 </p> |
1096 </p> |
2072 <p id="ch11Section_5_69"> |
1097 </li> |
2073 We shall now write a function for the word frequency generating script that we had looked at in the previous session. |
1098 </ul> |
2074 </p> |
1099 </div> |
2075 <pre class="programlisting"> |
1100 <p id="ch11Section_5_1d"> |
2076 function word_frequency { |
1101 The security model used in Linux is based on the UNIX idea of security, which is known to be robust and of proven quality. But Linux is not only safe from attacks from the Internet: it will adapt equally to other situations, utilizing the same high standards for security. |
2077 if [ $# -ne 1 ] |
1102 </p> |
2078 then |
1103 <div class="itemizedlist"> |
2079 echo "Usage: $0 file_name" |
1104 <ul class="itemizedlist" type="*"> |
2080 exit 1 |
1105 <li class="listitem" style="list-style-type: *"> |
2081 else |
1106 <p id="ch11Section_5_1e"> |
2082 if [ -f "$1" ] |
1107 Linux is scalable: |
2083 then |
1108 </p> |
2084 grep "[A-Za-z]*" -o "$1" | tr 'A-Z' 'a-z' | sort | uniq -c | sort -nr | less |
1109 </li> |
2085 fi |
1110 </ul> |
2086 fi |
1111 </div> |
2087 } |
1112 <p id="ch11Section_5_1f"> |
2088 </pre> |
1113 From a Palmtop with 2 MB of memory to a petabyte storage cluster with hundreds of nodes: add or remove the appropriate packages and Linux fits all. One does not need a supercomputer anymore,because you can use Linux to do big things using the building blocks provided with the system. If one wants to do little things, such as making an operating system for an embedded processor or just recycling your old 486, Linux will do that as well. |
2089 <p id="ch11Section_5_6a"> |
1114 </p> |
2090 As an exercise, modify the function to accept the input for the number of top frequency words to be shown (if none is given, assume 10). |
1115 <div class="itemizedlist"> |
|
1116 <ul class="itemizedlist" type="*"> |
|
1117 <li class="listitem" style="list-style-type: *"> |
|
1118 <p id="ch11Section_5_20"> |
|
1119 The Linux OS and Linux applications have very short debugtimes: |
|
1120 </p> |
|
1121 </li> |
|
1122 </ul> |
|
1123 </div> |
|
1124 <p id="ch11Section_5_21"> |
|
1125 Because Linux has been developed and tested by thousands of people, both errors and people to fix them are found very quickly. It often happens that there are only a couple of hours between discovery and fixing of a bug. |
|
1126 </p> |
2091 </p> |
1127 </div> |
2092 </div> |
1128 </div> |
2093 <div class="section" title="6.6.Further Reading:"> |
1129 <div class="section" title="2.Getting Started"> |
|
1130 <div class="titlepage"> |
|
1131 <div> |
|
1132 <div> |
|
1133 <h2 class="title" style="clear: both"> |
|
1134 <a name="id2945531"> |
|
1135 </a> |
|
1136 2.Getting Started |
|
1137 </h2> |
|
1138 </div> |
|
1139 </div> |
|
1140 </div> |
|
1141 <div class="section" title="2.1.Logging in, activating the user interface and logging out"> |
|
1142 <div class="titlepage"> |
2094 <div class="titlepage"> |
1143 <div> |
2095 <div> |
1144 <div> |
2096 <div> |
1145 <h3 class="title"> |
2097 <h3 class="title"> |
1146 <a name="id2945540"> |
2098 <a name="id2857809"> |
1147 </a> |
2099 </a> |
1148 2.1.Logging in, activating the user interface and logging out |
2100 6.6.Further Reading: |
1149 </h3> |
2101 </h3> |
1150 </div> |
2102 </div> |
1151 </div> |
2103 </div> |
1152 </div> |
2104 </div> |
1153 <p id="ch11Section_5_22"> |
|
1154 In order to work on a Linux system directly, one needs to provide a user name and password. You always need to authenticate to the system. Most PCbased Linux systems have two basic modes for a system to run in: either quick and clean in text console mode,which includes with mouse, multitasking and multiuser features, or in graphical console mode, which looks better but eats more system resources. |
|
1155 </p> |
|
1156 <div class="section" title="2.1.1.Graphical Mode"> |
|
1157 <div class="titlepage"> |
|
1158 <div> |
|
1159 <div> |
|
1160 <h4 class="title"> |
|
1161 <a name="id2945570"> |
|
1162 </a> |
|
1163 2.1.1.Graphical Mode |
|
1164 </h4> |
|
1165 </div> |
|
1166 </div> |
|
1167 </div> |
|
1168 <p id="ch11Section_5_23"> |
|
1169 This is the default nowadays on most desktop computers. You know you will be connecting to the system using graphical mode when you are first asked for your user name, and then to type your password. |
|
1170 </p> |
|
1171 <p id="ch11Section_5_24"> |
|
1172 To log in, make sure the mouse pointer is in the login window, provide your user name and password to the system and click |
|
1173 <span class="emphasis"> |
|
1174 <em> |
|
1175 OK |
|
1176 </em> |
|
1177 </span> |
|
1178 or press |
|
1179 <span class="emphasis"> |
|
1180 <em> |
|
1181 Enter |
|
1182 </em> |
|
1183 </span> |
|
1184 . |
|
1185 It is generally considered a bad idea to connect (graphically) using the root user name, the system adminstrator's account, since the use of graphics includes running a lot of extra programs, in root's case with a lot of extra permissions. To keep all risks as low as possible, use a normal user account to connect graphically. But there are enough risks to keep this in mind as a general advice, for all use of the root account: only log in as root when extra privileges are required. |
|
1186 </p> |
|
1187 <p id="ch11Section_5_25"> |
|
1188 After entering your user name/password combination, it can take a little while before the graphical environment is started, depending on the CPU speed of your computer, on the software you use and on your personal settings. |
|
1189 </p> |
|
1190 <p id="ch11Section_5_26"> |
|
1191 To continue, you will need to open a |
|
1192 <span class="emphasis"> |
|
1193 <em> |
|
1194 terminal window |
|
1195 </em> |
|
1196 </span> |
|
1197 or |
|
1198 <span class="emphasis"> |
|
1199 <em> |
|
1200 xterm |
|
1201 </em> |
|
1202 </span> |
|
1203 for short (X being the name for the underlying software supporting the graphical environment). This program can be found in the |
|
1204 <span class="emphasis"> |
|
1205 <em> |
|
1206 Applications>Utilities->System Tools |
|
1207 </em> |
|
1208 </span> |
|
1209 or |
|
1210 <span class="emphasis"> |
|
1211 <em> |
|
1212 Internet menu |
|
1213 </em> |
|
1214 </span> |
|
1215 , depending on what window manager you are using. There might be icons that you can use as a shortcut to get an |
|
1216 <span class="emphasis"> |
|
1217 <em> |
|
1218 xterm |
|
1219 </em> |
|
1220 </span> |
|
1221 window as well, and clicking the right mouse button on the desktop background will usually present you with a menu containing a terminal window application. |
|
1222 </p> |
|
1223 <p id="ch11Section_5_27"> |
|
1224 While browsing the menus, you will notice that a lot of things can be done without entering commands via the keyboard. For most users, the good old pointnclick method of dealing with the computer will do. But for those who want to enter the "heart" of the system, a tool stronger than a mouse will be required to handle the various tasks. This tool is the shell, and when in graphical mode, we activate our shell by opening a terminal window. |
|
1225 </p> |
|
1226 <p id="ch11Section_5_28"> |
|
1227 A terminal window should always show a command prompt when you open one. This terminal shows a standard prompt, which displays the user's login name, and the current working directory, represented by the twiddle (~) |
|
1228 </p> |
|
1229 <p id="ch11Section_5_29"> |
|
1230 Another common form for a prompt is this one: |
|
1231 [ |
|
1232 </p> |
|
1233 <div class="reference"> |
|
1234 <div class="titlepage"> |
|
1235 <hr /> |
|
1236 </div> |
|
1237 user@host |
|
1238 </div> |
|
1239 <p> |
|
1240 dir] |
|
1241 </p> |
|
1242 <p id="ch11Section_5_2a"> |
|
1243 In the above example, |
|
1244 <span class="emphasis"> |
|
1245 <em> |
|
1246 user |
|
1247 </em> |
|
1248 </span> |
|
1249 will be your login name, |
|
1250 <span class="emphasis"> |
|
1251 <em> |
|
1252 hosts |
|
1253 </em> |
|
1254 </span> |
|
1255 the name of the machine you are working on, and |
|
1256 <span class="emphasis"> |
|
1257 <em> |
|
1258 dir |
|
1259 </em> |
|
1260 </span> |
|
1261 an indication of your current location in the file system. Prompts can display all kinds of information, but they are not part of the commands you are giving to your system. To disconnect from the system in graphical mode, you need to close all terminal windows and other applications. After that, hit the |
|
1262 <span class="emphasis"> |
|
1263 <em> |
|
1264 logout |
|
1265 </em> |
|
1266 </span> |
|
1267 icon or find |
|
1268 <span class="emphasis"> |
|
1269 <em> |
|
1270 Log Out |
|
1271 </em> |
|
1272 </span> |
|
1273 in the menu. Closing everything is not really necessary, and the system can do this for you, but session management might put all currently open applications back on your screen when you connect again, which takes longer and is not always the desired effect. However, this behavior is configurable. |
|
1274 </p> |
|
1275 <p id="ch11Section_5_2b"> |
|
1276 When you see the login screen again, asking to enter user name and password, logout was successful. |
|
1277 </p> |
|
1278 </div> |
|
1279 <div class="section" title="2.1.2.Text Mode"> |
|
1280 <div class="titlepage"> |
|
1281 <div> |
|
1282 <div> |
|
1283 <h4 class="title"> |
|
1284 <a name="id2945728"> |
|
1285 </a> |
|
1286 2.1.2.Text Mode |
|
1287 </h4> |
|
1288 </div> |
|
1289 </div> |
|
1290 </div> |
|
1291 <p id="ch11Section_5_2c"> |
|
1292 One is in text mode when the whole screen is black, showing (in most cases white) characters. A text mode login screen typically shows some information about the machine you are working on, the name of the machine and a prompt waiting for you to log in. |
|
1293 </p> |
|
1294 <p id="ch11Section_5_2d"> |
|
1295 The login is different from a graphical login, in that you have to hit the |
|
1296 <span class="emphasis"> |
|
1297 <em> |
|
1298 Enter |
|
1299 </em> |
|
1300 </span> |
|
1301 key after providing your user name, because there are no buttons on the screen that you can click with the mouse. Then you should type your password, followed by another |
|
1302 <span class="emphasis"> |
|
1303 <em> |
|
1304 Enter |
|
1305 </em> |
|
1306 </span> |
|
1307 . You will not see any indication that you are entering something, not even an asterisk, and you won't see the cursor move. But this is normal on Linux and is done for security |
|
1308 reasons. |
|
1309 </p> |
|
1310 <p id="ch11Section_5_2e"> |
|
1311 When the system has accepted you as a valid user, you may get some more information, called the |
|
1312 <span class="emphasis"> |
|
1313 <em> |
|
1314 message of the day |
|
1315 </em> |
|
1316 </span> |
|
1317 , which can be anything. Additionally, it is popular on UNIX systems to display a fortune cookie, which contains some general wise or unwise (this is up to you) thoughts. After that, you will be given a shell, indicated with the same prompt that you would get in graphical mode. |
|
1318 </p> |
|
1319 <p id="ch11Section_5_2f"> |
|
1320 Also in text mode: log in as root only to do setup and configuration that absolutely requires administrator privileges, such as adding users, installing software packages, and performing network and other system configuration. Once you are finished, immediately leave the special account and resume your work as a nonprivileged user. |
|
1321 </p> |
|
1322 <p id="ch11Section_5_30"> |
|
1323 Logging out is done by entering the |
|
1324 <span class="emphasis"> |
|
1325 <em> |
|
1326 logout |
|
1327 </em> |
|
1328 </span> |
|
1329 command, followed by Enter. You are successfully disconnected from the system when you see the login screen again.Don't poweroff the computer after logging out. It is not meant to be shut off without application of the proper procedures for halting the system. Powering it off without going through the halting process might cause severe damage! |
|
1330 </p> |
|
1331 </div> |
|
1332 </div> |
|
1333 </div> |
|
1334 <div class="section" title="3.Basic Commands"> |
|
1335 <div class="titlepage"> |
|
1336 <div> |
|
1337 <div> |
|
1338 <h2 class="title" style="clear: both"> |
|
1339 <a name="id2945817"> |
|
1340 </a> |
|
1341 3.Basic Commands |
|
1342 </h2> |
|
1343 </div> |
|
1344 </div> |
|
1345 </div> |
|
1346 <div class="section" title="3.1.ls"> |
|
1347 <div class="titlepage"> |
|
1348 <div> |
|
1349 <div> |
|
1350 <h3 class="title"> |
|
1351 <a name="id2945826"> |
|
1352 </a> |
|
1353 3.1.ls |
|
1354 </h3> |
|
1355 </div> |
|
1356 </div> |
|
1357 </div> |
|
1358 <p id="ch11Section_5_31"> |
|
1359 When invoked without any arguments, |
|
1360 <span class="emphasis"> |
|
1361 <em> |
|
1362 ls |
|
1363 </em> |
|
1364 </span> |
|
1365 lists the files in the current working directory. A directory that is not the current working directory can be specified and ls will list the files there. The user also may specify any list of files and directories. In this case, all files and all contents of specified directories will be listed. The name |
|
1366 <span class="emphasis"> |
|
1367 <em> |
|
1368 ls |
|
1369 </em> |
|
1370 </span> |
|
1371 is derived from |
|
1372 <span class="emphasis"> |
|
1373 <em> |
|
1374 list segments |
|
1375 </em> |
|
1376 </span> |
|
1377 which was used in earlier systems. |
|
1378 </p> |
|
1379 <p id="ch11Section_5_32"> |
|
1380 Files whose names start with "." are not listed, unless the |
|
1381 <span class="emphasis"> |
|
1382 <em> |
|
1383 -a |
|
1384 </em> |
|
1385 </span> |
|
1386 flag is specified or the files are specified explicitly. |
|
1387 </p> |
|
1388 <p id="ch11Section_5_33"> |
|
1389 Without options, |
|
1390 <span class="emphasis"> |
|
1391 <em> |
|
1392 ls |
|
1393 </em> |
|
1394 </span> |
|
1395 displays files in a bare format. This bare format however makes it difficult to establish the type, permissions, and size of the files. The most common options to reveal this information or change the list of files are: |
|
1396 </p> |
|
1397 <div class="itemizedlist"> |
2105 <div class="itemizedlist"> |
1398 <ul class="itemizedlist" type="*"> |
2106 <ul class="itemizedlist" type="*"> |
1399 <li class="listitem" style="list-style-type: *"> |
2107 <li class="listitem" style="list-style-type: *"> |
1400 <p id="ch11Section_5_34"> |
2108 <p id="ch11Section_5_6b"> |
1401 <span class="emphasis"> |
|
1402 <em> |
|
1403 -l |
|
1404 </em> |
|
1405 </span> |
|
1406 long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename |
|
1407 </p> |
2109 </p> |
|
2110 <div class="reference"> |
|
2111 <div class="titlepage"> |
|
2112 <hr /> |
|
2113 </div> |
|
2114 http://www.freeos.com/guides/lsst/ |
|
2115 </div> |
1408 </li> |
2116 </li> |
1409 <li class="listitem" style="list-style-type: *"> |
2117 <li class="listitem" style="list-style-type: *"> |
1410 <p id="ch11Section_5_35"> |
2118 <p id="ch11Section_5_6c"> |
1411 <span class="emphasis"> |
|
1412 <em> |
|
1413 -F |
|
1414 </em> |
|
1415 </span> |
|
1416 appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix. |
|
1417 </p> |
2119 </p> |
|
2120 <div class="reference"> |
|
2121 <div class="titlepage"> |
|
2122 <hr /> |
|
2123 </div> |
|
2124 http://bash.cyberciti.biz/guide/Main_Page |
|
2125 </div> |
1418 </li> |
2126 </li> |
1419 <li class="listitem" style="list-style-type: *"> |
2127 <li class="listitem" style="list-style-type: *"> |
1420 <p id="ch11Section_5_36"> |
2128 <p id="ch11Section_5_6d"> |
1421 <span class="emphasis"> |
|
1422 <em> |
|
1423 -a |
|
1424 </em> |
|
1425 </span> |
|
1426 lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list. |
|
1427 </p> |
2129 </p> |
|
2130 <div class="reference"> |
|
2131 <div class="titlepage"> |
|
2132 <hr /> |
|
2133 </div> |
|
2134 http://tldp.org/LDP/abs/html/ |
|
2135 </div> |
1428 </li> |
2136 </li> |
1429 <li class="listitem" style="list-style-type: *"> |
2137 <li class="listitem" style="list-style-type: *"> |
1430 <p id="ch11Section_5_37"> |
2138 <p id="ch11Section_5_6e"> |
1431 <span class="emphasis"> |
|
1432 <em> |
|
1433 -R |
|
1434 </em> |
|
1435 </span> |
|
1436 recursively lists subdirectories. The command ls -R / would therefore list all files. |
|
1437 </p> |
2139 </p> |
1438 </li> |
2140 <div class="reference"> |
1439 <li class="listitem" style="list-style-type: *"> |
2141 <div class="titlepage"> |
1440 <p id="ch11Section_5_38"> |
2142 <hr /> |
1441 <span class="emphasis"> |
2143 </div> |
1442 <em> |
2144 http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html |
1443 -d |
2145 </div> |
1444 </em> |
|
1445 </span> |
|
1446 shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory. |
|
1447 </p> |
|
1448 </li> |
|
1449 <li class="listitem" style="list-style-type: *"> |
|
1450 <p id="ch11Section_5_39"> |
|
1451 <span class="emphasis"> |
|
1452 <em> |
|
1453 -t |
|
1454 </em> |
|
1455 </span> |
|
1456 sort the list of files by modification time. |
|
1457 </p> |
|
1458 </li> |
|
1459 <li class="listitem" style="list-style-type: *"> |
|
1460 <p id="ch11Section_5_3a"> |
|
1461 <span class="emphasis"> |
|
1462 <em> |
|
1463 -h |
|
1464 </em> |
|
1465 </span> |
|
1466 print sizes in human readable format. (e.g., 1K, 234M, 2G, etc.) |
|
1467 </p> |
|
1468 </li> |
2146 </li> |
1469 </ul> |
2147 </ul> |
1470 </div> |
2148 </div> |
1471 <p id="ch11Section_5_3b"> |
|
1472 In some environments, providing the option |
|
1473 <span class="emphasis"> |
|
1474 <em> |
|
1475 --color |
|
1476 </em> |
|
1477 </span> |
|
1478 (for GNU ls) or |
|
1479 <span class="emphasis"> |
|
1480 <em> |
|
1481 -G |
|
1482 </em> |
|
1483 </span> |
|
1484 (FreeBSD ls) causes ls to highlight different types of files with different colors, instead of with characters as |
|
1485 <span class="emphasis"> |
|
1486 <em> |
|
1487 -F |
|
1488 </em> |
|
1489 </span> |
|
1490 would. To determine what color to use for a file, GNU |
|
1491 <span class="emphasis"> |
|
1492 <em> |
|
1493 ls |
|
1494 </em> |
|
1495 </span> |
|
1496 checks the Unix file type, the file permissions, and the file extension, while FreeBSD |
|
1497 <span class="emphasis"> |
|
1498 <em> |
|
1499 ls |
|
1500 </em> |
|
1501 </span> |
|
1502 checks only the Unix file type and file permissions.: |
|
1503 </p> |
|
1504 <pre class="programlisting"> |
|
1505 $ ls |
|
1506 jeeves.rst psmith.html blandings.html |
|
1507 $ ls -l |
|
1508 drwxr--r-- 1 plum editors 4096 jeeves |
|
1509 -rw-r--r-- 1 plum editors 30405 psmith |
|
1510 -r-xr-xr-x 1 plum plum 8460 blandings |
|
1511 </pre> |
|
1512 <p id="ch11Section_5_3c"> |
|
1513 Here "$" actually is the beginning of the prompt. This is typical in most Unix-based systems. |
|
1514 </p> |
|
1515 </div> |
2149 </div> |
1516 <div class="section" title="3.2.date"> |
|
1517 <div class="titlepage"> |
|
1518 <div> |
|
1519 <div> |
|
1520 <h3 class="title"> |
|
1521 <a name="id2945987"> |
|
1522 </a> |
|
1523 3.2.date |
|
1524 </h3> |
|
1525 </div> |
|
1526 </div> |
|
1527 </div> |
|
1528 <p id="ch11Section_5_3d"> |
|
1529 The Unix date command displays the time and date. The super-user can use it to set the system clock. |
|
1530 </p> |
|
1531 <p id="ch11Section_5_3e"> |
|
1532 With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the timezone name, and the year. For example: |
|
1533 </p> |
|
1534 <pre class="programlisting"> |
|
1535 $date |
|
1536 Tue Sep 8 12:01:45 IST 2009 |
|
1537 </pre> |
|
1538 <p id="ch11Section_5_3f"> |
|
1539 On some systems to set the current date and time to September 8, 2004 01:22 you type: |
|
1540 </p> |
|
1541 <pre class="programlisting"> |
|
1542 $date --set="20040908 01:22" |
|
1543 </pre> |
|
1544 <p id="ch11Section_5_40"> |
|
1545 In order to view the various options for the |
|
1546 <span class="emphasis"> |
|
1547 <em> |
|
1548 date |
|
1549 </em> |
|
1550 </span> |
|
1551 command, type: |
|
1552 </p> |
|
1553 <pre class="programlisting"> |
|
1554 $man date |
|
1555 </pre> |
|
1556 <p id="ch11Section_5_41"> |
|
1557 This will take you to the "Manual" page comprising of all the details on the |
|
1558 <span class="emphasis"> |
|
1559 <em> |
|
1560 date |
|
1561 </em> |
|
1562 </span> |
|
1563 command. You can return to the terminal from the "man" page by pressing the |
|
1564 <span class="emphasis"> |
|
1565 <em> |
|
1566 Esc |
|
1567 </em> |
|
1568 </span> |
|
1569 key in the keyboard and typing ":q" in that order. |
|
1570 </p> |
|
1571 </div> |
|
1572 <div class="section" title="3.3.cd"> |
|
1573 <div class="titlepage"> |
|
1574 <div> |
|
1575 <div> |
|
1576 <h3 class="title"> |
|
1577 <a name="id2946047"> |
|
1578 </a> |
|
1579 3.3.cd |
|
1580 </h3> |
|
1581 </div> |
|
1582 </div> |
|
1583 </div> |
|
1584 <p id="ch11Section_5_42"> |
|
1585 This stands for "change directory". When one wants to go up to the parent directory, bypassing the tree of directories one has entered, cd .. can be used. |
|
1586 </p> |
|
1587 <p id="ch11Section_5_43"> |
|
1588 One dot '.' represents the current directory while two dots '..' represent the parent directory. |
|
1589 </p> |
|
1590 <p id="ch11Section_5_44"> |
|
1591 cd - will return you to the previous directory (a bit like an undo). |
|
1592 </p> |
|
1593 <p id="ch11Section_5_45"> |
|
1594 You can also use cd absolute path or cd relative path (see below): |
|
1595 </p> |
|
1596 <p id="ch11Section_5_46"> |
|
1597 Absolute paths: |
|
1598 </p> |
|
1599 <p id="ch11Section_5_47"> |
|
1600 An absolute path is easily recognised from the leading forward slash, /. The / means that you start at the top level directory and continue down. |
|
1601 </p> |
|
1602 <p id="ch11Section_5_48"> |
|
1603 For example to get to /boot/grub you would type: |
|
1604 </p> |
|
1605 <pre class="programlisting"> |
|
1606 $cd /boot/grub |
|
1607 </pre> |
|
1608 <p id="ch11Section_5_49"> |
|
1609 This is an absolute path because you start at the top of the hierarchy and go downwards from there (it doesn't matter where in the filesystem you were when you typed the command). |
|
1610 </p> |
|
1611 <p id="ch11Section_5_4a"> |
|
1612 Relative paths: |
|
1613 </p> |
|
1614 <p id="ch11Section_5_4b"> |
|
1615 A relative path doesn't have a preceding slash. Use a relative path when you start from a directory below the top level directory structure. This is dependent on where you are in the filesystem. |
|
1616 </p> |
|
1617 <p id="ch11Section_5_4c"> |
|
1618 For example if you are in root's home directory and want to get to /root/music, you type: |
|
1619 </p> |
|
1620 <pre class="programlisting"> |
|
1621 $ cd music |
|
1622 </pre> |
|
1623 <p id="ch11Section_5_4d"> |
|
1624 Please note that there is no / using the above cd command. Using a / would cause this to be an absolute path, working from the top of the hierarchy downward. |
|
1625 </p> |
|
1626 </div> |
|
1627 <div class="section" title="3.4.who"> |
|
1628 <div class="titlepage"> |
|
1629 <div> |
|
1630 <div> |
|
1631 <h3 class="title"> |
|
1632 <a name="id2946147"> |
|
1633 </a> |
|
1634 3.4.who |
|
1635 </h3> |
|
1636 </div> |
|
1637 </div> |
|
1638 </div> |
|
1639 <p id="ch11Section_5_4e"> |
|
1640 The standard Unix command |
|
1641 <span class="emphasis"> |
|
1642 <em> |
|
1643 who |
|
1644 </em> |
|
1645 </span> |
|
1646 displays a list of users who are currently logged into a computer. |
|
1647 </p> |
|
1648 <p id="ch11Section_5_4f"> |
|
1649 The |
|
1650 <span class="emphasis"> |
|
1651 <em> |
|
1652 who |
|
1653 </em> |
|
1654 </span> |
|
1655 command is related to the command |
|
1656 <span class="emphasis"> |
|
1657 <em> |
|
1658 w |
|
1659 </em> |
|
1660 </span> |
|
1661 , which provides the same information but also displays additional data and statistics.: |
|
1662 </p> |
|
1663 <pre class="programlisting"> |
|
1664 $who |
|
1665 beeblebrox tty7 2009-09-08 10:50 (:0) |
|
1666 beeblebrox pts/0 2009-09-08 11:25 (:0.0) |
|
1667 dumbledore pts/1 2009-09-08 18:11 (potter.xyz.in) |
|
1668 beeblebrox pts/2 2009-09-08 18:53 (:0.0) |
|
1669 </pre> |
|
1670 <p id="ch11Section_5_50"> |
|
1671 The command can be invoked with the arguments |
|
1672 <span class="emphasis"> |
|
1673 <em> |
|
1674 am i |
|
1675 </em> |
|
1676 </span> |
|
1677 or |
|
1678 <span class="emphasis"> |
|
1679 <em> |
|
1680 am I |
|
1681 </em> |
|
1682 </span> |
|
1683 (so it is invoked as |
|
1684 <span class="emphasis"> |
|
1685 <em> |
|
1686 who am i |
|
1687 </em> |
|
1688 </span> |
|
1689 or * who am I*), showing information about the current terminal only (see the |
|
1690 <span class="emphasis"> |
|
1691 <em> |
|
1692 -m |
|
1693 </em> |
|
1694 </span> |
|
1695 option below, of which this invocation is equivalent). |
|
1696 </p> |
|
1697 <p id="ch11Section_5_51"> |
|
1698 In order to find out the various options that can be appended to the |
|
1699 <span class="emphasis"> |
|
1700 <em> |
|
1701 who |
|
1702 </em> |
|
1703 </span> |
|
1704 command, check the |
|
1705 <span class="emphasis"> |
|
1706 <em> |
|
1707 man |
|
1708 </em> |
|
1709 </span> |
|
1710 page by typing out the following in the terminal: |
|
1711 </p> |
|
1712 <pre class="programlisting"> |
|
1713 $man who |
|
1714 </pre> |
|
1715 <p id="ch11Section_5_52"> |
|
1716 This will take you to the "Manual" page containing details about the |
|
1717 <span class="emphasis"> |
|
1718 <em> |
|
1719 who |
|
1720 </em> |
|
1721 </span> |
|
1722 command |
|
1723 </p> |
|
1724 </div> |
|
1725 <div class="section" title="3.5.mkdir"> |
|
1726 <div class="titlepage"> |
|
1727 <div> |
|
1728 <div> |
|
1729 <h3 class="title"> |
|
1730 <a name="id2946229"> |
|
1731 </a> |
|
1732 3.5.mkdir |
|
1733 </h3> |
|
1734 </div> |
|
1735 </div> |
|
1736 </div> |
|
1737 <p id="ch11Section_5_53"> |
|
1738 This command is used to make a new directory. Normal usage is as straightforward as follows: |
|
1739 </p> |
|
1740 <pre class="programlisting"> |
|
1741 $mkdir name_of_directory |
|
1742 </pre> |
|
1743 <p id="ch11Section_5_54"> |
|
1744 Where |
|
1745 <span class="emphasis"> |
|
1746 <em> |
|
1747 name_of_directory |
|
1748 </em> |
|
1749 </span> |
|
1750 is the name of the directory one wants to create. When typed as above (ie. normal usage), the new directory would be created within the current directory. On Unix, multiple directories can be specified, and |
|
1751 <span class="emphasis"> |
|
1752 <em> |
|
1753 mkdir |
|
1754 </em> |
|
1755 </span> |
|
1756 will try to create all of them. |
|
1757 </p> |
|
1758 <div class="section" title="3.5.1.Options"> |
|
1759 <div class="titlepage"> |
|
1760 <div> |
|
1761 <div> |
|
1762 <h4 class="title"> |
|
1763 <a name="id2946261"> |
|
1764 </a> |
|
1765 3.5.1.Options |
|
1766 </h4> |
|
1767 </div> |
|
1768 </div> |
|
1769 </div> |
|
1770 <p id="ch11Section_5_55"> |
|
1771 On Unix-like operating systems, |
|
1772 <span class="emphasis"> |
|
1773 <em> |
|
1774 mkdir |
|
1775 </em> |
|
1776 </span> |
|
1777 takes options. Three of the most common options are: |
|
1778 </p> |
|
1779 <div class="itemizedlist"> |
|
1780 <ul class="itemizedlist" type="*"> |
|
1781 <li class="listitem" style="list-style-type: *"> |
|
1782 <p id="ch11Section_5_56"> |
|
1783 <span class="emphasis"> |
|
1784 <em> |
|
1785 -p |
|
1786 </em> |
|
1787 </span> |
|
1788 : will also create all directories leading up to the given directory that do not exist already. If the given directory already exists, ignore the error. |
|
1789 </p> |
|
1790 </li> |
|
1791 <li class="listitem" style="list-style-type: *"> |
|
1792 <p id="ch11Section_5_57"> |
|
1793 <span class="emphasis"> |
|
1794 <em> |
|
1795 -v |
|
1796 </em> |
|
1797 </span> |
|
1798 : display each directory that mkdir creates. Most often used with -p. |
|
1799 </p> |
|
1800 </li> |
|
1801 <li class="listitem" style="list-style-type: *"> |
|
1802 <p id="ch11Section_5_58"> |
|
1803 <span class="emphasis"> |
|
1804 <em> |
|
1805 -m |
|
1806 </em> |
|
1807 </span> |
|
1808 : specify the octal permissions of directories created by mkdir. |
|
1809 </p> |
|
1810 </li> |
|
1811 </ul> |
|
1812 </div> |
|
1813 <p id="ch11Section_5_59"> |
|
1814 <span class="emphasis"> |
|
1815 <em> |
|
1816 -p |
|
1817 </em> |
|
1818 </span> |
|
1819 is most often used when using mkdir to build up complex directory hierarchies, in case a necessary directory is missing or already there. -m is commonly used to lock down temporary directories used by shell scripts. |
|
1820 </p> |
|
1821 </div> |
|
1822 <div class="section" title="3.5.2.Examples"> |
|
1823 <div class="titlepage"> |
|
1824 <div> |
|
1825 <div> |
|
1826 <h4 class="title"> |
|
1827 <a name="id2946319"> |
|
1828 </a> |
|
1829 3.5.2.Examples |
|
1830 </h4> |
|
1831 </div> |
|
1832 </div> |
|
1833 </div> |
|
1834 <p id="ch11Section_5_5a"> |
|
1835 An example of |
|
1836 <span class="emphasis"> |
|
1837 <em> |
|
1838 -p |
|
1839 </em> |
|
1840 </span> |
|
1841 in action is: |
|
1842 </p> |
|
1843 <pre class="programlisting"> |
|
1844 $mkdir -p /tmp/a/b/c |
|
1845 </pre> |
|
1846 <p id="ch11Section_5_5b"> |
|
1847 If |
|
1848 <span class="emphasis"> |
|
1849 <em> |
|
1850 /tmp/a |
|
1851 </em> |
|
1852 </span> |
|
1853 exists but |
|
1854 <span class="emphasis"> |
|
1855 <em> |
|
1856 /tmp/a/b |
|
1857 </em> |
|
1858 </span> |
|
1859 does not, mkdir will create |
|
1860 <span class="emphasis"> |
|
1861 <em> |
|
1862 /tmp/a/b |
|
1863 </em> |
|
1864 </span> |
|
1865 before creating |
|
1866 <span class="emphasis"> |
|
1867 <em> |
|
1868 /tmp/a/b/c |
|
1869 </em> |
|
1870 </span> |
|
1871 . |
|
1872 </p> |
|
1873 <p id="ch11Section_5_5c"> |
|
1874 And an even more powerful command, creating a full tree at once (this however is a Shell extension, nothing mkdir does itself): |
|
1875 </p> |
|
1876 <pre class="programlisting"> |
|
1877 $mkdir -p tmpdir/{trunk/sources/{includes,docs},branches,tags} |
|
1878 </pre> |
|
1879 <p id="ch11Section_5_5d"> |
|
1880 This will create: |
|
1881 </p> |
|
1882 tmpdir - branches |
|
1883 <div class="itemizedlist"> |
|
1884 <ul class="itemizedlist" type="*"> |
|
1885 <li class="listitem" style="list-style-type: *"> |
|
1886 <p id="ch11Section_5_5e"> |
|
1887 tag |
|
1888 </p> |
|
1889 </li> |
|
1890 <li class="listitem" style="list-style-type: *"> |
|
1891 trunk - sources - includes |
|
1892 <div class="itemizedlist"> |
|
1893 <ul class="itemizedlist" type="*"> |
|
1894 <li class="listitem" style="list-style-type: *"> |
|
1895 <p id="ch11Section_5_5f"> |
|
1896 docs |
|
1897 </p> |
|
1898 </li> |
|
1899 </ul> |
|
1900 </div> |
|
1901 </li> |
|
1902 </ul> |
|
1903 </div> |
|
1904 </div> |
|
1905 </div> |
|
1906 </div> |
|
1907 <div class="section" title="4.Getting Help"> |
|
1908 <div class="titlepage"> |
|
1909 <div> |
|
1910 <div> |
|
1911 <h2 class="title" style="clear: both"> |
|
1912 <a name="id2946395"> |
|
1913 </a> |
|
1914 4.Getting Help |
|
1915 </h2> |
|
1916 </div> |
|
1917 </div> |
|
1918 </div> |
|
1919 <div class="section" title="4.1.apropos and whatis"> |
|
1920 <div class="titlepage"> |
|
1921 <div> |
|
1922 <div> |
|
1923 <h3 class="title"> |
|
1924 <a name="id2946403"> |
|
1925 </a> |
|
1926 4.1.apropos and whatis |
|
1927 </h3> |
|
1928 </div> |
|
1929 </div> |
|
1930 </div> |
|
1931 <p id="ch11Section_5_60"> |
|
1932 This is a command to search the manual pages files in Unix and Unix-like operating systems. |
|
1933 </p> |
|
1934 <pre class="programlisting"> |
|
1935 $ apropos grep |
|
1936 egrep egrep (1) Search a file for a pattern using full regular expressions |
|
1937 fgrep fgrep (1) Search a file for a fixed-character string |
|
1938 fmlgrep fmlgrep (1) Search a file for a pattern |
|
1939 grep grep (1) Search a file for a pattern |
|
1940 gzgrep gzgrep (1) Search a possibly compressed file for a regular expression |
|
1941 nisgrep nismatch (1) Utilities for searching NIS+ tables |
|
1942 pgrep pgrep (1) Find or signal a process by name or other attribute |
|
1943 zgrep zgrep (1) Search a possibly compressed file for a regular expression |
|
1944 ... |
|
1945 </pre> |
|
1946 <p id="ch11Section_5_61"> |
|
1947 In this example, the user uses |
|
1948 <span class="emphasis"> |
|
1949 <em> |
|
1950 apropos |
|
1951 </em> |
|
1952 </span> |
|
1953 to search for the string "grep", and apropos returns the indicated |
|
1954 <span class="emphasis"> |
|
1955 <em> |
|
1956 man |
|
1957 </em> |
|
1958 </span> |
|
1959 pages that include the term "grep". |
|
1960 </p> |
|
1961 <p id="ch11Section_5_62"> |
|
1962 A short index of explanations for commands is available using the |
|
1963 <span class="emphasis"> |
|
1964 <em> |
|
1965 whatis |
|
1966 </em> |
|
1967 </span> |
|
1968 command, like in the examples below: |
|
1969 </p> |
|
1970 <pre class="programlisting"> |
|
1971 $whatis ls |
|
1972 ls (1) - list directory contents |
|
1973 </pre> |
|
1974 <p id="ch11Section_5_63"> |
|
1975 This displays short information about a command, and the first section in the collection of man pages that contains an appropriate page. |
|
1976 </p> |
|
1977 <p id="ch11Section_5_64"> |
|
1978 If you don't know where to get started and which man page to read, |
|
1979 <span class="emphasis"> |
|
1980 <em> |
|
1981 apropos |
|
1982 </em> |
|
1983 </span> |
|
1984 gives more information. Say that you do not know how to start a browser, then you could enter the following command: |
|
1985 </p> |
|
1986 <pre class="programlisting"> |
|
1987 $apropos browser |
|
1988 gmusicbrowser (1) - Jukebox for large collections of audio files |
|
1989 infobrowser (1) - read Info documents |
|
1990 libsmbclient (7) - An extension library for browsers and that can be used... |
|
1991 opera (1) - a standards-compliant graphical Web browser |
|
1992 sensible-browser (1) - sensible editing, paging, and web browsing |
|
1993 smbtree (1) - A text based smb network browser |
|
1994 tvtk_doc (1) - A GUI based TVTK documentation search browser. |
|
1995 viewres (1) - graphical class browser for Xt |
|
1996 w3m (1) - a text based Web browser and pager |
|
1997 www-browser (1) - a text based Web browser and pager |
|
1998 ... |
|
1999 </pre> |
|
2000 </div> |
|
2001 <div class="section" title="4.2.man"> |
|
2002 <div class="titlepage"> |
|
2003 <div> |
|
2004 <div> |
|
2005 <h3 class="title"> |
|
2006 <a name="id2946487"> |
|
2007 </a> |
|
2008 4.2.man |
|
2009 </h3> |
|
2010 </div> |
|
2011 </div> |
|
2012 </div> |
|
2013 <p id="ch11Section_5_65"> |
|
2014 Man pages (short for "manual pages") are the extensive documentation that comes preinstalled with almost all substantial Unix and Unix-like operating systems. The Unix command used to display them is |
|
2015 <span class="emphasis"> |
|
2016 <em> |
|
2017 man |
|
2018 </em> |
|
2019 </span> |
|
2020 . Each page is a self-contained document. |
|
2021 </p> |
|
2022 <p id="ch11Section_5_66"> |
|
2023 To read a manual page for a Unix command, one can use: |
|
2024 </p> |
|
2025 <pre class="programlisting"> |
|
2026 $ man <command_name> |
|
2027 </pre> |
|
2028 <p id="ch11Section_5_67"> |
|
2029 at a shell prompt; for example, "man ftp". In order to simplify navigation through the output, |
|
2030 <span class="emphasis"> |
|
2031 <em> |
|
2032 man |
|
2033 </em> |
|
2034 </span> |
|
2035 generally uses the less terminal pager. |
|
2036 </p> |
|
2037 <p id="ch11Section_5_68"> |
|
2038 Pages are traditionally referred to using the notation "name(section)"; for example, ftp(1). The same page name may appear in more than one section of the manual, this can occur when the names of system calls, user commands, or macro packages coincide. Two examples are |
|
2039 <span class="emphasis"> |
|
2040 <em> |
|
2041 man(1) |
|
2042 </em> |
|
2043 </span> |
|
2044 and |
|
2045 <span class="emphasis"> |
|
2046 <em> |
|
2047 man(7) |
|
2048 </em> |
|
2049 </span> |
|
2050 , or |
|
2051 <span class="emphasis"> |
|
2052 <em> |
|
2053 exit(2) |
|
2054 </em> |
|
2055 </span> |
|
2056 and |
|
2057 <span class="emphasis"> |
|
2058 <em> |
|
2059 exit(3) |
|
2060 </em> |
|
2061 </span> |
|
2062 . The syntax for accessing the non-default manual section varies between different man implementations. On Linux and |
|
2063 <span class="emphasis"> |
|
2064 <em> |
|
2065 BSD, for example, the syntax for reading *printf(3) |
|
2066 </em> |
|
2067 </span> |
|
2068 is: |
|
2069 </p> |
|
2070 <pre class="programlisting"> |
|
2071 $man 3 printf |
|
2072 </pre> |
|
2073 <p id="ch11Section_5_69"> |
|
2074 Another example: |
|
2075 </p> |
|
2076 <pre class="programlisting"> |
|
2077 $man man |
|
2078 </pre> |
|
2079 <p id="ch11Section_5_6a"> |
|
2080 The previous example will take you to the "Manual" page entry about manual pages! |
|
2081 </p> |
|
2082 <div class="section" title="4.2.1.Layout"> |
|
2083 <div class="titlepage"> |
|
2084 <div> |
|
2085 <div> |
|
2086 <h4 class="title"> |
|
2087 <a name="id2946569"> |
|
2088 </a> |
|
2089 4.2.1.Layout |
|
2090 </h4> |
|
2091 </div> |
|
2092 </div> |
|
2093 </div> |
|
2094 <p id="ch11Section_5_6b"> |
|
2095 All man pages follow a common layout that is optimized for presentation on a simple ASCII text display, possibly without any form of highlighting or font control. Sections present may include: |
|
2096 </p> |
|
2097 NAME |
|
2098 <p id="ch11Section_5_6c"> |
|
2099 The name of the command or function, followed by a one-line description of what it does. |
|
2100 </p> |
|
2101 SYNOPSIS |
|
2102 <p id="ch11Section_5_6d"> |
|
2103 In the case of a command, you get a formal description of how to run it and what command line options it takes. For program functions, a list of the parameters the function takes and which header file contains its definition. For experienced users, this may be all the documentation they need. |
|
2104 </p> |
|
2105 DESCRIPTION |
|
2106 <p id="ch11Section_5_6e"> |
|
2107 A textual description of the functioning of the command or function. |
|
2108 </p> |
|
2109 EXAMPLES |
|
2110 <p id="ch11Section_5_6f"> |
|
2111 Some examples of common usage. |
|
2112 </p> |
|
2113 SEE ALSO |
|
2114 <p id="ch11Section_5_70"> |
|
2115 A list of related commands or functions. |
|
2116 </p> |
|
2117 <p id="ch11Section_5_71"> |
|
2118 Other sections may be present, but these are not well standardized across man pages. Common examples include: OPTIONS, EXIT STATUS, ENVIRONMENT, KNOWN BUGS, FILES, AUTHOR, REPORTING BUGS, HISTORY and COPYRIGHT. |
|
2119 </p> |
|
2120 <p id="ch11Section_5_72"> |
|
2121 These days virtually every Unix command line application comes with its man page, and many Unix users perceive a lack of man pages as a sign of low quality; indeed, some projects, such as Debian, go out of their way to write man pages for programs lacking one. Few alternatives to |
|
2122 <span class="emphasis"> |
|
2123 <em> |
|
2124 man |
|
2125 </em> |
|
2126 </span> |
|
2127 have enjoyed much popularity, with the possible exception of the GNU project's "info" system, an early and simple hypertext system. |
|
2128 </p> |
|
2129 <p id="ch11Section_5_73"> |
|
2130 However, the format of a single page for each application, the lack of classification within the sections and the relatively unsophisticated formatting facilities have motivated the development of alternative documentation systems, such as the previously mentioned "info" system. |
|
2131 </p> |
|
2132 <p id="ch11Section_5_74"> |
|
2133 Most Unix GUI applications (particularly those built using the GNOME and KDE development environments) now provide end-user documentation in HTML and include embedded HTML viewers such as yelp for reading the help within the application. |
|
2134 </p> |
|
2135 <p id="ch11Section_5_75"> |
|
2136 Usually the man pages are written in English. Translations into other languages can be also available on the system. |
|
2137 </p> |
|
2138 <p id="ch11Section_5_76"> |
|
2139 The default format of the man pages is troff, with either the macro package man (appearance oriented) or on some systems mdoc (semantic oriented). This makes it possible to typeset a man page to PostScript, PDF and various other formats for viewing or printing. |
|
2140 </p> |
|
2141 </div> |
|
2142 </div> |
|
2143 <div class="section" title="4.3.info"> |
|
2144 <div class="titlepage"> |
|
2145 <div> |
|
2146 <div> |
|
2147 <h3 class="title"> |
|
2148 <a name="id2946679"> |
|
2149 </a> |
|
2150 4.3.info |
|
2151 </h3> |
|
2152 </div> |
|
2153 </div> |
|
2154 </div> |
|
2155 <p id="ch11Section_5_77"> |
|
2156 <span class="emphasis"> |
|
2157 <em> |
|
2158 info |
|
2159 </em> |
|
2160 </span> |
|
2161 is a software utility which forms a hypertextual, multipage documentation and help viewer working on a command line interface, useful when there is no GUI available. |
|
2162 </p> |
|
2163 <p id="ch11Section_5_78"> |
|
2164 The syntax is |
|
2165 </p> |
|
2166 <pre class="programlisting"> |
|
2167 $ info <command_name> |
|
2168 </pre> |
|
2169 <p id="ch11Section_5_79"> |
|
2170 <span class="emphasis"> |
|
2171 <em> |
|
2172 info |
|
2173 </em> |
|
2174 </span> |
|
2175 processes info files, which are Texinfo formatted files, and presents the documentation as a tree, with simple commands to traverse the tree and to follow cross references. For instance |
|
2176 </p> |
|
2177 <div class="itemizedlist"> |
|
2178 <ul class="itemizedlist" type="*"> |
|
2179 <li class="listitem" style="list-style-type: *"> |
|
2180 <p id="ch11Section_5_7a"> |
|
2181 <span class="emphasis"> |
|
2182 <em> |
|
2183 n |
|
2184 </em> |
|
2185 </span> |
|
2186 goes to the next page. |
|
2187 </p> |
|
2188 </li> |
|
2189 <li class="listitem" style="list-style-type: *"> |
|
2190 <p id="ch11Section_5_7b"> |
|
2191 <span class="emphasis"> |
|
2192 <em> |
|
2193 p |
|
2194 </em> |
|
2195 </span> |
|
2196 goes to the previous page. |
|
2197 </p> |
|
2198 </li> |
|
2199 <li class="listitem" style="list-style-type: *"> |
|
2200 <p id="ch11Section_5_7c"> |
|
2201 <span class="emphasis"> |
|
2202 <em> |
|
2203 u |
|
2204 </em> |
|
2205 </span> |
|
2206 goes to the upper page. |
|
2207 </p> |
|
2208 </li> |
|
2209 <li class="listitem" style="list-style-type: *"> |
|
2210 <p id="ch11Section_5_7d"> |
|
2211 <span class="emphasis"> |
|
2212 <em> |
|
2213 l |
|
2214 </em> |
|
2215 </span> |
|
2216 goes to the last(visited) node |
|
2217 </p> |
|
2218 </li> |
|
2219 <li class="listitem" style="list-style-type: *"> |
|
2220 <p id="ch11Section_5_7e"> |
|
2221 To follow a cross reference, the cursor can be moved over a link (a word preceded by a *) and enter pressed. |
|
2222 </p> |
|
2223 </li> |
|
2224 </ul> |
|
2225 </div> |
|
2226 <p id="ch11Section_5_7f"> |
|
2227 info was initially written for use with GNU/Linux and then ported to other Unix-like operating systems. |
|
2228 </p> |
|
2229 </div> |
|
2230 <div class="section" title="4.4.--help"> |
|
2231 <div class="titlepage"> |
|
2232 <div> |
|
2233 <div> |
|
2234 <h3 class="title"> |
|
2235 <a name="id2946765"> |
|
2236 </a> |
|
2237 4.4.--help |
|
2238 </h3> |
|
2239 </div> |
|
2240 </div> |
|
2241 </div> |
|
2242 <p id="ch11Section_5_80"> |
|
2243 Most GNU commands support the --help, which gives a short explanation about how to use the command and a list of available options. Below is the output of this option with the |
|
2244 <span class="emphasis"> |
|
2245 <em> |
|
2246 cat |
|
2247 </em> |
|
2248 </span> |
|
2249 command: |
|
2250 </p> |
|
2251 <pre class="programlisting"> |
|
2252 $ userprompt@host: cat --help |
|
2253 Usage: cat [OPTION] [FILE]... |
|
2254 Concatenate FILE(s), or standard input, to standard output. |
|
2255 |
|
2256 -A, --show-all equivalent to -vET |
|
2257 -b, --number-nonblank number nonempty output lines |
|
2258 -e equivalent to -vE |
|
2259 -E, --show-ends display $ at end of each line |
|
2260 -n, --number number all output lines |
|
2261 -s, --squeeze-blank suppress repeated empty output lines |
|
2262 -t equivalent to -vT |
|
2263 -T, --show-tabs display TAB characters as ^I |
|
2264 -u (ignored) |
|
2265 -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB |
|
2266 --help display this help and exit |
|
2267 --version output version information and exit |
|
2268 |
|
2269 With no FILE, or when FILE is -, read standard input. |
|
2270 |
|
2271 Examples: |
|
2272 cat f - g Output f's contents, then standard input, then g's contents. |
|
2273 cat Copy standard input to standard output. |
|
2274 |
|
2275 Report bugs to <bug-coreutils@gnu.org>. |
|
2276 </pre> |
|
2277 </div> |
|
2278 </div> |
|
2279 <div class="section" title="5.Basic file handling"> |
|
2280 <div class="titlepage"> |
|
2281 <div> |
|
2282 <div> |
|
2283 <h2 class="title" style="clear: both"> |
|
2284 <a name="id2946817"> |
|
2285 </a> |
|
2286 5.Basic file handling |
|
2287 </h2> |
|
2288 </div> |
|
2289 </div> |
|
2290 </div> |
|
2291 <div class="section" title="5.1.cp"> |
|
2292 <div class="titlepage"> |
|
2293 <div> |
|
2294 <div> |
|
2295 <h3 class="title"> |
|
2296 <a name="id2946826"> |
|
2297 </a> |
|
2298 5.1.cp |
|
2299 </h3> |
|
2300 </div> |
|
2301 </div> |
|
2302 </div> |
|
2303 <p id="ch11Section_5_81"> |
|
2304 <span class="emphasis"> |
|
2305 <em> |
|
2306 cp |
|
2307 </em> |
|
2308 </span> |
|
2309 is the command entered in a Unix shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name. |
|
2310 </p> |
|
2311 <div class="section" title="5.1.1.Usage"> |
|
2312 <div class="titlepage"> |
|
2313 <div> |
|
2314 <div> |
|
2315 <h4 class="title"> |
|
2316 <a name="id2946843"> |
|
2317 </a> |
|
2318 5.1.1.Usage |
|
2319 </h4> |
|
2320 </div> |
|
2321 </div> |
|
2322 </div> |
|
2323 <p id="ch11Section_5_82"> |
|
2324 To copy a file to another file: |
|
2325 </p> |
|
2326 <pre class="programlisting"> |
|
2327 $ cp [ -f ] [ -H ] [ -i ] [ -p ][ -- ] SourceFile TargetFile |
|
2328 </pre> |
|
2329 <p id="ch11Section_5_83"> |
|
2330 To copy a file to a directory: |
|
2331 </p> |
|
2332 <pre class="programlisting"> |
|
2333 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -r | -R ] [ -- ] SourceFile ... TargetDirectory |
|
2334 </pre> |
|
2335 <p id="ch11Section_5_84"> |
|
2336 To copy a directory to a directory: |
|
2337 </p> |
|
2338 <pre class="programlisting"> |
|
2339 $ cp [ -f ] [ -H ] [ -i ] [ -p ] [ -- ] { -r | -R } |
|
2340 SourceDirectory ... TargetDirectory |
|
2341 </pre> |
|
2342 </div> |
|
2343 <div class="section" title="5.1.2.Flags"> |
|
2344 <div class="titlepage"> |
|
2345 <div> |
|
2346 <div> |
|
2347 <h4 class="title"> |
|
2348 <a name="id2946877"> |
|
2349 </a> |
|
2350 5.1.2.Flags |
|
2351 </h4> |
|
2352 </div> |
|
2353 </div> |
|
2354 </div> |
|
2355 <p id="ch11Section_5_85"> |
|
2356 <span class="emphasis"> |
|
2357 <em> |
|
2358 -f |
|
2359 </em> |
|
2360 </span> |
|
2361 (force) specifies removal of the target file if it cannot be opened for write operations. The removal precedes any copying performed by the cp command. |
|
2362 </p> |
|
2363 <p id="ch11Section_5_86"> |
|
2364 <span class="emphasis"> |
|
2365 <em> |
|
2366 -P |
|
2367 </em> |
|
2368 </span> |
|
2369 makes the cp command copy symbolic links. The default is to follow symbolic links, that is, to copy files to which symbolic links point. |
|
2370 </p> |
|
2371 <p id="ch11Section_5_87"> |
|
2372 <span class="emphasis"> |
|
2373 <em> |
|
2374 -i |
|
2375 </em> |
|
2376 </span> |
|
2377 (interactive) prompts you with the name of a file to be overwritten. This occurs if the TargetDirectory or TargetFile parameter contains a file with the same name as a file specified in the SourceFile or SourceDirectory parameter. If you enter y or the locale's equivalent of y, the cp command continues. Any other answer prevents the cp command from overwriting the file. |
|
2378 </p> |
|
2379 <p id="ch11Section_5_88"> |
|
2380 <span class="emphasis"> |
|
2381 <em> |
|
2382 -p |
|
2383 </em> |
|
2384 </span> |
|
2385 (preserve) duplicates the following characteristics of each SourceFile/SourceDirectory in the corresponding TargetFile and/or TargetDirectory: |
|
2386 </p> |
|
2387 <div class="itemizedlist"> |
|
2388 <ul class="itemizedlist" type="*"> |
|
2389 <li class="listitem" style="list-style-type: *"> |
|
2390 <p id="ch11Section_5_89"> |
|
2391 The time of the last data modification and the time of the last access. |
|
2392 </p> |
|
2393 </li> |
|
2394 <li class="listitem" style="list-style-type: *"> |
|
2395 <p id="ch11Section_5_8a"> |
|
2396 The user ID and group ID (only if it has permissions to do this) |
|
2397 </p> |
|
2398 </li> |
|
2399 <li class="listitem" style="list-style-type: *"> |
|
2400 <p id="ch11Section_5_8b"> |
|
2401 The file permission bits and the SUID and SGID bits. |
|
2402 </p> |
|
2403 </li> |
|
2404 </ul> |
|
2405 </div> |
|
2406 <p id="ch11Section_5_8c"> |
|
2407 <span class="emphasis"> |
|
2408 <em> |
|
2409 -R |
|
2410 </em> |
|
2411 </span> |
|
2412 (recursive) copy directories (recursively copying all the contents) |
|
2413 </p> |
|
2414 </div> |
|
2415 <div class="section" title="5.1.3.Examples"> |
|
2416 <div class="titlepage"> |
|
2417 <div> |
|
2418 <div> |
|
2419 <h4 class="title"> |
|
2420 <a name="id2946974"> |
|
2421 </a> |
|
2422 5.1.3.Examples |
|
2423 </h4> |
|
2424 </div> |
|
2425 </div> |
|
2426 </div> |
|
2427 <p id="ch11Section_5_8d"> |
|
2428 To make a copy of a file in the current directory, enter: |
|
2429 </p> |
|
2430 <pre class="programlisting"> |
|
2431 $ cp prog.c prog.bak |
|
2432 </pre> |
|
2433 <p id="ch11Section_5_8e"> |
|
2434 This copies prog.c to prog.bak. If the prog.bak file does not already exist, the cp command creates it. If it does exist, the cp command replaces it with a copy of the prog.c file. |
|
2435 </p> |
|
2436 <p id="ch11Section_5_8f"> |
|
2437 To copy a file in your current directory into another directory, enter: |
|
2438 </p> |
|
2439 <pre class="programlisting"> |
|
2440 $ cp zaphod /home/books/hhgg |
|
2441 </pre> |
|
2442 <p id="ch11Section_5_90"> |
|
2443 This copies the jones file to /home/books/hhgg/zaphod. |
|
2444 </p> |
|
2445 <p id="ch11Section_5_91"> |
|
2446 To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter: |
|
2447 </p> |
|
2448 <pre class="programlisting"> |
|
2449 $ cp -p martin_luther_king martin_luther_king.jr |
|
2450 </pre> |
|
2451 <p id="ch11Section_5_92"> |
|
2452 This copies the |
|
2453 <span class="emphasis"> |
|
2454 <em> |
|
2455 martin_luther_king |
|
2456 </em> |
|
2457 </span> |
|
2458 file to the |
|
2459 <span class="emphasis"> |
|
2460 <em> |
|
2461 martin_luther_king.jr |
|
2462 </em> |
|
2463 </span> |
|
2464 file. Instead of creating the file with the current date and time stamp, the system gives the |
|
2465 <span class="emphasis"> |
|
2466 <em> |
|
2467 martin_luther_king.jr |
|
2468 </em> |
|
2469 </span> |
|
2470 file the same date and time as the |
|
2471 <span class="emphasis"> |
|
2472 <em> |
|
2473 martin_luther_king |
|
2474 </em> |
|
2475 </span> |
|
2476 file. The |
|
2477 <span class="emphasis"> |
|
2478 <em> |
|
2479 martin_luther_king.jr |
|
2480 </em> |
|
2481 </span> |
|
2482 file also inherits the |
|
2483 <span class="emphasis"> |
|
2484 <em> |
|
2485 martin_luther_king |
|
2486 </em> |
|
2487 </span> |
|
2488 file's access control protection. |
|
2489 </p> |
|
2490 <p id="ch11Section_5_93"> |
|
2491 To copy all the files in a directory to a new directory, enter: |
|
2492 </p> |
|
2493 <pre class="programlisting"> |
|
2494 $ cp /home/galactica/clients/* /home/hhgg/customers |
|
2495 </pre> |
|
2496 <p id="ch11Section_5_94"> |
|
2497 This copies only the files in the clients directory to the customers directory. |
|
2498 </p> |
|
2499 <p id="ch11Section_5_95"> |
|
2500 To copy a directory, including all its files and subdirectories, to another directory, enter: |
|
2501 </p> |
|
2502 <p id="ch11Section_5_96"> |
|
2503 $ cp -R /home/hhgg/clients /home/hhgg/customers |
|
2504 </p> |
|
2505 <p id="ch11Section_5_97"> |
|
2506 This copies the clients directory, including all its files, subdirectories, and the files in those subdirectories, to the customers/clients directory. |
|
2507 </p> |
|
2508 <p id="ch11Section_5_98"> |
|
2509 To copy a specific set of files of any extension to another directory, enter: |
|
2510 </p> |
|
2511 <pre class="programlisting"> |
|
2512 $ cp zaphod arthur ford /home/hhgg/clients |
|
2513 </pre> |
|
2514 <p id="ch11Section_5_99"> |
|
2515 This copies the |
|
2516 <span class="emphasis"> |
|
2517 <em> |
|
2518 zaphod |
|
2519 </em> |
|
2520 </span> |
|
2521 , |
|
2522 <span class="emphasis"> |
|
2523 <em> |
|
2524 arthur |
|
2525 </em> |
|
2526 </span> |
|
2527 , and |
|
2528 <span class="emphasis"> |
|
2529 <em> |
|
2530 ford |
|
2531 </em> |
|
2532 </span> |
|
2533 files in your current working directory to the /home/hhgg/clients directory. |
|
2534 </p> |
|
2535 <p id="ch11Section_5_9a"> |
|
2536 To use pattern-matching characters to copy files, enter: |
|
2537 </p> |
|
2538 <pre class="programlisting"> |
|
2539 $ cp programs/*.py . |
|
2540 </pre> |
|
2541 <p id="ch11Section_5_9b"> |
|
2542 This copies the files in the programs directory that end with |
|
2543 <span class="emphasis"> |
|
2544 <em> |
|
2545 .py |
|
2546 </em> |
|
2547 </span> |
|
2548 to the current directory, signified by the single "." (dot). You must type a space between the |
|
2549 <span class="emphasis"> |
|
2550 <em> |
|
2551 py |
|
2552 </em> |
|
2553 </span> |
|
2554 and the final dot. |
|
2555 </p> |
|
2556 </div> |
|
2557 </div> |
|
2558 <div class="section" title="5.2.mv"> |
|
2559 <div class="titlepage"> |
|
2560 <div> |
|
2561 <div> |
|
2562 <h3 class="title"> |
|
2563 <a name="id2947135"> |
|
2564 </a> |
|
2565 5.2.mv |
|
2566 </h3> |
|
2567 </div> |
|
2568 </div> |
|
2569 </div> |
|
2570 <p id="ch11Section_5_9c"> |
|
2571 <span class="emphasis"> |
|
2572 <em> |
|
2573 mv |
|
2574 </em> |
|
2575 </span> |
|
2576 (short for move) is a Unix command that moves one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. If possible (i.e. when the original and new files are on the same file system), |
|
2577 <span class="emphasis"> |
|
2578 <em> |
|
2579 mv |
|
2580 </em> |
|
2581 </span> |
|
2582 will rename the file instead. Write permission is required on all directories being modified. |
|
2583 </p> |
|
2584 <div class="section" title="5.2.1.Conflicting existing file"> |
|
2585 <div class="titlepage"> |
|
2586 <div> |
|
2587 <div> |
|
2588 <h4 class="title"> |
|
2589 <a name="id2947159"> |
|
2590 </a> |
|
2591 5.2.1.Conflicting existing file |
|
2592 </h4> |
|
2593 </div> |
|
2594 </div> |
|
2595 </div> |
|
2596 <p id="ch11Section_5_9d"> |
|
2597 In all cases, when a file is moved to have the name of an existing file (in the same directory), the existing file is deleted. If the existing file is not writable but is in a directory that is writable, then the mv command asks for confirmation if possible (i.e. if run from a terminal) before proceeding, unless the -f (force) option is used. |
|
2598 </p> |
|
2599 </div> |
|
2600 <div class="section" title="5.2.2.Differences with copy and delete"> |
|
2601 <div class="titlepage"> |
|
2602 <div> |
|
2603 <div> |
|
2604 <h4 class="title"> |
|
2605 <a name="id2947178"> |
|
2606 </a> |
|
2607 5.2.2.Differences with copy and delete |
|
2608 </h4> |
|
2609 </div> |
|
2610 </div> |
|
2611 </div> |
|
2612 <p id="ch11Section_5_9e"> |
|
2613 Note that, usually, when moving files within the same volume, moving (and/or renaming) is not the same as simply copying and then deleting the original. When moving a file, the link is simply removed from the old parent directory and added to the new parent directory. However, the file itself is untouched (i.e. it has the same inodes and resides at the same place on the disk). For example, you cannot copy a file you cannot read, but you can move (and/or rename) it (provided you have write permission to its old and new parent directories). Also, suppose there is a non-empty directory you do not have write permission to. You cannot delete this directory (since you cannot delete its contents); but you can move (and/or rename) it. Also, since moving between filenames on a single volume does not involve copying, it is faster and does not place strain of lots of reads and writes on the disk. Moving files across different volumes, however, does necessitate copying and deleting. |
|
2614 </p> |
|
2615 </div> |
|
2616 <div class="section" title="5.2.3.Examples"> |
|
2617 <div class="titlepage"> |
|
2618 <div> |
|
2619 <div> |
|
2620 <h4 class="title"> |
|
2621 <a name="id2944863"> |
|
2622 </a> |
|
2623 5.2.3.Examples |
|
2624 </h4> |
|
2625 </div> |
|
2626 </div> |
|
2627 </div> |
|
2628 <pre class="programlisting"> |
|
2629 $ mv myfile mynewfilename renames a file |
|
2630 $ mv myfile otherfilename renames a file and deletes the existing file "myfile" |
|
2631 $ mv myfile /myfile moves 'myfile' from the current directory to the root directory |
|
2632 $ mv myfile dir/myfile moves 'myfile' to 'dir/myfile' relative to the current directory |
|
2633 $ mv myfile dir same as the previous command (the filename is implied to be the same) |
|
2634 $ mv myfile dir/myfile2 moves 'myfile' to dir and renames it to 'myfile2' |
|
2635 $ mv foo bar baz dir moves multiple files to directory dir |
|
2636 $ mv --help shows a very concise help about the syntax of the command |
|
2637 $ man mv prints an extensive user manual for 'mv' in the terminal |
|
2638 </pre> |
|
2639 <p id="ch11Section_5_9f"> |
|
2640 In all cases, the file or files being moved or renamed can be a directory. |
|
2641 </p> |
|
2642 <p id="ch11Section_5_a0"> |
|
2643 Note that when the command is called with two arguments (as |
|
2644 <span class="emphasis"> |
|
2645 <em> |
|
2646 mv name1 name2 |
|
2647 </em> |
|
2648 </span> |
|
2649 or |
|
2650 <span class="emphasis"> |
|
2651 <em> |
|
2652 mv name1 /dir/name2 |
|
2653 </em> |
|
2654 </span> |
|
2655 ), it can have three different effects, depending on whether |
|
2656 <span class="emphasis"> |
|
2657 <em> |
|
2658 name2 |
|
2659 </em> |
|
2660 </span> |
|
2661 does not exist, is an existing file, or is an existing directory. If the user intends to refer to an existing directory, |
|
2662 <span class="emphasis"> |
|
2663 <em> |
|
2664 /. |
|
2665 </em> |
|
2666 </span> |
|
2667 (or in some Unix versions |
|
2668 <span class="emphasis"> |
|
2669 <em> |
|
2670 / |
|
2671 </em> |
|
2672 </span> |
|
2673 is sufficient) may be appended to the name to force the system to check this. To move a file to a new directory, the directory must be created first. |
|
2674 </p> |
|
2675 </div> |
|
2676 </div> |
|
2677 <div class="section" title="5.3.rm"> |
|
2678 <div class="titlepage"> |
|
2679 <div> |
|
2680 <div> |
|
2681 <h3 class="title"> |
|
2682 <a name="id2944921"> |
|
2683 </a> |
|
2684 5.3.rm |
|
2685 </h3> |
|
2686 </div> |
|
2687 </div> |
|
2688 </div> |
|
2689 <p id="ch11Section_5_a1"> |
|
2690 <span class="emphasis"> |
|
2691 <em> |
|
2692 rm |
|
2693 </em> |
|
2694 </span> |
|
2695 (short for "remove") is one of several basic Unix command lines that operates on files. It is used to delete files from a filesystem. The data is not actually destroyed. Only the index listing where the file is stored is destroyed, and the storage is made available for reuse. There are undelete utilities that will attempt to reconstruct the index and can bring the file back if the parts were not reused. |
|
2696 </p> |
|
2697 <p id="ch11Section_5_a2"> |
|
2698 Here's example to remove a file named "foo" from a directory, here shown with the -i option: |
|
2699 </p> |
|
2700 <pre class="programlisting"> |
|
2701 $ rm -i foo |
|
2702 remove foo? y |
|
2703 </pre> |
|
2704 <div class="section" title="5.3.1.Options"> |
|
2705 <div class="titlepage"> |
|
2706 <div> |
|
2707 <div> |
|
2708 <h4 class="title"> |
|
2709 <a name="id2944951"> |
|
2710 </a> |
|
2711 5.3.1.Options |
|
2712 </h4> |
|
2713 </div> |
|
2714 </div> |
|
2715 </div> |
|
2716 <p id="ch11Section_5_a3"> |
|
2717 Common options that rm accepts include: |
|
2718 </p> |
|
2719 <div class="itemizedlist"> |
|
2720 <ul class="itemizedlist" type="*"> |
|
2721 <li class="listitem" style="list-style-type: *"> |
|
2722 <p id="ch11Section_5_a4"> |
|
2723 <span class="emphasis"> |
|
2724 <em> |
|
2725 -r |
|
2726 </em> |
|
2727 </span> |
|
2728 , which removes directories, removing the contents recursively beforehand (so as not to leave files without a directory to reside in) ("recursive") |
|
2729 </p> |
|
2730 </li> |
|
2731 <li class="listitem" style="list-style-type: *"> |
|
2732 <p id="ch11Section_5_a5"> |
|
2733 <span class="emphasis"> |
|
2734 <em> |
|
2735 -i |
|
2736 </em> |
|
2737 </span> |
|
2738 , which asks for every deletion to be confirmed ("interactive") |
|
2739 </p> |
|
2740 </li> |
|
2741 <li class="listitem" style="list-style-type: *"> |
|
2742 <p id="ch11Section_5_a6"> |
|
2743 <span class="emphasis"> |
|
2744 <em> |
|
2745 -f |
|
2746 </em> |
|
2747 </span> |
|
2748 , which ignores non-existent files and overrides any confirmation prompts ("force") |
|
2749 </p> |
|
2750 </li> |
|
2751 <li class="listitem" style="list-style-type: *"> |
|
2752 <p id="ch11Section_5_a7"> |
|
2753 <span class="emphasis"> |
|
2754 <em> |
|
2755 -v |
|
2756 </em> |
|
2757 </span> |
|
2758 , which shows what is being removed as it happens ("verbose") |
|
2759 </p> |
|
2760 </li> |
|
2761 </ul> |
|
2762 </div> |
|
2763 <p id="ch11Section_5_a8"> |
|
2764 <span class="emphasis"> |
|
2765 <em> |
|
2766 rm |
|
2767 </em> |
|
2768 </span> |
|
2769 is often aliased to "rm -i" so as to avoid accidental deletion of files. If a user still wishes to delete a large number of files without confirmation, they can manually cancel out the -i argument by adding the -f option (as the option specified later on the expanded command line "rm -i -f" takes precedence). |
|
2770 </p> |
|
2771 <p id="ch11Section_5_a9"> |
|
2772 <span class="emphasis"> |
|
2773 <em> |
|
2774 rm -rf |
|
2775 </em> |
|
2776 </span> |
|
2777 (variously, rm -rf /, rm -rf *, and others) is frequently used in jokes and anecdotes about Unix disasters. The rm -rf variant of the command, if run by a superuser on the root directory, would cause the contents of every writable mounted filesystem on the computer to be deleted. |
|
2778 </p> |
|
2779 <p id="ch11Section_5_aa"> |
|
2780 <span class="emphasis"> |
|
2781 <em> |
|
2782 rm |
|
2783 </em> |
|
2784 </span> |
|
2785 is often used in conjunction with xargs to supply a list of files to delete: |
|
2786 </p> |
|
2787 <pre class="programlisting"> |
|
2788 xargs rm < filelist |
|
2789 </pre> |
|
2790 <p id="ch11Section_5_ab"> |
|
2791 When |
|
2792 <span class="emphasis"> |
|
2793 <em> |
|
2794 rm |
|
2795 </em> |
|
2796 </span> |
|
2797 is used on a symbolic link, it deletes the link, but does not affect the target of the link. |
|
2798 </p> |
|
2799 </div> |
|
2800 <div class="section" title="5.3.2.Permissions"> |
|
2801 <div class="titlepage"> |
|
2802 <div> |
|
2803 <div> |
|
2804 <h4 class="title"> |
|
2805 <a name="id2947530"> |
|
2806 </a> |
|
2807 5.3.2.Permissions |
|
2808 </h4> |
|
2809 </div> |
|
2810 </div> |
|
2811 </div> |
|
2812 <p id="ch11Section_5_ac"> |
|
2813 Usually, on most filesystems, deleting a file requires write permission on the parent directory (and execute permission, in order to enter the directory in the first place). (Note that, confusingly for beginners, permissions on the file itself are irrelevant. However, GNU rm asks for confirmation if a write-protected file is to be deleted, unless the -f option is used.) |
|
2814 </p> |
|
2815 <p id="ch11Section_5_ad"> |
|
2816 To delete a directory (with rm -r), one must delete all of its contents recursively. This requires that one must have read and write and execute permission to that directory (if it's not empty) and all non-empty subdirectories recursively (if there are any). The read permissions are needed to list the contents of the directory in order to delete them. This sometimes leads to an odd situation where a non-empty directory cannot be deleted because one doesn't have write permission to it and so cannot delete its contents; but if the same directory were empty, one would be able to delete it. |
|
2817 </p> |
|
2818 <p id="ch11Section_5_ae"> |
|
2819 If a file resides in a directory with the sticky bit set, then deleting the file requires one to be the owner of the file. |
|
2820 </p> |
|
2821 </div> |
|
2822 </div> |
|
2823 </div> |
|
2824 <div class="section" title="6.Command Line Arguments"> |
|
2825 <div class="titlepage"> |
|
2826 <div> |
|
2827 <div> |
|
2828 <h2 class="title" style="clear: both"> |
|
2829 <a name="id2947569"> |
|
2830 </a> |
|
2831 6.Command Line Arguments |
|
2832 </h2> |
|
2833 </div> |
|
2834 </div> |
|
2835 </div> |
|
2836 <p id="ch11Section_5_af"> |
|
2837 In computer command line interfaces, a command line argument is an argument sent to a program being called. In general, a program can take any number of command line arguments, which may be necessary for the program to run, or may even be ignored, depending on the function of that program. |
|
2838 </p> |
|
2839 <p id="ch11Section_5_b0"> |
|
2840 For example, in Unix and Unix-like environments, an example of a command-line argument is: |
|
2841 </p> |
|
2842 <pre class="programlisting"> |
|
2843 rm file.s |
|
2844 </pre> |
|
2845 <p id="ch11Section_5_b1"> |
|
2846 "file.s" is a command line argument which tells the program rm to remove the file "file.s". |
|
2847 </p> |
|
2848 <p id="ch11Section_5_b2"> |
|
2849 Programming languages such as C, C++ and Java allow a program to interpret the command line arguments by handling them as string parameters in the main function. |
|
2850 </p> |
|
2851 <p id="ch11Section_5_b3"> |
|
2852 A command line option or simply |
|
2853 <span class="emphasis"> |
|
2854 <em> |
|
2855 option |
|
2856 </em> |
|
2857 </span> |
|
2858 (also known as a command line parameter, flag, or a switch) is an indication by a user that a computer program should change its default output. |
|
2859 </p> |
|
2860 <p id="ch11Section_5_b4"> |
|
2861 Long options are introduced via "--", and are typically whole words. For example, |
|
2862 <span class="emphasis"> |
|
2863 <em> |
|
2864 ls --long --classify --all |
|
2865 </em> |
|
2866 </span> |
|
2867 . Arguments to long options are provided with "=", as |
|
2868 <span class="emphasis"> |
|
2869 <em> |
|
2870 ls --block-size=1024 |
|
2871 </em> |
|
2872 </span> |
|
2873 . Some Unix programs use long options with single dashes, for example MPlayer as in |
|
2874 <span class="emphasis"> |
|
2875 <em> |
|
2876 mplayer -nosound |
|
2877 </em> |
|
2878 </span> |
|
2879 . |
|
2880 </p> |
|
2881 <p id="ch11Section_5_b5"> |
|
2882 Linux also uses "--" to terminate option lists. For example, an attempt to delete a file called |
|
2883 <span class="emphasis"> |
|
2884 <em> |
|
2885 -file1 |
|
2886 </em> |
|
2887 </span> |
|
2888 by using |
|
2889 <span class="emphasis"> |
|
2890 <em> |
|
2891 rm -file1 |
|
2892 </em> |
|
2893 </span> |
|
2894 may produce an error, since rm may interpret |
|
2895 <span class="emphasis"> |
|
2896 <em> |
|
2897 -file1 |
|
2898 </em> |
|
2899 </span> |
|
2900 as a command line switch. Using |
|
2901 <span class="emphasis"> |
|
2902 <em> |
|
2903 rm -- -file1 |
|
2904 </em> |
|
2905 </span> |
|
2906 removes ambiguity. |
|
2907 </p> |
|
2908 </div> |
|
2909 <div class="section" title="7.Basic Text Processing"> |
|
2910 <div class="titlepage"> |
|
2911 <div> |
|
2912 <div> |
|
2913 <h2 class="title" style="clear: both"> |
|
2914 <a name="id2947661"> |
|
2915 </a> |
|
2916 7.Basic Text Processing |
|
2917 </h2> |
|
2918 </div> |
|
2919 </div> |
|
2920 </div> |
|
2921 <div class="section" title="7.1.head"> |
|
2922 <div class="titlepage"> |
|
2923 <div> |
|
2924 <div> |
|
2925 <h3 class="title"> |
|
2926 <a name="id2947670"> |
|
2927 </a> |
|
2928 7.1.head |
|
2929 </h3> |
|
2930 </div> |
|
2931 </div> |
|
2932 </div> |
|
2933 <p id="ch11Section_5_b6"> |
|
2934 <span class="emphasis"> |
|
2935 <em> |
|
2936 head |
|
2937 </em> |
|
2938 </span> |
|
2939 is a program on Unix and Unix-like systems used to display the first few lines of a text file or piped data. The command syntax is: |
|
2940 </p> |
|
2941 <pre class="programlisting"> |
|
2942 $ head [options] <file_name> |
|
2943 </pre> |
|
2944 <p id="ch11Section_5_b7"> |
|
2945 By default, |
|
2946 <span class="emphasis"> |
|
2947 <em> |
|
2948 head |
|
2949 </em> |
|
2950 </span> |
|
2951 will print the first 10 lines of its input to the standard output. The number of lines printed may be changed with a command line option. The following example shows the first 20 lines of filename: |
|
2952 </p> |
|
2953 <pre class="programlisting"> |
|
2954 $ head -n 20 filename |
|
2955 </pre> |
|
2956 <p id="ch11Section_5_b8"> |
|
2957 This displays the first 5 lines of all files starting with |
|
2958 <span class="emphasis"> |
|
2959 <em> |
|
2960 foo |
|
2961 </em> |
|
2962 </span> |
|
2963 : |
|
2964 </p> |
|
2965 <pre class="programlisting"> |
|
2966 $ head -n 5 foo* |
|
2967 </pre> |
|
2968 <p id="ch11Section_5_b9"> |
|
2969 Some versions omit the n and just let you say -5. |
|
2970 </p> |
|
2971 <div class="section" title="7.1.1.Flags"> |
|
2972 <div class="titlepage"> |
|
2973 <div> |
|
2974 <div> |
|
2975 <h4 class="title"> |
|
2976 <a name="id2947721"> |
|
2977 </a> |
|
2978 7.1.1.Flags |
|
2979 </h4> |
|
2980 </div> |
|
2981 </div> |
|
2982 </div> |
|
2983 <pre class="programlisting"> |
|
2984 -c <x number of bytes> Copy first x number of bytes. |
|
2985 </pre> |
|
2986 <p id="ch11Section_5_ba"> |
|
2987 Other options: |
|
2988 <span class="emphasis"> |
|
2989 <em> |
|
2990 sed |
|
2991 </em> |
|
2992 </span> |
|
2993 </p> |
|
2994 <p id="ch11Section_5_bb"> |
|
2995 Many early versions of Unix did not have this command, and so documentation and books had |
|
2996 <span class="emphasis"> |
|
2997 <em> |
|
2998 sed |
|
2999 </em> |
|
3000 </span> |
|
3001 do this job: |
|
3002 </p> |
|
3003 <pre class="programlisting"> |
|
3004 sed 5q foo |
|
3005 </pre> |
|
3006 <p id="ch11Section_5_bc"> |
|
3007 This says to print every line (implicit), and quit after the fifth. |
|
3008 </p> |
|
3009 </div> |
|
3010 </div> |
|
3011 <div class="section" title="7.2.tail"> |
|
3012 <div class="titlepage"> |
|
3013 <div> |
|
3014 <div> |
|
3015 <h3 class="title"> |
|
3016 <a name="id2947756"> |
|
3017 </a> |
|
3018 7.2.tail |
|
3019 </h3> |
|
3020 </div> |
|
3021 </div> |
|
3022 </div> |
|
3023 <p id="ch11Section_5_bd"> |
|
3024 <span class="emphasis"> |
|
3025 <em> |
|
3026 tail |
|
3027 </em> |
|
3028 </span> |
|
3029 is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data. |
|
3030 </p> |
|
3031 <p id="ch11Section_5_be"> |
|
3032 The command-syntax is: |
|
3033 </p> |
|
3034 <pre class="programlisting"> |
|
3035 $ tail [options] <file_name> |
|
3036 </pre> |
|
3037 <p id="ch11Section_5_bf"> |
|
3038 By default, |
|
3039 <span class="emphasis"> |
|
3040 <em> |
|
3041 tail |
|
3042 </em> |
|
3043 </span> |
|
3044 will print the last 10 lines of its input to the standard output. With command line options the number of lines printed and the printing units (lines, blocks or bytes) may be changed. The following example shows the last 20 lines of filename: |
|
3045 </p> |
|
3046 <pre class="programlisting"> |
|
3047 $ tail -n 20 filename |
|
3048 </pre> |
|
3049 <p id="ch11Section_5_c0"> |
|
3050 This example shows the last 15 bytes of all files starting with |
|
3051 <span class="emphasis"> |
|
3052 <em> |
|
3053 foo |
|
3054 </em> |
|
3055 </span> |
|
3056 : |
|
3057 </p> |
|
3058 <pre class="programlisting"> |
|
3059 $ tail -c 15 foo* |
|
3060 </pre> |
|
3061 <p id="ch11Section_5_c1"> |
|
3062 This example shows all lines of filename from the second line onwards: |
|
3063 </p> |
|
3064 <pre class="programlisting"> |
|
3065 $ tail -n +2 filename |
|
3066 </pre> |
|
3067 <p id="ch11Section_5_c2"> |
|
3068 Using an older syntax (still used in Sun Solaris as the -n option is not supported), the last 20 lines and the last 50 bytes of filename can be shown with the following command: |
|
3069 </p> |
|
3070 <pre class="programlisting"> |
|
3071 $ tail -20 filename |
|
3072 $ tail -50c filename |
|
3073 </pre> |
|
3074 <p id="ch11Section_5_c3"> |
|
3075 However this syntax is now obsolete and does not conform with the POSIX 1003.1-2001 standard. Even if still supported in current versions, when used with other options (like -f, see below), these switches could not work at all. |
|
3076 </p> |
|
3077 <div class="section" title="7.2.1.File monitoring"> |
|
3078 <div class="titlepage"> |
|
3079 <div> |
|
3080 <div> |
|
3081 <h4 class="title"> |
|
3082 <a name="id2947834"> |
|
3083 </a> |
|
3084 7.2.1.File monitoring |
|
3085 </h4> |
|
3086 </div> |
|
3087 </div> |
|
3088 </div> |
|
3089 <p id="ch11Section_5_c4"> |
|
3090 <span class="emphasis"> |
|
3091 <em> |
|
3092 tail |
|
3093 </em> |
|
3094 </span> |
|
3095 has a special command line option |
|
3096 <span class="emphasis"> |
|
3097 <em> |
|
3098 -f |
|
3099 </em> |
|
3100 </span> |
|
3101 (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display. This is particularly useful for monitoring log files. The following command will display the last 10 lines of messages and append new lines to the display as new lines are added to messages: |
|
3102 </p> |
|
3103 <pre class="programlisting"> |
|
3104 $ tail -f /var/adm/messages |
|
3105 </pre> |
|
3106 <p id="ch11Section_5_c5"> |
|
3107 To interrupt tail while it is monitoring, break-in with |
|
3108 <span class="emphasis"> |
|
3109 <em> |
|
3110 Ctrl+C |
|
3111 </em> |
|
3112 </span> |
|
3113 . This command can be run "in the background" with &, see job control. |
|
3114 </p> |
|
3115 <p id="ch11Section_5_c6"> |
|
3116 If you have a command's result to monitor, you can use the |
|
3117 <span class="emphasis"> |
|
3118 <em> |
|
3119 watch |
|
3120 </em> |
|
3121 </span> |
|
3122 command. |
|
3123 </p> |
|
3124 </div> |
|
3125 </div> |
|
3126 <div class="section" title="7.3.cut"> |
|
3127 <div class="titlepage"> |
|
3128 <div> |
|
3129 <div> |
|
3130 <h3 class="title"> |
|
3131 <a name="id2947883"> |
|
3132 </a> |
|
3133 7.3.cut |
|
3134 </h3> |
|
3135 </div> |
|
3136 </div> |
|
3137 </div> |
|
3138 <p id="ch11Section_5_c7"> |
|
3139 In computing, |
|
3140 <span class="emphasis"> |
|
3141 <em> |
|
3142 cut |
|
3143 </em> |
|
3144 </span> |
|
3145 is a Unix command line utility which is used to extract sections from each line of input usually from a file. |
|
3146 </p> |
|
3147 <p id="ch11Section_5_c8"> |
|
3148 Extraction of line segments can typically be done by |
|
3149 <span class="emphasis"> |
|
3150 <em> |
|
3151 bytes (-b), characters (-c) |
|
3152 </em> |
|
3153 </span> |
|
3154 , or |
|
3155 <span class="emphasis"> |
|
3156 <em> |
|
3157 fields (-f) |
|
3158 </em> |
|
3159 </span> |
|
3160 separated by a |
|
3161 <span class="emphasis"> |
|
3162 <em> |
|
3163 delimiter (-d the tab character by default) |
|
3164 </em> |
|
3165 </span> |
|
3166 . A range must be provided in each case which consists of one of N, N-M, N- (N to the end of the line), or -M (beginning of the line to M), where N and M are counted from 1 (there is no zeroth value). Since version 6, an error is thrown if you include a zeroth value. Prior to this the value was ignored and assumed to be 1. |
|
3167 </p> |
|
3168 <p id="ch11Section_5_c9"> |
|
3169 Assuming a file named file containing the lines: |
|
3170 </p> |
|
3171 <pre class="programlisting"> |
|
3172 foo:bar:baz:qux:quux |
|
3173 one:two:three:four:five:six:seven |
|
3174 alpha:beta:gamma:delta:epsilon:zeta:eta:teta:iota:kappa:lambda:mu |
|
3175 </pre> |
|
3176 <p id="ch11Section_5_ca"> |
|
3177 To output the fourth through tenth characters of each line: |
|
3178 </p> |
|
3179 <pre class="programlisting"> |
|
3180 $ cut -c 4-10 file |
|
3181 </pre> |
|
3182 <p id="ch11Section_5_cb"> |
|
3183 This gives the output: |
|
3184 </p> |
|
3185 <pre class="programlisting"> |
|
3186 :bar:ba |
|
3187 :two:th |
|
3188 ha:beta |
|
3189 </pre> |
|
3190 <p id="ch11Section_5_cc"> |
|
3191 To output the fifth field through the end of the line of each line using the colon character as the field delimiter: |
|
3192 </p> |
|
3193 <pre class="programlisting"> |
|
3194 $ cut -d : -f 5- file |
|
3195 </pre> |
|
3196 <p id="ch11Section_5_cd"> |
|
3197 This gives the output: |
|
3198 </p> |
|
3199 <pre class="programlisting"> |
|
3200 quux |
|
3201 five:six:seven |
|
3202 epsilon:zeta:eta:teta:iota:kappa:lambda:mu |
|
3203 </pre> |
|
3204 </div> |
|
3205 <div class="section" title="7.4.paste"> |
|
3206 <div class="titlepage"> |
|
3207 <div> |
|
3208 <div> |
|
3209 <h3 class="title"> |
|
3210 <a name="id2947969"> |
|
3211 </a> |
|
3212 7.4.paste |
|
3213 </h3> |
|
3214 </div> |
|
3215 </div> |
|
3216 </div> |
|
3217 <p id="ch11Section_5_ce"> |
|
3218 <span class="emphasis"> |
|
3219 <em> |
|
3220 paste |
|
3221 </em> |
|
3222 </span> |
|
3223 is a Unix command line utility which is used to join files horizontally (parallel merging) by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output. It is effectively the horizontal equivalent to the utility |
|
3224 <span class="emphasis"> |
|
3225 <em> |
|
3226 cat |
|
3227 </em> |
|
3228 </span> |
|
3229 command which operates on the vertical plane of two or more files. |
|
3230 </p> |
|
3231 <p id="ch11Section_5_cf"> |
|
3232 To paste several columns of data together into the file |
|
3233 <span class="emphasis"> |
|
3234 <em> |
|
3235 www |
|
3236 </em> |
|
3237 </span> |
|
3238 from files |
|
3239 <span class="emphasis"> |
|
3240 <em> |
|
3241 who |
|
3242 </em> |
|
3243 </span> |
|
3244 , |
|
3245 <span class="emphasis"> |
|
3246 <em> |
|
3247 where |
|
3248 </em> |
|
3249 </span> |
|
3250 , and |
|
3251 <span class="emphasis"> |
|
3252 <em> |
|
3253 when |
|
3254 </em> |
|
3255 </span> |
|
3256 : |
|
3257 </p> |
|
3258 <pre class="programlisting"> |
|
3259 $ paste who where when > www |
|
3260 </pre> |
|
3261 <p id="ch11Section_5_d0"> |
|
3262 If the files contain: |
|
3263 </p> |
|
3264 <table summary="paste" border="1"> |
|
3265 <colgroup> |
|
3266 <col width="11"> |
|
3267 </col> |
|
3268 <col width="12"> |
|
3269 </col> |
|
3270 <col width="12"> |
|
3271 </col> |
|
3272 </colgroup> |
|
3273 </table> |
|
3274 <p id="ch11Section_5_dd"> |
|
3275 This creates the file named |
|
3276 <span class="emphasis"> |
|
3277 <em> |
|
3278 www |
|
3279 </em> |
|
3280 </span> |
|
3281 containing: |
|
3282 </p> |
|
3283 <pre class="programlisting"> |
|
3284 Batman GothamCity January 3 |
|
3285 Trillian Andromeda February 4 |
|
3286 Jeeves London March 19 |
|
3287 </pre> |
|
3288 </div> |
|
3289 </div> |
|
3290 <div class="section" title="8.Shell Meta Characters"> |
|
3291 <div class="titlepage"> |
|
3292 <div> |
|
3293 <div> |
|
3294 <h2 class="title" style="clear: both"> |
|
3295 <a name="id2948101"> |
|
3296 </a> |
|
3297 8.Shell Meta Characters |
|
3298 </h2> |
|
3299 </div> |
|
3300 </div> |
|
3301 </div> |
|
3302 <p id="ch11Section_5_de"> |
|
3303 Unix recognizes certain special characters, called "meta characters," as command directives. The shell meta characters are recognized anywhere they appear in the command line, even if they are not surrounded by blank space. For that reason, it is safest to only use the characters A-Z, a-z, 0-9, and the period, dash, and underscore characters when naming files and directories on Unix. If your file or directory has a shell meta character in the name, you will find it difficult to use the name in a shell command. |
|
3304 </p> |
|
3305 <p id="ch11Section_5_df"> |
|
3306 The shell meta characters include: |
|
3307 </p> |
|
3308 <p id="ch11Section_5_e0"> |
|
3309 / < > ! $ % ^ & * | { } [ ] " ' ` ~ ; |
|
3310 </p> |
|
3311 <p id="ch11Section_5_e1"> |
|
3312 Different shells may differ in the meta characters recognized. |
|
3313 </p> |
|
3314 <p id="ch11Section_5_e2"> |
|
3315 As an example, |
|
3316 </p> |
|
3317 <pre class="programlisting"> |
|
3318 $ ls file.* |
|
3319 </pre> |
|
3320 <p id="ch11Section_5_e3"> |
|
3321 run on a directory containing the files file, file.c, file.lst, and myfile would list the files file.c and file.lst. However,: |
|
3322 </p> |
|
3323 <pre class="programlisting"> |
|
3324 $ ls file.? |
|
3325 </pre> |
|
3326 <p id="ch11Section_5_e4"> |
|
3327 run on the same directory would only list file.c because the ? only matches one character, no more, no less. This can save you a great deal of typing time. For example, if there is a file called california_cornish_hens_with_wild_rice and no other files whose names begin with 'c', you could view the file without typing the whole name by typing this: |
|
3328 </p> |
|
3329 <pre class="programlisting"> |
|
3330 $ more c* |
|
3331 </pre> |
|
3332 <p id="ch11Section_5_e5"> |
|
3333 because the c* matches that long file name. |
|
3334 </p> |
|
3335 <p id="ch11Section_5_e6"> |
|
3336 Filenames containing metacharacters can pose many problems and should never be intentionally created. If you do find that you've created a file with metacharacters, and you would like to remove it, you have three options. You may use wildcards to match metacharacter, use the to directly enter the filename, or put the command in double quotes (except in the case of double quotes within the file name, these must be captured with one of the first two methods). For example, deleting a file named "``*`|more"` can be accomplished with: |
|
3337 </p> |
|
3338 <pre class="programlisting"> |
|
3339 $ rm ??more |
|
3340 </pre> |
|
3341 <p id="ch11Section_5_e7"> |
|
3342 or: |
|
3343 </p> |
|
3344 <pre class="programlisting"> |
|
3345 $ rm $\backslash$*$\backslash$|more |
|
3346 </pre> |
|
3347 <p id="ch11Section_5_e8"> |
|
3348 or: |
|
3349 </p> |
|
3350 <pre class="programlisting"> |
|
3351 $ rm ''*|more'' |
|
3352 </pre> |
|
3353 </div> |
|
3354 <div class="section" title="9.Looking At Files"> |
|
3355 <div class="titlepage"> |
|
3356 <div> |
|
3357 <div> |
|
3358 <h2 class="title" style="clear: both"> |
|
3359 <a name="id2948205"> |
|
3360 </a> |
|
3361 9.Looking At Files |
|
3362 </h2> |
|
3363 </div> |
|
3364 </div> |
|
3365 </div> |
|
3366 <div class="section" title="9.1.cat"> |
|
3367 <div class="titlepage"> |
|
3368 <div> |
|
3369 <div> |
|
3370 <h3 class="title"> |
|
3371 <a name="id2948213"> |
|
3372 </a> |
|
3373 9.1.cat |
|
3374 </h3> |
|
3375 </div> |
|
3376 </div> |
|
3377 </div> |
|
3378 <p id="ch11Section_5_e9"> |
|
3379 The |
|
3380 <span class="emphasis"> |
|
3381 <em> |
|
3382 cat |
|
3383 </em> |
|
3384 </span> |
|
3385 command is a standard Unix program used to concatenate and display files. The name is from "catenate", a synonym of |
|
3386 <span class="emphasis"> |
|
3387 <em> |
|
3388 concatenate |
|
3389 </em> |
|
3390 </span> |
|
3391 . |
|
3392 </p> |
|
3393 <p id="ch11Section_5_ea"> |
|
3394 The Single Unix Specification specifies the behavior that the contents of each of the files given in sequence as arguments will be written to the standard output in the same sequence, and mandates one option, -u, where each byte is printed as it is read. |
|
3395 </p> |
|
3396 <p id="ch11Section_5_eb"> |
|
3397 If the filename is specified as -, then |
|
3398 <span class="emphasis"> |
|
3399 <em> |
|
3400 cat |
|
3401 </em> |
|
3402 </span> |
|
3403 will read from standard input at that point in the sequence. If no files are specified, |
|
3404 <span class="emphasis"> |
|
3405 <em> |
|
3406 cat |
|
3407 </em> |
|
3408 </span> |
|
3409 will read from standard input entered. |
|
3410 </p> |
|
3411 <div class="section" title="9.1.1.Jargon File Definition"> |
|
3412 <div class="titlepage"> |
|
3413 <div> |
|
3414 <div> |
|
3415 <h4 class="title"> |
|
3416 <a name="id2948256"> |
|
3417 </a> |
|
3418 9.1.1.Jargon File Definition |
|
3419 </h4> |
|
3420 </div> |
|
3421 </div> |
|
3422 </div> |
|
3423 <p id="ch11Section_5_ec"> |
|
3424 The Jargon File version 4.4.7 lists this as the definition of |
|
3425 <span class="emphasis"> |
|
3426 <em> |
|
3427 cat |
|
3428 </em> |
|
3429 </span> |
|
3430 : |
|
3431 </p> |
|
3432 <pre class="programlisting"> |
|
3433 1. To spew an entire file to the screen or some other output sink without |
|
3434 pause (syn. blast). |
|
3435 |
|
3436 2. By extension, to dump large amounts of data at an unprepared target or |
|
3437 with no intention of browsing it carefully. Usage: considered silly. |
|
3438 Rare outside Unix sites. See also dd, BLT. |
|
3439 |
|
3440 Among Unix fans, *cat(1)* is considered an excellent example of |
|
3441 user-interface design, because it delivers the file contents without |
|
3442 such verbosity as spacing or headers between the files, and because |
|
3443 it does not require the files to consist of lines of text, but works |
|
3444 with any sort of data. |
|
3445 |
|
3446 Among Unix critics, *cat(1)* is considered the canonical example of |
|
3447 bad user-interface design, because of its woefully unobvious name. |
|
3448 It is far more often used to blast a single file to standard output |
|
3449 than to concatenate two or more files. The name cat for the former |
|
3450 operation is just as unintuitive as, say, LISP's cdr. |
|
3451 |
|
3452 Of such oppositions are holy wars made... |
|
3453 </pre> |
|
3454 </div> |
|
3455 <div class="section" title="9.1.2.Useless Use of 'cat'"> |
|
3456 <div class="titlepage"> |
|
3457 <div> |
|
3458 <div> |
|
3459 <h4 class="title"> |
|
3460 <a name="id2948296"> |
|
3461 </a> |
|
3462 9.1.2.Useless Use of 'cat' |
|
3463 </h4> |
|
3464 </div> |
|
3465 </div> |
|
3466 </div> |
|
3467 <p id="ch11Section_5_ed"> |
|
3468 UUOC (from comp.unix.shell on Usenet) stands for Useless Use of cat. As it is observed on |
|
3469 <span class="emphasis"> |
|
3470 <em> |
|
3471 comp.unix.shell |
|
3472 </em> |
|
3473 </span> |
|
3474 , The purpose of cat is to concatenate (or 'catenate') files. If it's only one file, concatenating it with nothing at all is a waste of time, and costs you a process. |
|
3475 </p> |
|
3476 <p id="ch11Section_5_ee"> |
|
3477 Nevertheless one sees people doing: |
|
3478 </p> |
|
3479 <pre class="programlisting"> |
|
3480 $ cat file | some_command and its args ... |
|
3481 </pre> |
|
3482 <p id="ch11Section_5_ef"> |
|
3483 instead of the equivalent and cheaper: |
|
3484 </p> |
|
3485 <pre class="programlisting"> |
|
3486 <file some_command and its args ... |
|
3487 </pre> |
|
3488 <p id="ch11Section_5_f0"> |
|
3489 or (equivalently and more classically): |
|
3490 </p> |
|
3491 <pre class="programlisting"> |
|
3492 some_command and its args ... <file |
|
3493 </pre> |
|
3494 <p id="ch11Section_5_f1"> |
|
3495 Since 1995, occasional awards for UUOC have been given out. The activity of fixing instances of UUOC is sometimes called 'demoggification'. |
|
3496 </p> |
|
3497 <p id="ch11Section_5_f2"> |
|
3498 Amongst many, it is still considered safer to use |
|
3499 <span class="emphasis"> |
|
3500 <em> |
|
3501 cat |
|
3502 </em> |
|
3503 </span> |
|
3504 for such cases given that the < and > keys are next to each other in many popular keyboard mappings. While the risk might be low, the impact of using > instead of < can be high and prohibitive. |
|
3505 </p> |
|
3506 </div> |
|
3507 <div class="section" title="9.1.3.zcat"> |
|
3508 <div class="titlepage"> |
|
3509 <div> |
|
3510 <div> |
|
3511 <h4 class="title"> |
|
3512 <a name="id2948366"> |
|
3513 </a> |
|
3514 9.1.3.zcat |
|
3515 </h4> |
|
3516 </div> |
|
3517 </div> |
|
3518 </div> |
|
3519 <p id="ch11Section_5_f3"> |
|
3520 <span class="emphasis"> |
|
3521 <em> |
|
3522 zcat |
|
3523 </em> |
|
3524 </span> |
|
3525 is a Unix program similar to |
|
3526 <span class="emphasis"> |
|
3527 <em> |
|
3528 cat |
|
3529 </em> |
|
3530 </span> |
|
3531 , that decompresses individual files and concatenates them to standard output. Traditionally |
|
3532 <span class="emphasis"> |
|
3533 <em> |
|
3534 zcat |
|
3535 </em> |
|
3536 </span> |
|
3537 operated on files compressed by compress but today it is usually able to operate on |
|
3538 <span class="emphasis"> |
|
3539 <em> |
|
3540 gzip |
|
3541 </em> |
|
3542 </span> |
|
3543 or even |
|
3544 <span class="emphasis"> |
|
3545 <em> |
|
3546 bzip2 |
|
3547 </em> |
|
3548 </span> |
|
3549 archives. On such systems, it is equivalent to |
|
3550 <span class="emphasis"> |
|
3551 <em> |
|
3552 gunzip -c |
|
3553 </em> |
|
3554 </span> |
|
3555 </p> |
|
3556 </div> |
|
3557 </div> |
|
3558 <div class="section" title="9.2.more"> |
|
3559 <div class="titlepage"> |
|
3560 <div> |
|
3561 <div> |
|
3562 <h3 class="title"> |
|
3563 <a name="id2948400"> |
|
3564 </a> |
|
3565 9.2.more |
|
3566 </h3> |
|
3567 </div> |
|
3568 </div> |
|
3569 </div> |
|
3570 <p id="ch11Section_5_f4"> |
|
3571 In computing, |
|
3572 <span class="emphasis"> |
|
3573 <em> |
|
3574 more |
|
3575 </em> |
|
3576 </span> |
|
3577 is a command to view (but not modify) the contents of a text file one screen at a time (terminal pager). It is available on Unix and Unix-like systems, DOS, OS/2 and Microsoft Windows. Programs of this sort are called pagers. |
|
3578 </p> |
|
3579 <div class="section" title="9.2.1.Usage"> |
|
3580 <div class="titlepage"> |
|
3581 <div> |
|
3582 <div> |
|
3583 <h4 class="title"> |
|
3584 <a name="id2948419"> |
|
3585 </a> |
|
3586 9.2.1.Usage |
|
3587 </h4> |
|
3588 </div> |
|
3589 </div> |
|
3590 </div> |
|
3591 <p id="ch11Section_5_f5"> |
|
3592 The command-syntax is: |
|
3593 </p> |
|
3594 <pre class="programlisting"> |
|
3595 $ more [options] [file_name] |
|
3596 </pre> |
|
3597 <p id="ch11Section_5_f6"> |
|
3598 If no file name is provided, |
|
3599 <span class="emphasis"> |
|
3600 <em> |
|
3601 more |
|
3602 </em> |
|
3603 </span> |
|
3604 looks for input from stdin. |
|
3605 </p> |
|
3606 <p id="ch11Section_5_f7"> |
|
3607 Once |
|
3608 <span class="emphasis"> |
|
3609 <em> |
|
3610 more |
|
3611 </em> |
|
3612 </span> |
|
3613 has obtained input, it displays as much as can fit on the current screen and waits for user input to advance, with the exception that a form feed (^L) will also cause |
|
3614 <span class="emphasis"> |
|
3615 <em> |
|
3616 more |
|
3617 </em> |
|
3618 </span> |
|
3619 to wait at that line, regardless of the amount of text on the screen. In the lower-left corner of the screen is displayed the text "--More--" and a percentage, representing the percent of the file that |
|
3620 <span class="emphasis"> |
|
3621 <em> |
|
3622 more |
|
3623 </em> |
|
3624 </span> |
|
3625 has paged through. (This percentage includes the text displayed on the current screen.) When |
|
3626 <span class="emphasis"> |
|
3627 <em> |
|
3628 more |
|
3629 </em> |
|
3630 </span> |
|
3631 reaches the end of a file (100%) it exits. The most common methods of navigating through a file are |
|
3632 <span class="emphasis"> |
|
3633 <em> |
|
3634 Enter |
|
3635 </em> |
|
3636 </span> |
|
3637 , which advances the output by one line, and |
|
3638 <span class="emphasis"> |
|
3639 <em> |
|
3640 Space |
|
3641 </em> |
|
3642 </span> |
|
3643 , which advances the output by one screen. |
|
3644 </p> |
|
3645 <p id="ch11Section_5_f8"> |
|
3646 There are also other commands that can be used while navigating through the document; consult |
|
3647 <span class="emphasis"> |
|
3648 <em> |
|
3649 more |
|
3650 </em> |
|
3651 </span> |
|
3652 's |
|
3653 <span class="emphasis"> |
|
3654 <em> |
|
3655 man |
|
3656 </em> |
|
3657 </span> |
|
3658 page for more details. |
|
3659 </p> |
|
3660 <p id="ch11Section_5_f9"> |
|
3661 <span class="emphasis"> |
|
3662 <em> |
|
3663 Options |
|
3664 </em> |
|
3665 </span> |
|
3666 are typically entered before the file name, but can also be entered in the environment variable |
|
3667 <span class="emphasis"> |
|
3668 <em> |
|
3669 $MORE |
|
3670 </em> |
|
3671 </span> |
|
3672 . Options entered in the actual command line will override those entered in the |
|
3673 <span class="emphasis"> |
|
3674 <em> |
|
3675 $MORE |
|
3676 </em> |
|
3677 </span> |
|
3678 environment variable. Available options may vary between Unix systems. |
|
3679 </p> |
|
3680 </div> |
|
3681 </div> |
|
3682 <div class="section" title="9.3.less"> |
|
3683 <div class="titlepage"> |
|
3684 <div> |
|
3685 <div> |
|
3686 <h3 class="title"> |
|
3687 <a name="id2948510"> |
|
3688 </a> |
|
3689 9.3.less |
|
3690 </h3> |
|
3691 </div> |
|
3692 </div> |
|
3693 </div> |
|
3694 <p id="ch11Section_5_fa"> |
|
3695 <span class="emphasis"> |
|
3696 <em> |
|
3697 less |
|
3698 </em> |
|
3699 </span> |
|
3700 is a terminal pager program on Unix, Windows and Unix-like systems used to view (but not change) the contents of a text file one screen at a time. It is similar to |
|
3701 <span class="emphasis"> |
|
3702 <em> |
|
3703 more |
|
3704 </em> |
|
3705 </span> |
|
3706 , but has the extended capability of allowing both forward and backward navigation through the file. Unlike most Unix text editors/viewers, |
|
3707 <span class="emphasis"> |
|
3708 <em> |
|
3709 less |
|
3710 </em> |
|
3711 </span> |
|
3712 does not need to read the entire file before starting, resulting in faster load times with large files. |
|
3713 </p> |
|
3714 <div class="section" title="9.3.1.Usage"> |
|
3715 <div class="titlepage"> |
|
3716 <div> |
|
3717 <div> |
|
3718 <h4 class="title"> |
|
3719 <a name="id2948538"> |
|
3720 </a> |
|
3721 9.3.1.Usage |
|
3722 </h4> |
|
3723 </div> |
|
3724 </div> |
|
3725 </div> |
|
3726 <p id="ch11Section_5_fb"> |
|
3727 <span class="emphasis"> |
|
3728 <em> |
|
3729 less |
|
3730 </em> |
|
3731 </span> |
|
3732 can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While |
|
3733 <span class="emphasis"> |
|
3734 <em> |
|
3735 less |
|
3736 </em> |
|
3737 </span> |
|
3738 is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both |
|
3739 <span class="emphasis"> |
|
3740 <em> |
|
3741 more |
|
3742 </em> |
|
3743 </span> |
|
3744 and |
|
3745 <span class="emphasis"> |
|
3746 <em> |
|
3747 vi |
|
3748 </em> |
|
3749 </span> |
|
3750 . It is also possible to search for character patterns in the file. |
|
3751 </p> |
|
3752 <p id="ch11Section_5_fc"> |
|
3753 By default, |
|
3754 <span class="emphasis"> |
|
3755 <em> |
|
3756 less |
|
3757 </em> |
|
3758 </span> |
|
3759 displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat. |
|
3760 </p> |
|
3761 <p id="ch11Section_5_fd"> |
|
3762 The command-syntax is: |
|
3763 </p> |
|
3764 <pre class="programlisting"> |
|
3765 $ less [options] file_name |
|
3766 </pre> |
|
3767 </div> |
|
3768 <div class="section" title="9.3.2.Frequently Used Options"> |
|
3769 <div class="titlepage"> |
|
3770 <div> |
|
3771 <div> |
|
3772 <h4 class="title"> |
|
3773 <a name="id2948592"> |
|
3774 </a> |
|
3775 9.3.2.Frequently Used Options |
|
3776 </h4> |
|
3777 </div> |
|
3778 </div> |
|
3779 </div> |
|
3780 <div class="itemizedlist"> |
|
3781 <ul class="itemizedlist" type="*"> |
|
3782 <li class="listitem" style="list-style-type: *"> |
|
3783 <p id="ch11Section_5_fe"> |
|
3784 -g: Highlights just the current match of any searched string. |
|
3785 </p> |
|
3786 </li> |
|
3787 <li class="listitem" style="list-style-type: *"> |
|
3788 <p id="ch11Section_5_ff"> |
|
3789 -I: Case-insensitive searches. |
|
3790 </p> |
|
3791 </li> |
|
3792 <li class="listitem" style="list-style-type: *"> |
|
3793 <p id="ch11Section_5_100"> |
|
3794 -M: Shows more detailed prompt, including file position. |
|
3795 </p> |
|
3796 </li> |
|
3797 <li class="listitem" style="list-style-type: *"> |
|
3798 <p id="ch11Section_5_101"> |
|
3799 -N: Shows line numbers (useful for source code viewing). |
|
3800 </p> |
|
3801 </li> |
|
3802 <li class="listitem" style="list-style-type: *"> |
|
3803 <p id="ch11Section_5_102"> |
|
3804 -S: Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling. |
|
3805 </p> |
|
3806 </li> |
|
3807 <li class="listitem" style="list-style-type: *"> |
|
3808 <p id="ch11Section_5_103"> |
|
3809 -?: Shows help. |
|
3810 </p> |
|
3811 </li> |
|
3812 </ul> |
|
3813 </div> |
|
3814 </div> |
|
3815 <div class="section" title="9.3.3.Frequently Used Commands"> |
|
3816 <div class="titlepage"> |
|
3817 <div> |
|
3818 <div> |
|
3819 <h4 class="title"> |
|
3820 <a name="id2948644"> |
|
3821 </a> |
|
3822 9.3.3.Frequently Used Commands |
|
3823 </h4> |
|
3824 </div> |
|
3825 </div> |
|
3826 </div> |
|
3827 <div class="itemizedlist"> |
|
3828 <ul class="itemizedlist" type="*"> |
|
3829 <li class="listitem" style="list-style-type: *"> |
|
3830 <p id="ch11Section_5_104"> |
|
3831 [Arrows]/[Page Up]/[Page Down]/[Home]/[End]: Navigation. |
|
3832 </p> |
|
3833 </li> |
|
3834 <li class="listitem" style="list-style-type: *"> |
|
3835 <p id="ch11Section_5_105"> |
|
3836 [Space bar]: Next page. |
|
3837 </p> |
|
3838 </li> |
|
3839 <li class="listitem" style="list-style-type: *"> |
|
3840 <p id="ch11Section_5_106"> |
|
3841 b: Previous page. |
|
3842 </p> |
|
3843 </li> |
|
3844 <li class="listitem" style="list-style-type: *"> |
|
3845 <p id="ch11Section_5_107"> |
|
3846 ng: Jump to line number n. Default is the start of the file. |
|
3847 </p> |
|
3848 </li> |
|
3849 <li class="listitem" style="list-style-type: *"> |
|
3850 <p id="ch11Section_5_108"> |
|
3851 nG: Jump to line number n. Default is the end of the file. |
|
3852 </p> |
|
3853 </li> |
|
3854 <li class="listitem" style="list-style-type: *"> |
|
3855 <p id="ch11Section_5_109"> |
|
3856 /pattern: Search for pattern. Regular expressions can be used. |
|
3857 </p> |
|
3858 </li> |
|
3859 <li class="listitem" style="list-style-type: *"> |
|
3860 <p id="ch11Section_5_10a"> |
|
3861 '^ or g: Go to start of file. |
|
3862 </p> |
|
3863 </li> |
|
3864 <li class="listitem" style="list-style-type: *"> |
|
3865 <p id="ch11Section_5_10b"> |
|
3866 '$ or G: Go to end of file. |
|
3867 </p> |
|
3868 </li> |
|
3869 <li class="listitem" style="list-style-type: *"> |
|
3870 <p id="ch11Section_5_10c"> |
|
3871 s: Save current content (got from another program like grep) in a file. |
|
3872 </p> |
|
3873 </li> |
|
3874 <li class="listitem" style="list-style-type: *"> |
|
3875 <p id="ch11Section_5_10d"> |
|
3876 =: File information. |
|
3877 </p> |
|
3878 </li> |
|
3879 <li class="listitem" style="list-style-type: *"> |
|
3880 <p id="ch11Section_5_10e"> |
|
3881 h: Help. |
|
3882 </p> |
|
3883 </li> |
|
3884 <li class="listitem" style="list-style-type: *"> |
|
3885 <p id="ch11Section_5_10f"> |
|
3886 q: Quit. |
|
3887 </p> |
|
3888 </li> |
|
3889 </ul> |
|
3890 </div> |
|
3891 </div> |
|
3892 <div class="section" title="9.3.4.Examples"> |
|
3893 <div class="titlepage"> |
|
3894 <div> |
|
3895 <div> |
|
3896 <h4 class="title"> |
|
3897 <a name="id2948732"> |
|
3898 </a> |
|
3899 9.3.4.Examples |
|
3900 </h4> |
|
3901 </div> |
|
3902 </div> |
|
3903 </div> |
|
3904 <pre class="programlisting"> |
|
3905 $ less -M readme.txt #Read "readme.txt." |
|
3906 $ less +F /var/log/mail.log #Follow mode for log |
|
3907 $ file * | less #Easier file analysis. |
|
3908 $ grep -i void *.c | less -I -p void #Case insensitive search for "void" in all .c files |
|
3909 </pre> |
|
3910 </div> |
|
3911 </div> |
|
3912 </div> |
|
3913 <div class="section" title="10.Directory Structure"> |
|
3914 <div class="titlepage"> |
|
3915 <div> |
|
3916 <div> |
|
3917 <h2 class="title" style="clear: both"> |
|
3918 <a name="id2948748"> |
|
3919 </a> |
|
3920 10.Directory Structure |
|
3921 </h2> |
|
3922 </div> |
|
3923 </div> |
|
3924 </div> |
|
3925 <p id="ch11Section_5_110"> |
|
3926 In the File Hierarchy Standard (FHS) all files and directories appear under the root directory "/", even if they are stored on different physical devices. Note however that some of these directories may or may not be present on a Unix system depending on whether certain subsystems, such as the X Window System, are installed. |
|
3927 </p> |
|
3928 <p id="ch11Section_5_111"> |
|
3929 The majority of these directories exist in all UNIX operating systems and are generally used in much the same way; however, the descriptions here are those used specifically for the FHS, and are not considered authoritative for platforms other than Linux. |
|
3930 </p> |
|
3931 <table summary="Directory Structure" border="1"> |
|
3932 <colgroup> |
|
3933 <col width="15"> |
|
3934 </col> |
|
3935 <col width="48"> |
|
3936 </col> |
|
3937 </colgroup> |
|
3938 </table> |
|
3939 <div class="section" title="10.1.man hier"> |
|
3940 <div class="titlepage"> |
|
3941 <div> |
|
3942 <div> |
|
3943 <h3 class="title"> |
|
3944 <a name="id2949017"> |
|
3945 </a> |
|
3946 10.1.man hier |
|
3947 </h3> |
|
3948 </div> |
|
3949 </div> |
|
3950 </div> |
|
3951 <p id="ch11Section_5_136"> |
|
3952 This is the manual page on the UNIX filesystem. The syntax for this is: |
|
3953 </p> |
|
3954 <pre class="programlisting"> |
|
3955 $ man hier |
|
3956 </pre> |
|
3957 </div> |
|
3958 <div class="section" title="10.2.ls -l"> |
|
3959 <div class="titlepage"> |
|
3960 <div> |
|
3961 <div> |
|
3962 <h3 class="title"> |
|
3963 <a name="id2949033"> |
|
3964 </a> |
|
3965 10.2.ls -l |
|
3966 </h3> |
|
3967 </div> |
|
3968 </div> |
|
3969 </div> |
|
3970 <p id="ch11Section_5_137"> |
|
3971 Shows you huge amounts of information (permissions, owners, size, and when last modified) for folders and files. The syntax is |
|
3972 </p> |
|
3973 <pre class="programlisting"> |
|
3974 $ ls -l |
|
3975 </pre> |
|
3976 <p id="ch11Section_5_138"> |
|
3977 This can be done after entering the required directory. |
|
3978 </p> |
|
3979 </div> |
|
3980 </div> |
|
3981 <div class="section" title="11.Permissions and Ownership"> |
|
3982 <div class="titlepage"> |
|
3983 <div> |
|
3984 <div> |
|
3985 <h2 class="title" style="clear: both"> |
|
3986 <a name="id2949056"> |
|
3987 </a> |
|
3988 11.Permissions and Ownership |
|
3989 </h2> |
|
3990 </div> |
|
3991 </div> |
|
3992 </div> |
|
3993 <div class="section" title="11.1.chmod"> |
|
3994 <div class="titlepage"> |
|
3995 <div> |
|
3996 <div> |
|
3997 <h3 class="title"> |
|
3998 <a name="id2949065"> |
|
3999 </a> |
|
4000 11.1.chmod |
|
4001 </h3> |
|
4002 </div> |
|
4003 </div> |
|
4004 </div> |
|
4005 <p id="ch11Section_5_139"> |
|
4006 The |
|
4007 <span class="emphasis"> |
|
4008 <em> |
|
4009 chmod |
|
4010 </em> |
|
4011 </span> |
|
4012 command (abbreviated from 'change mode') is a shell command and C language function in Unix and Unix-like environments. When executed, it can change file system modes of files and directories. The modes include permissions and special modes.A chmod command first appeared in AT&T Unix version 1, and is still used today on Unix-like machines. |
|
4013 </p> |
|
4014 <div class="section" title="11.1.1.Usage"> |
|
4015 <div class="titlepage"> |
|
4016 <div> |
|
4017 <div> |
|
4018 <h4 class="title"> |
|
4019 <a name="id2949091"> |
|
4020 </a> |
|
4021 11.1.1.Usage |
|
4022 </h4> |
|
4023 </div> |
|
4024 </div> |
|
4025 </div> |
|
4026 <p id="ch11Section_5_13a"> |
|
4027 The |
|
4028 <span class="emphasis"> |
|
4029 <em> |
|
4030 chmod |
|
4031 </em> |
|
4032 </span> |
|
4033 command options are specified like this: |
|
4034 </p> |
|
4035 <pre class="programlisting"> |
|
4036 $ chmod [options] mode[,mode] file1 [file2 ...] |
|
4037 </pre> |
|
4038 <p id="ch11Section_5_13b"> |
|
4039 To view what the permissions currently are, type: |
|
4040 </p> |
|
4041 <pre class="programlisting"> |
|
4042 $ ls -l file |
|
4043 </pre> |
|
4044 </div> |
|
4045 <div class="section" title="11.1.2.Command line options"> |
|
4046 <div class="titlepage"> |
|
4047 <div> |
|
4048 <div> |
|
4049 <h4 class="title"> |
|
4050 <a name="id2949118"> |
|
4051 </a> |
|
4052 11.1.2.Command line options |
|
4053 </h4> |
|
4054 </div> |
|
4055 </div> |
|
4056 </div> |
|
4057 <p id="ch11Section_5_13c"> |
|
4058 The |
|
4059 <span class="emphasis"> |
|
4060 <em> |
|
4061 chmod |
|
4062 </em> |
|
4063 </span> |
|
4064 command has a number of command line options that affect its behavior. The most common options are: |
|
4065 </p> |
|
4066 <div class="itemizedlist"> |
|
4067 <ul class="itemizedlist" type="*"> |
|
4068 <li class="listitem" style="list-style-type: *"> |
|
4069 <p id="ch11Section_5_13d"> |
|
4070 -R: Changes the modes of directories and files recursively |
|
4071 </p> |
|
4072 </li> |
|
4073 <li class="listitem" style="list-style-type: *"> |
|
4074 <p id="ch11Section_5_13e"> |
|
4075 -v: Verbose mode; lists all files as they are being processed |
|
4076 </p> |
|
4077 </li> |
|
4078 </ul> |
|
4079 </div> |
|
4080 <div class="section" title="11.1.2.1.Symbolic modes"> |
|
4081 <div class="titlepage"> |
|
4082 <div> |
|
4083 <div> |
|
4084 <h5 class="title"> |
|
4085 <a name="id2949154"> |
|
4086 </a> |
|
4087 11.1.2.1.Symbolic modes |
|
4088 </h5> |
|
4089 </div> |
|
4090 </div> |
|
4091 </div> |
|
4092 <p id="ch11Section_5_13f"> |
|
4093 To the |
|
4094 <span class="emphasis"> |
|
4095 <em> |
|
4096 chmod |
|
4097 </em> |
|
4098 </span> |
|
4099 utility, all permissions and special modes are represented by its mode parameter. One way to adjust the mode of files or directories is to specify a symbolic mode. The symbolic mode is composed of three components, which are combined to form a single string of text: |
|
4100 </p> |
|
4101 <pre class="programlisting"> |
|
4102 $ chmod [references][operator][modes] file1 ... |
|
4103 </pre> |
|
4104 <p id="ch11Section_5_140"> |
|
4105 The references (or classes) are used to distinguish the users to whom the permissions apply. If no references are specified it defaults to all but modifies only the permissions allowed by the umask. The references are represented by one or more of the following letters: |
|
4106 </p> |
|
4107 <table summary="Symbolic modes" border="1"> |
|
4108 <colgroup> |
|
4109 <col width="14"> |
|
4110 </col> |
|
4111 <col width="8"> |
|
4112 </col> |
|
4113 <col width="45"> |
|
4114 </col> |
|
4115 </colgroup> |
|
4116 </table> |
|
4117 <p id="ch11Section_5_150"> |
|
4118 The |
|
4119 <span class="emphasis"> |
|
4120 <em> |
|
4121 chmod |
|
4122 </em> |
|
4123 </span> |
|
4124 program uses an operator to specify how the modes of a file should be adjusted. The following operators are accepted: |
|
4125 </p> |
|
4126 <table summary="Symbolic modes" border="1"> |
|
4127 <colgroup> |
|
4128 <col width="14"> |
|
4129 </col> |
|
4130 <col width="54"> |
|
4131 </col> |
|
4132 </colgroup> |
|
4133 </table> |
|
4134 <p id="ch11Section_5_157"> |
|
4135 The modes indicate which permissions are to be granted or taken away from the specified classes. There are three basic modes which correspond to the basic permissions: |
|
4136 </p> |
|
4137 <table summary="Symbolic modes" border="1"> |
|
4138 <colgroup> |
|
4139 <col width="5"> |
|
4140 </col> |
|
4141 <col width="14"> |
|
4142 </col> |
|
4143 <col width="48"> |
|
4144 </col> |
|
4145 </colgroup> |
|
4146 </table> |
|
4147 <p id="ch11Section_5_16d"> |
|
4148 The combination of these three components produces a string that is understood by the chmod command. Multiple changes can be specified by separating multiple symbolic modes with commas. |
|
4149 </p> |
|
4150 </div> |
|
4151 <div class="section" title="11.1.2.2.Symbolic examples"> |
|
4152 <div class="titlepage"> |
|
4153 <div> |
|
4154 <div> |
|
4155 <h5 class="title"> |
|
4156 <a name="id2949700"> |
|
4157 </a> |
|
4158 11.1.2.2.Symbolic examples |
|
4159 </h5> |
|
4160 </div> |
|
4161 </div> |
|
4162 </div> |
|
4163 <p id="ch11Section_5_16e"> |
|
4164 Add the 'read' and 'write' permissions to the 'user' and 'group' classes of a directory: |
|
4165 </p> |
|
4166 <pre class="programlisting"> |
|
4167 $ chmod ug+rw mydir |
|
4168 $ ls -ld mydir |
|
4169 drw-rw---- 2 starwars yoda 96 Dec 8 12:53 mydir |
|
4170 </pre> |
|
4171 <p id="ch11Section_5_16f"> |
|
4172 For a file, remove |
|
4173 <span class="emphasis"> |
|
4174 <em> |
|
4175 write |
|
4176 </em> |
|
4177 </span> |
|
4178 permissions for all classes: |
|
4179 </p> |
|
4180 <pre class="programlisting"> |
|
4181 $ chmod a-w myfile |
|
4182 $ ls -l myfile |
|
4183 -r-xr-xr-x 2 starwars yoda 96 Dec 8 12:53 myfile |
|
4184 </pre> |
|
4185 <p id="ch11Section_5_170"> |
|
4186 Set the permissions for the |
|
4187 <span class="emphasis"> |
|
4188 <em> |
|
4189 u*ser and the *g*roup to read and execute only (no write permission) on *mydir |
|
4190 </em> |
|
4191 </span> |
|
4192 . |
|
4193 </p> |
|
4194 <pre class="programlisting"> |
|
4195 $ chmod ug=rx mydir |
|
4196 $ ls -ld mydir |
|
4197 dr-xr-x--- 2 starwars yoda 96 Dec 8 12:53 mydir |
|
4198 </pre> |
|
4199 </div> |
|
4200 <div class="section" title="11.1.2.3.Octal numbers"> |
|
4201 <div class="titlepage"> |
|
4202 <div> |
|
4203 <div> |
|
4204 <h5 class="title"> |
|
4205 <a name="id2949741"> |
|
4206 </a> |
|
4207 11.1.2.3.Octal numbers |
|
4208 </h5> |
|
4209 </div> |
|
4210 </div> |
|
4211 </div> |
|
4212 <p id="ch11Section_5_171"> |
|
4213 The |
|
4214 <span class="emphasis"> |
|
4215 <em> |
|
4216 chmod |
|
4217 </em> |
|
4218 </span> |
|
4219 command also accepts three and four-digit octal numbers representing modes. Using a three-digit octal number to set the modes of a file named myfile : |
|
4220 </p> |
|
4221 <pre class="programlisting"> |
|
4222 $ chmod 664 myfile |
|
4223 $ ls -l myfile |
|
4224 -rw-rw-r-- 1 57 Jul 3 10:13 myfile |
|
4225 </pre> |
|
4226 <p id="ch11Section_5_172"> |
|
4227 Since the |
|
4228 <span class="emphasis"> |
|
4229 <em> |
|
4230 setuid |
|
4231 </em> |
|
4232 </span> |
|
4233 , |
|
4234 <span class="emphasis"> |
|
4235 <em> |
|
4236 setgid |
|
4237 </em> |
|
4238 </span> |
|
4239 and |
|
4240 <span class="emphasis"> |
|
4241 <em> |
|
4242 sticky |
|
4243 </em> |
|
4244 </span> |
|
4245 bits are not set, this is equivalent to: |
|
4246 </p> |
|
4247 <pre class="programlisting"> |
|
4248 $ chmod 0664 myfile |
|
4249 </pre> |
|
4250 </div> |
|
4251 <div class="section" title="11.1.2.4.Special modes"> |
|
4252 <div class="titlepage"> |
|
4253 <div> |
|
4254 <div> |
|
4255 <h5 class="title"> |
|
4256 <a name="id2949782"> |
|
4257 </a> |
|
4258 11.1.2.4.Special modes |
|
4259 </h5> |
|
4260 </div> |
|
4261 </div> |
|
4262 </div> |
|
4263 <p id="ch11Section_5_173"> |
|
4264 The |
|
4265 <span class="emphasis"> |
|
4266 <em> |
|
4267 chmod |
|
4268 </em> |
|
4269 </span> |
|
4270 command is also capable of changing the additional permissions or special modes of a file or directory. The symbolic modes use |
|
4271 <span class="strong"> |
|
4272 <strong> |
|
4273 s |
|
4274 </strong> |
|
4275 </span> |
|
4276 to represent the |
|
4277 <span class="emphasis"> |
|
4278 <em> |
|
4279 setuid |
|
4280 </em> |
|
4281 </span> |
|
4282 and |
|
4283 <span class="emphasis"> |
|
4284 <em> |
|
4285 setgid |
|
4286 </em> |
|
4287 </span> |
|
4288 modes, and |
|
4289 <span class="strong"> |
|
4290 <strong> |
|
4291 t |
|
4292 </strong> |
|
4293 </span> |
|
4294 to represent the sticky mode. The modes are only applied to the appropriate classes, regardless of whether or not other classes are specified. |
|
4295 </p> |
|
4296 <p id="ch11Section_5_174"> |
|
4297 Most operating systems support the specification of special modes using octal modes, but some do not. On these systems, only the symbolic modes can be used. |
|
4298 </p> |
|
4299 </div> |
|
4300 </div> |
|
4301 </div> |
|
4302 </div> |
|
4303 <div class="section" title="12.Redirection and Piping"> |
|
4304 <div class="titlepage"> |
|
4305 <div> |
|
4306 <div> |
|
4307 <h2 class="title" style="clear: both"> |
|
4308 <a name="id2949829"> |
|
4309 </a> |
|
4310 12.Redirection and Piping |
|
4311 </h2> |
|
4312 </div> |
|
4313 </div> |
|
4314 </div> |
|
4315 <p id="ch11Section_5_175"> |
|
4316 In computing, |
|
4317 <span class="emphasis"> |
|
4318 <em> |
|
4319 redirection |
|
4320 </em> |
|
4321 </span> |
|
4322 is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations. |
|
4323 </p> |
|
4324 <p id="ch11Section_5_176"> |
|
4325 Programs do redirection with the |
|
4326 <span class="emphasis"> |
|
4327 <em> |
|
4328 dup2(2) |
|
4329 </em> |
|
4330 </span> |
|
4331 system call, or its less-flexible but higher-level stdio analogues, |
|
4332 <span class="emphasis"> |
|
4333 <em> |
|
4334 freopen(3) |
|
4335 </em> |
|
4336 </span> |
|
4337 and |
|
4338 <span class="emphasis"> |
|
4339 <em> |
|
4340 popen(3) |
|
4341 </em> |
|
4342 </span> |
|
4343 . |
|
4344 </p> |
|
4345 <div class="section" title="12.1.Redirecting standard input and standard output"> |
|
4346 <div class="titlepage"> |
|
4347 <div> |
|
4348 <div> |
|
4349 <h3 class="title"> |
|
4350 <a name="id2949866"> |
|
4351 </a> |
|
4352 12.1.Redirecting standard input and standard output |
|
4353 </h3> |
|
4354 </div> |
|
4355 </div> |
|
4356 </div> |
|
4357 <p id="ch11Section_5_177"> |
|
4358 Redirection is usually implemented by placing certain characters between commands. Typically, the syntax of these characters is as follows: |
|
4359 </p> |
|
4360 <pre class="programlisting"> |
|
4361 $ command1 > file1 |
|
4362 </pre> |
|
4363 <p id="ch11Section_5_178"> |
|
4364 executes |
|
4365 <span class="emphasis"> |
|
4366 <em> |
|
4367 command1 |
|
4368 </em> |
|
4369 </span> |
|
4370 , placing the output in file1. Note that this will truncate any existing data in |
|
4371 <span class="emphasis"> |
|
4372 <em> |
|
4373 file1 |
|
4374 </em> |
|
4375 </span> |
|
4376 . To append output to the end of the file, use the >> operator.: |
|
4377 </p> |
|
4378 <pre class="programlisting"> |
|
4379 $ command1 < file1 |
|
4380 </pre> |
|
4381 <p id="ch11Section_5_179"> |
|
4382 executes |
|
4383 <span class="emphasis"> |
|
4384 <em> |
|
4385 command1 |
|
4386 </em> |
|
4387 </span> |
|
4388 , using |
|
4389 <span class="emphasis"> |
|
4390 <em> |
|
4391 file1 |
|
4392 </em> |
|
4393 </span> |
|
4394 as the source of input (as opposed to the keyboard).: |
|
4395 </p> |
|
4396 <pre class="programlisting"> |
|
4397 $ command1 < infile > outfile |
|
4398 </pre> |
|
4399 <p id="ch11Section_5_17a"> |
|
4400 combines the two capabilities: |
|
4401 <span class="emphasis"> |
|
4402 <em> |
|
4403 command1 |
|
4404 </em> |
|
4405 </span> |
|
4406 reads from |
|
4407 <span class="emphasis"> |
|
4408 <em> |
|
4409 infile |
|
4410 </em> |
|
4411 </span> |
|
4412 and writes to |
|
4413 <span class="emphasis"> |
|
4414 <em> |
|
4415 outfile |
|
4416 </em> |
|
4417 </span> |
|
4418 </p> |
|
4419 </div> |
|
4420 <div class="section" title="12.2.Piping"> |
|
4421 <div class="titlepage"> |
|
4422 <div> |
|
4423 <div> |
|
4424 <h3 class="title"> |
|
4425 <a name="id2949936"> |
|
4426 </a> |
|
4427 12.2.Piping |
|
4428 </h3> |
|
4429 </div> |
|
4430 </div> |
|
4431 </div> |
|
4432 <p id="ch11Section_5_17b"> |
|
4433 Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file: |
|
4434 A pipeline of three programs run on a text terminal: |
|
4435 </p> |
|
4436 <pre class="programlisting"> |
|
4437 $ command1 | command2 |
|
4438 </pre> |
|
4439 <p id="ch11Section_5_17c"> |
|
4440 executes |
|
4441 <span class="emphasis"> |
|
4442 <em> |
|
4443 command1 |
|
4444 </em> |
|
4445 </span> |
|
4446 , using its output as the input for |
|
4447 <span class="emphasis"> |
|
4448 <em> |
|
4449 command2 |
|
4450 </em> |
|
4451 </span> |
|
4452 (commonly called piping, since the "|" character is known as a "pipe"). |
|
4453 </p> |
|
4454 <p id="ch11Section_5_17d"> |
|
4455 This is equivalent to using two redirects and a temporary file: |
|
4456 </p> |
|
4457 <pre class="programlisting"> |
|
4458 $ command1 > tempfile |
|
4459 $ command2 < tempfile |
|
4460 $ rm tempfile |
|
4461 </pre> |
|
4462 <p id="ch11Section_5_17e"> |
|
4463 A good example for command piping is combining |
|
4464 <span class="emphasis"> |
|
4465 <em> |
|
4466 echo |
|
4467 </em> |
|
4468 </span> |
|
4469 with another command to achieve something interactive in a non-interactive shell, e.g.: |
|
4470 </p> |
|
4471 <pre class="programlisting"> |
|
4472 $ echo -e "user\npass" | ftp localhost |
|
4473 </pre> |
|
4474 <p id="ch11Section_5_17f"> |
|
4475 This runs the ftp client with input user, press return, then pass. |
|
4476 </p> |
|
4477 </div> |
|
4478 <div class="section" title="12.3.Redirecting to and from the standard file handles"> |
|
4479 <div class="titlepage"> |
|
4480 <div> |
|
4481 <div> |
|
4482 <h3 class="title"> |
|
4483 <a name="id2949997"> |
|
4484 </a> |
|
4485 12.3.Redirecting to and from the standard file handles |
|
4486 </h3> |
|
4487 </div> |
|
4488 </div> |
|
4489 </div> |
|
4490 <p id="ch11Section_5_180"> |
|
4491 In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are: |
|
4492 </p> |
|
4493 <table summary="Redirecting to and from the standard file handles" border="1"> |
|
4494 <colgroup> |
|
4495 <col width="12"> |
|
4496 </col> |
|
4497 <col width="13"> |
|
4498 </col> |
|
4499 <col width="24"> |
|
4500 </col> |
|
4501 </colgroup> |
|
4502 </table> |
|
4503 <p id="ch11Section_5_18d"> |
|
4504 For example: |
|
4505 </p> |
|
4506 <pre class="programlisting"> |
|
4507 $ command1 2> file1 |
|
4508 </pre> |
|
4509 <p id="ch11Section_5_18e"> |
|
4510 executes |
|
4511 <span class="emphasis"> |
|
4512 <em> |
|
4513 command1 |
|
4514 </em> |
|
4515 </span> |
|
4516 , directing the standard error stream to |
|
4517 <span class="emphasis"> |
|
4518 <em> |
|
4519 file1 |
|
4520 </em> |
|
4521 </span> |
|
4522 . |
|
4523 </p> |
|
4524 <p id="ch11Section_5_18f"> |
|
4525 In shells derived from |
|
4526 <span class="emphasis"> |
|
4527 <em> |
|
4528 csh |
|
4529 </em> |
|
4530 </span> |
|
4531 (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result. |
|
4532 </p> |
|
4533 <p id="ch11Section_5_190"> |
|
4534 Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example: |
|
4535 </p> |
|
4536 <pre class="programlisting"> |
|
4537 $ find / -name .profile > results 2>&1 |
|
4538 </pre> |
|
4539 <p id="ch11Section_5_191"> |
|
4540 will try to find all files named |
|
4541 <span class="emphasis"> |
|
4542 <em> |
|
4543 .profile |
|
4544 </em> |
|
4545 </span> |
|
4546 . Executed without redirection, it will output hits to |
|
4547 <span class="emphasis"> |
|
4548 <em> |
|
4549 stdout |
|
4550 </em> |
|
4551 </span> |
|
4552 and errors (e.g. for lack of privilege to traverse protected directories) to |
|
4553 <span class="emphasis"> |
|
4554 <em> |
|
4555 stderr |
|
4556 </em> |
|
4557 </span> |
|
4558 . If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge |
|
4559 <span class="emphasis"> |
|
4560 <em> |
|
4561 stderr |
|
4562 </em> |
|
4563 </span> |
|
4564 (handle 2) into |
|
4565 <span class="emphasis"> |
|
4566 <em> |
|
4567 stdout |
|
4568 </em> |
|
4569 </span> |
|
4570 (handle 1) using 2>&1 . |
|
4571 </p> |
|
4572 <p id="ch11Section_5_192"> |
|
4573 It's possible use 2>&1 before ">" but it doesn't work. In fact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged. |
|
4574 </p> |
|
4575 <p id="ch11Section_5_193"> |
|
4576 If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus: |
|
4577 </p> |
|
4578 <pre class="programlisting"> |
|
4579 $ find / -name .profile 2>&1 | less |
|
4580 </pre> |
|
4581 <p id="ch11Section_5_194"> |
|
4582 A simplified form of the command: |
|
4583 </p> |
|
4584 <pre class="programlisting"> |
|
4585 $ command > file 2>&1 |
|
4586 </pre> |
|
4587 <p id="ch11Section_5_195"> |
|
4588 is: |
|
4589 </p> |
|
4590 <pre class="programlisting"> |
|
4591 $ command &>file |
|
4592 </pre> |
|
4593 <p id="ch11Section_5_196"> |
|
4594 or: |
|
4595 </p> |
|
4596 <pre class="programlisting"> |
|
4597 $command >&file |
|
4598 </pre> |
|
4599 </div> |
|
4600 <div class="section" title="12.4.Chained pipelines"> |
|
4601 <div class="titlepage"> |
|
4602 <div> |
|
4603 <div> |
|
4604 <h3 class="title"> |
|
4605 <a name="id2950201"> |
|
4606 </a> |
|
4607 12.4.Chained pipelines |
|
4608 </h3> |
|
4609 </div> |
|
4610 </div> |
|
4611 </div> |
|
4612 <p id="ch11Section_5_197"> |
|
4613 The redirection and piping tokens can be chained together to create complex commands. For example: |
|
4614 </p> |
|
4615 <pre class="programlisting"> |
|
4616 $ ls | grep '\.sh' | sort > shlist |
|
4617 </pre> |
|
4618 <p id="ch11Section_5_198"> |
|
4619 lists the contents of the current directory, where this output is filtered to only contain lines which contain |
|
4620 <span class="emphasis"> |
|
4621 <em> |
|
4622 .sh |
|
4623 </em> |
|
4624 </span> |
|
4625 , sort this resultant output lexicographically, and place the final output in |
|
4626 <span class="emphasis"> |
|
4627 <em> |
|
4628 shlist |
|
4629 </em> |
|
4630 </span> |
|
4631 . This type of construction is used very commonly in shell scripts and batch files. |
|
4632 </p> |
|
4633 </div> |
|
4634 <div class="section" title="12.5.Redirect to multiple outputs"> |
|
4635 <div class="titlepage"> |
|
4636 <div> |
|
4637 <div> |
|
4638 <h3 class="title"> |
|
4639 <a name="id2950235"> |
|
4640 </a> |
|
4641 12.5.Redirect to multiple outputs |
|
4642 </h3> |
|
4643 </div> |
|
4644 </div> |
|
4645 </div> |
|
4646 <p id="ch11Section_5_199"> |
|
4647 The standard command |
|
4648 <span class="emphasis"> |
|
4649 <em> |
|
4650 tee |
|
4651 </em> |
|
4652 </span> |
|
4653 can redirect output from a command to several destinations. |
|
4654 </p> |
|
4655 <pre class="programlisting"> |
|
4656 $ ls -lrt | tee xyz |
|
4657 </pre> |
|
4658 <p id="ch11Section_5_19a"> |
|
4659 This directs the file list output to both standard output as well as to the file |
|
4660 <span class="emphasis"> |
|
4661 <em> |
|
4662 xyz |
|
4663 </em> |
|
4664 </span> |
|
4665 . |
|
4666 </p> |
|
4667 </div> |
|
4668 </div> |
|
4669 <div class="section" title="13.More Text Processing"> |
|
4670 <div class="titlepage"> |
|
4671 <div> |
|
4672 <div> |
|
4673 <h2 class="title" style="clear: both"> |
|
4674 <a name="id2950266"> |
|
4675 </a> |
|
4676 13.More Text Processing |
|
4677 </h2> |
|
4678 </div> |
|
4679 </div> |
|
4680 </div> |
|
4681 <div class="section" title="13.1.grep"> |
|
4682 <div class="titlepage"> |
|
4683 <div> |
|
4684 <div> |
|
4685 <h3 class="title"> |
|
4686 <a name="id2950274"> |
|
4687 </a> |
|
4688 13.1.grep |
|
4689 </h3> |
|
4690 </div> |
|
4691 </div> |
|
4692 </div> |
|
4693 <p id="ch11Section_5_19b"> |
|
4694 <span class="emphasis"> |
|
4695 <em> |
|
4696 grep |
|
4697 </em> |
|
4698 </span> |
|
4699 is a command line text search utility originally written for Unix. The name is taken from the first letters in |
|
4700 <span class="emphasis"> |
|
4701 <em> |
|
4702 global / regular expression / print |
|
4703 </em> |
|
4704 </span> |
|
4705 , a series of instructions for the |
|
4706 <span class="emphasis"> |
|
4707 <em> |
|
4708 ed |
|
4709 </em> |
|
4710 </span> |
|
4711 text editor. The |
|
4712 <span class="emphasis"> |
|
4713 <em> |
|
4714 grep |
|
4715 </em> |
|
4716 </span> |
|
4717 command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output. |
|
4718 </p> |
|
4719 <div class="section" title="13.1.1.Usage"> |
|
4720 <div class="titlepage"> |
|
4721 <div> |
|
4722 <div> |
|
4723 <h4 class="title"> |
|
4724 <a name="id2950305"> |
|
4725 </a> |
|
4726 13.1.1.Usage |
|
4727 </h4> |
|
4728 </div> |
|
4729 </div> |
|
4730 </div> |
|
4731 <p id="ch11Section_5_19c"> |
|
4732 This is an example of a common |
|
4733 <span class="emphasis"> |
|
4734 <em> |
|
4735 grep |
|
4736 </em> |
|
4737 </span> |
|
4738 usage: |
|
4739 </p> |
|
4740 <pre class="programlisting"> |
|
4741 $ grep apple fruitlist.txt |
|
4742 </pre> |
|
4743 <p id="ch11Section_5_19d"> |
|
4744 In this case, |
|
4745 <span class="emphasis"> |
|
4746 <em> |
|
4747 grep |
|
4748 </em> |
|
4749 </span> |
|
4750 prints all lines containing 'apple' from the file |
|
4751 <span class="emphasis"> |
|
4752 <em> |
|
4753 fruitlist.txt |
|
4754 </em> |
|
4755 </span> |
|
4756 , regardless of word boundaries; therefore lines containing 'pineapple' or 'apples' are also printed. The |
|
4757 <span class="emphasis"> |
|
4758 <em> |
|
4759 grep |
|
4760 </em> |
|
4761 </span> |
|
4762 command is case sensitive by default, so this example's output does not include lines containing 'Apple' (with a capital A) unless they also contain 'apple'. |
|
4763 </p> |
|
4764 <p id="ch11Section_5_19e"> |
|
4765 Like most Unix commands, |
|
4766 <span class="emphasis"> |
|
4767 <em> |
|
4768 grep |
|
4769 </em> |
|
4770 </span> |
|
4771 accepts command line arguments to change this and many other behaviors. For example: |
|
4772 </p> |
|
4773 <pre class="programlisting"> |
|
4774 $ grep -i apple fruitlist.txt |
|
4775 </pre> |
|
4776 <p id="ch11Section_5_19f"> |
|
4777 This prints all lines containing 'apple' regardless of capitalization. The '-i' argument tells |
|
4778 <span class="emphasis"> |
|
4779 <em> |
|
4780 grep |
|
4781 </em> |
|
4782 </span> |
|
4783 to be case insensitive, or to ignore case. |
|
4784 </p> |
|
4785 <p id="ch11Section_5_1a0"> |
|
4786 To print all lines containing 'apple' as a word ('pineapple' and 'apples' will not match): |
|
4787 </p> |
|
4788 <pre class="programlisting"> |
|
4789 $ grep -w apple fruitlist.txt |
|
4790 </pre> |
|
4791 <p id="ch11Section_5_1a1"> |
|
4792 Regular expressions can be used to match more complicated queries. |
|
4793 </p> |
|
4794 <div class="section" title="13.1.1.1.Variations"> |
|
4795 <div class="titlepage"> |
|
4796 <div> |
|
4797 <div> |
|
4798 <h5 class="title"> |
|
4799 <a name="id2950384"> |
|
4800 </a> |
|
4801 13.1.1.1.Variations |
|
4802 </h5> |
|
4803 </div> |
|
4804 </div> |
|
4805 </div> |
|
4806 <p id="ch11Section_5_1a2"> |
|
4807 There are countless implementations and derivatives of |
|
4808 <span class="emphasis"> |
|
4809 <em> |
|
4810 grep |
|
4811 </em> |
|
4812 </span> |
|
4813 available for many operating systems. Early variants of |
|
4814 <span class="emphasis"> |
|
4815 <em> |
|
4816 grep |
|
4817 </em> |
|
4818 </span> |
|
4819 included |
|
4820 <span class="emphasis"> |
|
4821 <em> |
|
4822 egrep |
|
4823 </em> |
|
4824 </span> |
|
4825 and |
|
4826 <span class="emphasis"> |
|
4827 <em> |
|
4828 fgrep |
|
4829 </em> |
|
4830 </span> |
|
4831 . The former applies an extended regular expression syntax that was added to Unix after Ken Thompson's original regular expression implementation. The latter searches for any of a list of 'fixed' strings using the Aho-Corasick algorithm. These variants are embodied in most modern |
|
4832 <span class="emphasis"> |
|
4833 <em> |
|
4834 grep |
|
4835 </em> |
|
4836 </span> |
|
4837 implementations as command-line switches (and standardized as -E and -F in POSIX). In such combined implementations, |
|
4838 <span class="emphasis"> |
|
4839 <em> |
|
4840 grep |
|
4841 </em> |
|
4842 </span> |
|
4843 may also behave differently depending on the name by which it is invoked, allowing |
|
4844 <span class="emphasis"> |
|
4845 <em> |
|
4846 fgrep |
|
4847 </em> |
|
4848 </span> |
|
4849 , |
|
4850 <span class="emphasis"> |
|
4851 <em> |
|
4852 egrep |
|
4853 </em> |
|
4854 </span> |
|
4855 , and |
|
4856 <span class="emphasis"> |
|
4857 <em> |
|
4858 grep |
|
4859 </em> |
|
4860 </span> |
|
4861 to be links to the same program. |
|
4862 </p> |
|
4863 <p id="ch11Section_5_1a3"> |
|
4864 <span class="emphasis"> |
|
4865 <em> |
|
4866 pcregrep |
|
4867 </em> |
|
4868 </span> |
|
4869 is an implementation of |
|
4870 <span class="emphasis"> |
|
4871 <em> |
|
4872 grep |
|
4873 </em> |
|
4874 </span> |
|
4875 that uses Perl regular expression syntax. |
|
4876 </p> |
|
4877 <p id="ch11Section_5_1a4"> |
|
4878 Other commands contain the word 'grep' to indicate that they search (usually for regular expression matches). The |
|
4879 <span class="emphasis"> |
|
4880 <em> |
|
4881 pgrep |
|
4882 </em> |
|
4883 </span> |
|
4884 utility, for instance, displays the processes whose names match a given regular expression. |
|
4885 </p> |
|
4886 </div> |
|
4887 </div> |
|
4888 </div> |
|
4889 <div class="section" title="13.2.tr"> |
|
4890 <div class="titlepage"> |
|
4891 <div> |
|
4892 <div> |
|
4893 <h3 class="title"> |
|
4894 <a name="id2950463"> |
|
4895 </a> |
|
4896 13.2.tr |
|
4897 </h3> |
|
4898 </div> |
|
4899 </div> |
|
4900 </div> |
|
4901 <p id="ch11Section_5_1a5"> |
|
4902 <span class="emphasis"> |
|
4903 <em> |
|
4904 tr |
|
4905 </em> |
|
4906 </span> |
|
4907 (abbreviated from |
|
4908 <span class="emphasis"> |
|
4909 <em> |
|
4910 translate |
|
4911 </em> |
|
4912 </span> |
|
4913 or |
|
4914 <span class="emphasis"> |
|
4915 <em> |
|
4916 transliterate |
|
4917 </em> |
|
4918 </span> |
|
4919 ) is a command in Unix-like operating systems. |
|
4920 </p> |
|
4921 <p id="ch11Section_5_1a6"> |
|
4922 When executed, the program reads from the standard input and writes to the standard output. It takes as parameters two sets of characters, and replaces occurrences of the characters in the first set with the corresponding elements from the other set. For example, |
|
4923 </p> |
|
4924 <pre class="programlisting"> |
|
4925 $ tr 'abcd' 'jkmn' |
|
4926 </pre> |
|
4927 <p id="ch11Section_5_1a7"> |
|
4928 maps 'a' to 'j', 'b' to 'k', 'c' to 'm', and 'd' to 'n'. |
|
4929 </p> |
|
4930 <p id="ch11Section_5_1a8"> |
|
4931 Sets of characters may be abbreviated by using character ranges. The previous example could be written: |
|
4932 </p> |
|
4933 <pre class="programlisting"> |
|
4934 $ tr 'a-d' 'jkmn' |
|
4935 </pre> |
|
4936 <p id="ch11Section_5_1a9"> |
|
4937 In POSIX compliant versions of |
|
4938 <span class="emphasis"> |
|
4939 <em> |
|
4940 tr |
|
4941 </em> |
|
4942 </span> |
|
4943 the set represented by a character range depends on the locale's collating order, so it is safer to avoid character ranges in scripts that might be executed in a locale different from that in which they were written. Ranges can often be replaced with POSIX character sets such as [:alpha:]. |
|
4944 </p> |
|
4945 <p id="ch11Section_5_1aa"> |
|
4946 The |
|
4947 <span class="emphasis"> |
|
4948 <em> |
|
4949 -c |
|
4950 </em> |
|
4951 </span> |
|
4952 flag complements the first set of characters. |
|
4953 </p> |
|
4954 <pre class="programlisting"> |
|
4955 $ tr -cd '[:alnum:]' |
|
4956 </pre> |
|
4957 <p id="ch11Section_5_1ab"> |
|
4958 therefore removes all non-alphanumeric characters. |
|
4959 </p> |
|
4960 <p id="ch11Section_5_1ac"> |
|
4961 The |
|
4962 <span class="emphasis"> |
|
4963 <em> |
|
4964 -s |
|
4965 </em> |
|
4966 </span> |
|
4967 flag causes tr to compress sequences of identical adjacent characters in its output to a single token. For example, |
|
4968 </p> |
|
4969 <pre class="programlisting"> |
|
4970 $ tr -s '\n' '\n' |
|
4971 </pre> |
|
4972 <p id="ch11Section_5_1ad"> |
|
4973 replaces sequences of one or more newline characters with a single newline. |
|
4974 </p> |
|
4975 <p id="ch11Section_5_1ae"> |
|
4976 The |
|
4977 <span class="emphasis"> |
|
4978 <em> |
|
4979 -d |
|
4980 </em> |
|
4981 </span> |
|
4982 flag causes tr to delete all tokens of the specified set of characters from its input. In this case, only a single character set argument is used. The following command removes carriage return characters, thereby converting a file in DOS/Windows format to one in Unix format. |
|
4983 </p> |
|
4984 <pre class="programlisting"> |
|
4985 $ tr -d '\r' |
|
4986 </pre> |
|
4987 <p id="ch11Section_5_1af"> |
|
4988 Most versions of |
|
4989 <span class="emphasis"> |
|
4990 <em> |
|
4991 tr |
|
4992 </em> |
|
4993 </span> |
|
4994 , including GNU |
|
4995 <span class="emphasis"> |
|
4996 <em> |
|
4997 tr |
|
4998 </em> |
|
4999 </span> |
|
5000 and classic Unix |
|
5001 <span class="emphasis"> |
|
5002 <em> |
|
5003 tr |
|
5004 </em> |
|
5005 </span> |
|
5006 , operate on single byte characters and are not Unicode compliant. An exception is the Heirloom Toolchest implementation, which provides basic Unicode support. |
|
5007 </p> |
|
5008 <p id="ch11Section_5_1b0"> |
|
5009 Ruby and Perl also have an internal |
|
5010 <span class="emphasis"> |
|
5011 <em> |
|
5012 tr |
|
5013 </em> |
|
5014 </span> |
|
5015 operator, which operates analogously. Tcl's |
|
5016 <span class="emphasis"> |
|
5017 <em> |
|
5018 string map |
|
5019 </em> |
|
5020 </span> |
|
5021 command is more general in that it maps strings to strings while |
|
5022 <span class="emphasis"> |
|
5023 <em> |
|
5024 tr |
|
5025 </em> |
|
5026 </span> |
|
5027 maps characters to characters. |
|
5028 </p> |
|
5029 </div> |
|
5030 </div> |
|
5031 <div class="section" title="14.Elementary Regex"> |
|
5032 <div class="titlepage"> |
|
5033 <div> |
|
5034 <div> |
|
5035 <h2 class="title" style="clear: both"> |
|
5036 <a name="id2950613"> |
|
5037 </a> |
|
5038 14.Elementary Regex |
|
5039 </h2> |
|
5040 </div> |
|
5041 </div> |
|
5042 </div> |
|
5043 <p id="ch11Section_5_1b1"> |
|
5044 In computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters. A regular expression (often shortened to regex or regexp) is written in a formal language that can be interpreted by a regular expression processor, a program that either serves as a parser generator or examines text and identifies parts that match the provided specification. |
|
5045 </p> |
|
5046 <p id="ch11Section_5_1b2"> |
|
5047 Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, Perl, Ruby and Tcl have a powerful regular expression engine built directly into their syntax. Several utilities provided by Unix distributionsincluding the editor |
|
5048 <span class="emphasis"> |
|
5049 <em> |
|
5050 ed |
|
5051 </em> |
|
5052 </span> |
|
5053 and the filter |
|
5054 <span class="emphasis"> |
|
5055 <em> |
|
5056 grep |
|
5057 </em> |
|
5058 </span> |
|
5059 were the first to popularize the concept of regular expressions. |
|
5060 </p> |
|
5061 <p id="ch11Section_5_1b3"> |
|
5062 Traditional Unix regular expression syntax followed common conventions but often differed from tool to tool. The IEEE POSIX |
|
5063 <span class="emphasis"> |
|
5064 <em> |
|
5065 Basic Regular Expressions |
|
5066 </em> |
|
5067 </span> |
|
5068 (BRE) standard (released alongside an alternative flavor called Extended Regular Expressions or ERE) was designed mostly for backward compatibility with the traditional (Simple Regular Expression) syntax but provided a common standard which has since been adopted as the default syntax of many Unix regular expression tools, though there is often some variation or additional features. Many such tools also provide support for ERE syntax with command line arguments. |
|
5069 </p> |
|
5070 <p id="ch11Section_5_1b4"> |
|
5071 In the BRE syntax, most characters are treated as literals they match only themselves (i.e., a matches "a"). The exceptions, listed below, are called metacharacters or metasequences. |
|
5072 </p> |
|
5073 <table summary="Elementary Regex" border="1"> |
|
5074 <colgroup> |
|
5075 <col width="13"> |
|
5076 </col> |
|
5077 <col width="60"> |
|
5078 </col> |
|
5079 </colgroup> |
|
5080 </table> |
|
5081 <div class="section" title="14.1.Lazy quantification"> |
|
5082 <div class="titlepage"> |
|
5083 <div> |
|
5084 <div> |
|
5085 <h3 class="title"> |
|
5086 <a name="id2950823"> |
|
5087 </a> |
|
5088 14.1.Lazy quantification |
|
5089 </h3> |
|
5090 </div> |
|
5091 </div> |
|
5092 </div> |
|
5093 <p id="ch11Section_5_1c9"> |
|
5094 The standard quantifiers in regular expressions are greedy, meaning they match as much as they can, only giving back as necessary to match the remainder of the regex. For example, someone new to regexes wishing to find the first instance of an item between < and > symbols in this example: |
|
5095 </p> |
|
5096 <pre class="programlisting"> |
|
5097 Another whale explosion occurred on <January 26>, <2004>. |
|
5098 </pre> |
|
5099 <p id="ch11Section_5_1ca"> |
|
5100 ...would likely come up with the pattern <.*>, or similar. However, this pattern will actually return "<January 26>, <2004>" instead of the "<January 26>" which might be expected, because the * quantifier is greedy it will consume as many characters as possible from the input, and "January 26>, <2004" has more characters than "January 26". |
|
5101 </p> |
|
5102 <p id="ch11Section_5_1cb"> |
|
5103 Though this problem can be avoided in a number of ways (e.g., by specifying the text that is not to be matched: <[^>]*>), modern regular expression tools allow a quantifier to be specified as |
|
5104 <span class="emphasis"> |
|
5105 <em> |
|
5106 lazy |
|
5107 </em> |
|
5108 </span> |
|
5109 (also known as non-greedy, reluctant, minimal, or ungreedy) by putting a question mark after the quantifier (e.g., <.*?>), or by using a modifier which reverses the greediness of quantifiers (though changing the meaning of the standard quantifiers can be confusing). By using a lazy quantifier, the expression tries the minimal match first. Though in the previous example lazy matching is used to select one of many matching results, in some cases it can also be used to improve performance when greedy matching would require more backtracking. |
|
5110 </p> |
|
5111 </div> |
|
5112 </div> |
|
5113 <div class="section" title="15.One Liners"> |
|
5114 <div class="titlepage"> |
|
5115 <div> |
|
5116 <div> |
|
5117 <h2 class="title" style="clear: both"> |
|
5118 <a name="id2950898"> |
|
5119 </a> |
|
5120 15.One Liners |
|
5121 </h2> |
|
5122 </div> |
|
5123 </div> |
|
5124 </div> |
|
5125 <p id="ch11Section_5_1cc"> |
|
5126 A |
|
5127 <span class="emphasis"> |
|
5128 <em> |
|
5129 one-liner |
|
5130 </em> |
|
5131 </span> |
|
5132 is textual input to the command-line of an operating system shell that performs some function in just one line of input. |
|
5133 </p> |
|
5134 <p id="ch11Section_5_1cd"> |
|
5135 The one liner can be |
|
5136 </p> |
|
5137 <div class="orderedlist"> |
|
5138 <ol class="orderedlist" type="1"> |
|
5139 <li class="listitem"> |
|
5140 <p id="ch11Section_5_1ce"> |
|
5141 An expression written in the language of the shell. |
|
5142 </p> |
|
5143 </li> |
|
5144 <li class="listitem"> |
|
5145 <p id="ch11Section_5_1cf"> |
|
5146 The invocation of an interpreter together with program source for the interpreter to run. |
|
5147 </p> |
|
5148 </li> |
|
5149 <li class="listitem"> |
|
5150 <p id="ch11Section_5_1d0"> |
|
5151 The invocation of a compiler together with source to compile and |
|
5152 instructions for executing the compiled program. |
|
5153 </p> |
|
5154 </li> |
|
5155 </ol> |
|
5156 </div> |
|
5157 <p id="ch11Section_5_1d1"> |
|
5158 Certain dynamic scripting languages such as AWK, sed, and perl have traditionally been adept at expressing one-liners. Specialist shell interpreters such as these Unix shells or the Windows PowerShell, allow for the construction of powerful one-liners. |
|
5159 </p> |
|
5160 <p id="ch11Section_5_1d2"> |
|
5161 The use of the phrase one-liner has been widened to also include program-source for any language that does something useful in one line. |
|
5162 </p> |
|
5163 <p id="ch11Section_5_1d3"> |
|
5164 The word |
|
5165 <span class="emphasis"> |
|
5166 <em> |
|
5167 One-liner |
|
5168 </em> |
|
5169 </span> |
|
5170 has two references in the index of the book |
|
5171 <span class="emphasis"> |
|
5172 <em> |
|
5173 The AWK Programming Language |
|
5174 </em> |
|
5175 </span> |
|
5176 (the book is often referred to by the abbreviation TAPL). It explains the programming language AWK, which is part of the Unix operating system. The authors explain the birth of the One-liner paradigm with their daily work on early Unix machines: |
|
5177 </p> |
|
5178 <pre class="programlisting"> |
|
5179 The 1977 version had only a few built-in variables and predefined functions. It was designed for writing short programs [...] Our model was that an invocation would be one or two lines long, typed in and used immediately. Defaults were chosen to match this style [...] We, being the authors, knew how the language was supposed to be used, and so we only wrote one-liners. |
|
5180 </pre> |
|
5181 <p id="ch11Section_5_1d4"> |
|
5182 Notice that this original definition of a One-liner implies immediate execution of the program without any compilation. So, in a strict sense, only source code for interpreted languages qualifies as a One-liner. But this strict understanding of a One-liner was broadened in 1985 when the IOCCC introduced the category of Best One Liner for C, which is a compiled language. |
|
5183 </p> |
|
5184 <p id="ch11Section_5_1d5"> |
|
5185 The TAPL book contains 20 examples of One-liners (A Handful of Useful awk One-Liners) at the end of the book's first chapter. |
|
5186 </p> |
|
5187 <p id="ch11Section_5_1d6"> |
|
5188 Here are the first few of them: |
|
5189 </p> |
|
5190 <div class="orderedlist"> |
|
5191 <ol class="orderedlist" type="1"> |
|
5192 <li class="listitem"> |
|
5193 <p id="ch11Section_5_1d7"> |
|
5194 Print the total number of input lines: |
|
5195 </p> |
|
5196 <p id="ch11Section_5_1d8"> |
|
5197 END { print NR } |
|
5198 </p> |
|
5199 </li> |
|
5200 <li class="listitem"> |
|
5201 <p id="ch11Section_5_1d9"> |
|
5202 Print the tenth input line: |
|
5203 </p> |
|
5204 <p id="ch11Section_5_1da"> |
|
5205 NR == 10 |
|
5206 </p> |
|
5207 </li> |
|
5208 <li class="listitem"> |
|
5209 <p id="ch11Section_5_1db"> |
|
5210 Print the last field of every input line: |
|
5211 </p> |
|
5212 <p id="ch11Section_5_1dc"> |
|
5213 { print $NF } |
|
5214 </p> |
|
5215 </li> |
|
5216 </ol> |
|
5217 </div> |
|
5218 <p id="ch11Section_5_1dd"> |
|
5219 One-liners are also used to show off the differential expressive power of programming languages. Frequently, one-liners are used to demonstrate programming ability. Contests are often held to see who can create the most exceptional one-liner. |
|
5220 </p> |
|
5221 <p id="ch11Section_5_1de"> |
|
5222 The following example is a C program (a winning entry in the "Best one-liner" category of the IOCCC, here split to two lines for presentation).: |
|
5223 </p> |
|
5224 <pre class="programlisting"> |
|
5225 main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return |
|
5226 *t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);} |
|
5227 </pre> |
|
5228 <p id="ch11Section_5_1df"> |
|
5229 This one-liner program is a |
|
5230 <span class="emphasis"> |
|
5231 <em> |
|
5232 glob pattern matcher |
|
5233 </em> |
|
5234 </span> |
|
5235 . It understands the glob characters '*' meaning 'zero or more characters' and '?' meaning exactly one character, just like most Unix shells. |
|
5236 </p> |
|
5237 <p id="ch11Section_5_1e0"> |
|
5238 Run it with two args, the string and the glob pattern. The exit status is 0 (shell true) when the pattern matches, 1 otherwise. The glob pattern must match the whole string, so you may want to use * at the beginning and end of the pattern if you are looking for something in the middle. Examples: |
|
5239 </p> |
|
5240 <pre class="programlisting"> |
|
5241 $ prog foo 'f??'; echo $? |
|
5242 |
|
5243 $ prog 'best short program' '??st*o**p?*'; echo $? |
|
5244 </pre> |
|
5245 <p id="ch11Section_5_1e1"> |
|
5246 Here is a one line shell script to show directories: |
|
5247 </p> |
|
5248 <pre class="programlisting"> |
|
5249 $ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' |
|
5250 </pre> |
|
5251 </div> |
2150 </div> |
5252 </div> |
2151 </div> |
5253 </div> |
2152 </div> |
5254 </body> |
2153 </body> |
5255 </html> |
2154 </html> |