- 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.
推薦閱讀
- C++案例趣學(xué)
- 復(fù)雜軟件設(shè)計之道:領(lǐng)域驅(qū)動設(shè)計全面解析與實(shí)戰(zhàn)
- 樂學(xué)Web編程:網(wǎng)站制作不神秘
- Swift 3 New Features
- Apache Karaf Cookbook
- MySQL數(shù)據(jù)庫基礎(chǔ)實(shí)例教程(微課版)
- Mastering Ext JS
- 零基礎(chǔ)學(xué)單片機(jī)C語言程序設(shè)計
- Oracle GoldenGate 12c Implementer's Guide
- Python網(wǎng)絡(luò)爬蟲技術(shù)與應(yīng)用
- Scala編程實(shí)戰(zhàn)
- Windows Phone 8 Game Development
- HTML5+CSS3+JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- 深入理解BootLoader
- Go語言從入門到精通