- Bash Cookbook
- Ron Brash Ganesh Naik
- 715字
- 2021-07-23 19:17:41
How it works...
Before proceeding, note that the contents of the loremipsum.txt file will be different for every instance downloaded. Lorem Ipsum is pseudo random text that is used in a variety of text-related duties, often as placeholder values because it looks to be a language of sorts and is useful where the human brain is disturbed by copy and paste stub text.
Great! Let's get started:
- In the first step, the commands should produce similar results to the following block (for brevity, we excluded much of the output to keep the recipe coherent), but notice that head begins at the beginning, or head, of loremipsum.txt, and tail begins at the end, or tail, of loremipsum.txt. When we specify the -n flag with a decimal number such as 1, both utilities will output a single line or whatever number of lines is entered:
$ cat loremipsum.txt
Feugiat orci massa inceptos proin adipiscing urna vestibulum
hendrerit morbi convallis commodo porta magna, auctor cras nulla ligula
sit vehicula primis ultrices duis rutrum cras feugiat sit facilisis
fusce placerat sociosqu amet cursus quisque praesent mauris facilisis,
egestas curabitur imperdiet sit elementum ornare sed class ante pharetra
in, nisi luctus sit accumsan iaculis eu platea sit ullamcorper platea
erat convallis orci volutpat curabitur nostra tellus erat non nisl
condimentum, cubilia lacinia eget rhoncus pharetra euismod sagittis
morbi risus, nisl scelerisque fringilla arcu auctor turpis ultricies
imperdiet nibh eget felis leo enim auctor sed netus ultricies sit fames
...
$ head loremipsum.txt
Feugiat orci massa inceptos proin adipiscing urna vestibulum
hendrerit morbi convallis commodo porta magna, auctor cras nulla ligula
sit vehicula primis ultrices duis rutrum cras feugiat sit facilisis
fusce placerat sociosqu amet cursus quisque praesent mauris facilisis,
egestas curabitur imperdiet sit elementum ornare sed class ante pharetra in
$ head -n 1 loremipsum.txt
Feugiat orci massa inceptos proin adipiscing urna vestibulum
$ tail loremipsum.txt
euismod torquent primis mattis velit aptent risus accumsan cubilia eros
justo ad sodales dapibus tempor, donec mauris erat at lacinia senectus
luctus venenatis mollis ullamcorper ante mollis nisl leo sollicitudin
felis congue tempus nam curabitur viverra venenatis quis, felis pretium
enim posuere elit bibendum dictumst, bibendum mattis blandit sociosqu
adipiscing cursus quisque augue facilisis vehicula metus taciti conubia
odio proin rutrum aliquam lorem, erat lobortis etiam eget risus lectus
sodales mauris blandit, curabitur velit risus litora tincidunt inceptos
nam ipsum platea felis mi arcu consequat velit viverra, facilisis
ulputate semper vitae suspendisse aliquam, amet proin potenti semper
$ tail -n 1 loremipsum.txt
ulputate semper vitae suspendisse aliquam, amet proin potenti semper
- In the second step, we discover a key difference between the head and tail commands. Tail is able to monitor a file while continuously dumping the tail contents of the file to standard out (stdout). If the file has a read error, or is moved/rotated out, -f (lowercase) will often stop outputting information, while -F will reopen the file and continue outputting the contents.
-F instead of -f is usually the desired option between the two if tailing system logs.
- With tail still running in continuous mode, several new entries should appear among the output. This sample is from when a system's wireless adapter was forced to reconnect to a standard access point (AP):
Dec 8 14:21:40 moon kernel: userif-2: sent link up event.
Dec 8 14:21:40 moon kernel: wlp3s0: authenticate with 18:d6:c7:fa:26:b0
Dec 8 14:21:40 moon kernel: wlp3s0: send auth to 18:d6:c7:fa:26:b0 (try 1/3)
Dec 8 14:21:40 moon kernel: wlp3s0: authenticated
Dec 8 14:21:40 moon kernel: wlp3s0: associate with 18:d6:c7:fa:26:b0 (try 1/3)
Dec 8 14:21:40 moon kernel: wlp3s0: RX AssocResp from 18:d6:c7:fa:26:b0 (capab=0x411 status=0 aid=1)
Dec 8 14:21:40 moon kernel: wlp3s0: associated
Dec 8 14:21:40 moon kernel: bridge-wlp3s0: device is wireless, enabling SMAC
Dec 8 14:21:40 moon kernel: userif-2: sent link down event.
Dec 8 14:21:40 moon kernel: userif-2: sent link up event.
- After killing the tail command, your console should be back at the prompt again: $.
- Running more and using the spacebar or Enter keys will progress output through the entire loremipsum.txt file. The more command is only able to view from beginning to end, and not back and forth.
- The less command is certainly more powerful and offers the user to be able to navigate through loremipsum.txt using several key combinations. It also offers search facilities among other features.
推薦閱讀
- Implementing VMware Horizon 7(Second Edition)
- Qt 5 and OpenCV 4 Computer Vision Projects
- Android項目開發入門教程
- Oracle Database In-Memory(架構與實踐)
- 人臉識別原理及算法:動態人臉識別系統研究
- C程序設計案例教程
- C語言程序設計案例精粹
- C語言程序設計與應用(第2版)
- Serverless Web Applications with React and Firebase
- Unity Character Animation with Mecanim
- Java高級程序設計
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐
- Python Linux系統管理與自動化運維
- 企業級Java現代化:寫給開發者的云原生簡明指南
- Dart:Scalable Application Development