Viewing text files on Linux

来源:互联网 发布:aris visio 软件 编辑:程序博客网 时间:2024/06/06 09:14

A lot of times, we have the need to view text files on a Linux box, whether it be logs or scripts or what have you. This post is more like a cheat-sheet for me on using thecat, head, tail, moreand less commands, but with examples and some screenshots.

Short version

  1. head <filename>- View the top few lines of a file
    • -<lines> - Displays the first <lines> number of lines of a file
  2. tail <filename> - View the bottom few lines of a file
    • -<lines> - Displays the last <lines> number of lines of a file
    • -f : continually watch for any additions at the end of the file
    • -f --pid=PID - continually display any additions until process with PID terminates
    • -f -s <sec> - continually display any additions at intervals of<sec> seconds
  3. cat <filename> - View the whole file
    • -n : line-numbered output
    • -b : line-numbered output with no line numbers for blank lines
    • -s : multiple blank lines compressed into a single blank line
  4. more <filename> - View the whole file, one screenful at a time.
    • spacebar : View next screen
    • b : View previous screen
    • d : View next half-screen
    • Enter : View next line
    • = : Current line number in file
    • v : Startvi editor on current line
    • /string : Search for string in file
      • n : Go to next occurrence of string
      • ' : Go to first occurrence of string
  5. less <filename>- Same asmore, but with many more features. Displays the portion of the file without waiting for the entire file to be read by it. Accepts most of themorecommands.
    • Pg Dn : View next screen
    • Pg Up : View previous screen
    • Up arrow : View previus line
    • Down arrow : View next line

Long Version

  1. head

    You can use the head command to see the first few lines of a text file. This can come in handy especially when you have a long file and want to see its title or comments present at the top and don't want to scroll all the way there.

    nitin@jane-saucy:~$ head long_test.txt ## A sample "Lorem Ipsum" dummy text file for testing purposes#Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus.Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.

    You can use the -<lines> switch to see a particular number of lines starting from the first line of the file.

    nitin@jane-saucy:~$ head -5 long_test.txt ## A sample "Lorem Ipsum" dummy text file for testing purposes#Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.
  2. tail

    tail lets you see the last few lines of a text file. This comes in handy when you're checking log files, etc.

    nitin@jane-saucy:~$ tail long_test.txtNulla facilisi.Praesent neque justo, accumsan nec elit eu, gravida sagittis leo.Vivamus id aliquet lacus.Cras viverra, metus quis dignissim gravida, mi odio porta tortor, ut venenatis ipsum ipsum sit amet nibh.Integer in magna in nibh facilisis porta in ut libero. Nam vestibulum eros at consequat euismod.

    The -f switch let's you monitor a file for lines being appended to the file. The-f --pid=<PID> continually monitors a file until process with the PID terminates. The-f -s <sec>continually displays additions if any at intervals of <sec> seconds.

    Sorry about not being able to show you examples for the-f switch. It works better when you watch it live in action.

  3. cat

    Although you generally use cat to concatenate strings to other strings or append them to files and what not, just typingcat <filename> displays the contents of a file on the screen.

    nitin@jane-saucy:~$ cat test.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit.Suspendisse lacinia.

    You can also view the file with line numbers using-n switch.

    nitin@jane-saucy:~$ cat -n long_test.txt      1  #     2  # A sample "Lorem Ipsum" dummy text file for testing purposes     3  #     4       5  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.      6       7  Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus.      8       9  Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.

    You can also view the files with line numbers only for the lines containing text with the-bswitch.

    nitin@jane-saucy:~$ cat -b long_test.txt 1 # 2 # A sample "Lorem Ipsum" dummy text file for testing purposes 3 #

     4  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit. 5  Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus. 6  Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat. 7  Quisque et tortor nisl. Nunc sit amet pretium mi. Donec fringilla tellus vitae justo aliquet fermentum. 8  Praesent tristique feugiat diam, id tempus dolor rhoncus a. 9  Ut ornare ligula nunc, eu faucibus enim bibendum sit amet. Quisque dignissim semper ultrices.

    You can also compress multiple blank lines into a single blank line using the-s switch. Here's a comparison,

    nitin@jane-saucy:~$ cat -n blank_test.txt      1  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.      2       3       4       5       6       7  Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus.      8       9      10      11      12      13  Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.nitin@jane-saucy:~$ cat -ns blank_test.txt      1  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin at tincidunt elit.      2       3  Maecenas leo lectus, blandit sit amet enim ac, viverra convallis felis. Donec lacinia cursus turpis et dapibus.      4       5  Praesent euismod ullamcorper venenatis. Etiam vestibulum est vitae sem consectetur feugiat.
  4. more

    You can use more to read through a file right from the beginning. Like,tail -f, it's better to see this in action yourself than me using screenshots, so I leave that up to you. I will however provide a few screenshots where possible.

    Navigating

    • spacebar lets you view the next screenful of text
    • b View previous screenful of text
    • d lets you view the next half-screen of text
    • Enter lets you view the next line of text
    • = displays the current line number in file

    Searching

    To search for a string whenmore is running, type a / followed by the string. For example, if I wanted to search for the wordet, I'd type /et and hit enter. To go to the next occurrence of the string, hit then key and to find the first occurrence of the string, hit the ' key.

    Opening vi text editor at a line

    To open the vi text editor at a particular line, hit thev key.

  5. less

    less takes the most of commands thatmore does but also packs a more powerful punch. With less you don't have to wait for the file to be completely read byless and can start viewing the content that's already been buffered.

    Some easier navigation in theless tool are as follows.

    • Pg Dn lets you view next screen
    • Pg Up lets you view previous screen
    • Up arrow lets you view previous line
    • Down arrow lets you view next line

https://2buntu.com/articles/1491/viewing-text-files-on-linux-cat-head-tail-more-and-less/

原创粉丝点击