- How can I display the contents of a text file on the command line?
- 9 Answers 9
- Using cat
- Using less
- Using od
- Terminal Basics #5: View the File Contents in Linux
- Use cat command to display file content
- Using the less command to read large text files
- Head and tail to show part of text files
- Practice examples
- Test your knowledge
- What is the shell command to display contents of a file?
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
How can I display the contents of a text file on the command line?
I would like to display the contents of a text file on the command line. The file only contains 5-6 characters. Is there an easy way to do this?
9 Answers 9
Using cat
Since your file is short, you can use cat .
Using less
If you have to view the contents of a longer file, you can use a pager such as less .
You can make less behave like cat when invoked on small files and behave normally otherwise by passing it the -F and -X flags.
I have an alias for less -FX . You can make one yourself like so:
If you add the alias to your shell configuration, you can use it forever.
Using od
If your file contains strange or unprintable characters, you can use od to examine the characters. For example,
$ cat file (ÐZ4 ?o=÷jï $ od -c test 0000000 202 233 ( 320 K j 357 024 J 017 h Z 4 240 ? o 0000020 = 367 \n 0000023
Does less have any clear advantages over other pager programs like pg , or does it just boil down to personal preference?
@SamWeinberg: less has more features than pg . Take a look at the less(1) and pg(1) manpages. There are other pagers as well. Take a look at unix.stackexchange.com/questions/81129/…. less is probably the most widely-used pager but which one you use comes down to personal preference.
Even though everybody uses cat filename to print a files text to the standard output first purpose is concatenating. From cat’s man page:
cat — concatenate files and print on the standard output
Now cat is fine for printing files but there are alternatives:
The ( ) return the value of an expression, in this case the content of filename which then is expanded by $ for echo or printf .
This does exactly what you want and is easy to remember.
Here is an example that lets you select a file in a menu and then prints it.
#!/bin/bash select fname in *; do # Don't forget the "" around the second part, else newlines won't be printed printf "%s" "$(<$fname)" break done
Your update: " < filename is exactly what you want, . " is misleading. Overall, although this is an interesting discussion on alternatives, I think cat is simpler.
Tools for handling text files on unix are basic, everyday-commands:
In unix and linux to print out whole content in file
You can use following command to display content of a text file.
One option is to use more
However it does not have all the feature added by less .
One simple example is that you can't scroll back up in the output. Generally it has been superceeded by less - which was named in jest because
I always use $ less "your file here" , as it is very simple, provides a built in interactive grep command, and gives you an easy to use interface that you can scroll with the arrow keys.
(It is also included on nearly every *nix system)
less is the overkill-version of more (compare man less with man more ), and for me it has two annoying features: 1) it switches to the alternate screen buffer, when less terminates, the file you were just viewing vanishes 2) at EOF you have to explicitly type q (I know, there's an option for this). So one of my first actions in a new environment is setting export PAGER=/bin/more in my profile and use more all the time.
@ott--: 1) Try out the -X flag. 2) Try out the -E flag. less has a more emulation mode. You can enable it by setting the LESS_IS_MORE environmental variable. You can scroll upwards in the more emulation mode.
Though, in general, I do agree that less is overly-complicated. Its ability to run external commands is a perfect example of its over-complexity.
@EvanTeitelman I've always found the grep functionality extremely useful myself, @ott-- I find that because of it's emulation of more , and it's many additional features, it does the job very well.
If its a large file, and you want to search some specific part, you can use
cat filename | grep text_to_search -ni
Also you can use more interactive Vim editor (or vi editor if you do not have Vim):
vim filename Or vi filename
Vim/vi is a great editor, can also be used as a reader in "Normal Mode" or using -R option, it has many features that will help you in browsing through the file.
Shorter for vim -R is view . But keep in mind that it not likes redirections, as discussed in xargs and vi - “Input is not from a terminal”.
thanks @manatwork for the heads up! I have recently started using Vim and I like it because of its several features. Regarding redirections, I forgot about that, thanks for reminder. As of now, I am working on a remote VM, where I use ssh without GUI interface, therefore, Vim is of great use, when any other GUI editor cannot work, that's why I emphasized Vim here.
Use cat command to display the content of filename.
Use vim command to edit file.
Shall I compare thee to a summer’s day? Thou art more lovely and more temperate. Rough winds do shake the darling buds of May, And summer’s lease hath all too short a date. Sometime too hot the eye of heaven shines, And often is his gold complexion dimmed; And every fair from fair sometime declines, By chance or nature’s changing course untrimmed. But thy eternal summer shall not fade Nor lose possession of that fair thou ow’st, Nor shall Death brag thou wand’rest in his shade, When in eternal lines to time thou grow’st. So long as men can breathe or eyes can see, So long lives this, and this gives life to thee.
Clearly, cat is going to be the most popular answer to this question, but the code examples above will also provide the desired output (file courtesy of Shakespeare, via Project Gutenberg). However learning basic one-liners using Perl and/or Raku has its merits, simply because you can get an awful lot of work done with them.
Grep through a file, return matching lines:
~$ #Perl: ~$ perl -ne 'print if /eternal/' Sonnet_18.txt But thy eternal summer shall not fade When in eternal lines to time thou grow’st. ~$ #Raku: ~$ raku -ne '.put if /eternal/' Sonnet_18.txt But thy eternal summer shall not fade When in eternal lines to time thou grow’st.
Substitute one bit of text with another, redirect output to a new file:
~$ #Perl: ~$ perl -pe 's/eternal/forevermore/g' Sonnet_18.txt > new_sonnet.txt ~$ #Raku: ~$ raku -pe 's:g/eternal/forevermore/' Sonnet_18.txt > new_sonnet.txt
Terminal Basics #5: View the File Contents in Linux
In this chapter of the Terminal Basics series, you'll learn about viewing the contents of files in the Linux command line.
You learned to create new files in the previous chapter of the Terminal Basics series. In this chapter, you'll learn to read the files. I'll be discussing the most common Linux commands to display the contents of a text file. Before you do that, let's create our 'playground' with sample files. Let's create a directory first and switch to it.
mkdir display_files && cd display_files
And then, create a new file named columbo.txt with the following text (use the cat command with >> as discussed in the previous chapter):
Prescription: Murder Ransom for a Dead Man Murder by the Book Death Lends a Hand Dead Weight Suitable for Framing Lady in Waiting Short Fuse Blueprint for Murder
You don't have to type it all by yourself. You can copy-paste in the terminal using Ctrl+Shift+V. Most terminals support this shortcut. With things set, let's see various ways of viewing files in the Linux terminal.
Use cat command to display file content
The cat command is the most popular method to view files in Linux. It is dead simple to use. Just give it the file name and it displays the file content on the screen. Things cannot go simpler than this.
This is the output it shows:
Optional challenge: Use the cat or echo command with >> redirection to add a new line with "Etude in Black" text to the columbo.txt file. Refer to the previous chapter if you need help.
Using the less command to read large text files
The cat command is so simple. In fact, it is too simple. And simple doesn't work in complicated scenarios. Try using the cat command to view the content of the services file.
This services is a huge file with hundreds of lines. When you use cat, it floods the entire screen with the entire text. This is not ideal. Can you read the first line of the file? Yes, you can but you have to scroll all the way up. If the file has thousands of lines, you won't even be able to scroll back to the first few lines. This is where the less command comes into the picture. It lets you read the contents of a file in a page-by-page manner. You exit the viewing mode and your terminal screen is clean as ever. Use the less command to read the services file:
Now you are in a different viewing mode. You can use the arrow keys to move line by line. You can also use the Page Up and Page Down keys to move up and down by pages. You can even search for certain text using /search_term. When you are done reading the file, press Q key to exit the less view and go back to the normal terminal viewing. This table will help you use less:
Keys | Action |
---|---|
Up arrow | Move one line up |
Down arrow | Move one line down |
Space or PgDn | Move one page down |
b or PgUp | Move one page up |
g | Move to the beginning of the file |
G | Move to the end of the file |
ng | Move to the nth line |
/pattern | Search for pattern and use n to move to next match |
q | Exit less |
From viewing files in real time to bookmarking text, less can do a lot more. Read this to learn more about it.
Head and tail to show part of text files
If you only want to see certain parts of the text file in cat-styled display, use the head and tail commands. By default, the head command displays the first 10 lines of a file.
Practice examples
#create or clear the content of the file echo -n > sample #put content to the file for i in do echo "This is the line $i" >> sample done
Create a new file named script.sh and copy-paste the above script content into it. Now run the script like this to generate your sample file:
Now, you have got a file named sample that contains lines like "This is the line number N" for every 70 lines.
Let's take it to the next level. You can combine them both to show specific lines of a file. For example, to show lines from 35 to 40, use it like this:
head -n 40 filename | tail -n +35
Here:
- head -n 40 filename will display the first 40 lines of the file.
- tail -n +35 will display the lines from the 35th line to the end of the output from the head command. Yeah! Mind the + sign that changes the normal behavior of the tail command.
You can also combine them to show only a particular line. Let's say you want to display the 55th line; combine head and tail like this.
head -n 55 filename | tail -n 1
- head -n 55 filename will display the first 55 lines of the file.
- tail -n 1 will display the last line of the output from the head command, which will be the 55th line of the file.
Test your knowledge
Time for you to exercise your grey cells and practice what you learned in this chapter.
- Use the same sample file and display lines from 63 and 68.
- Now display the lines from 67 to 70.
- How about displaying the first line only?
- What do you see in the /etc/passwd file? Display its content.
That's it for this chapter. Next, you'll learn about removing files and folders in the command line. Stay tuned.
What is the shell command to display contents of a file?
For viewing HTML files you can also use lynx , links , elinks or w3m which are text-mode browsers. They can also be used to view .txt files.
file : Display the type of file cat : Display the content of the file and outputs it on stdout.
You can use vi, emacs command to edit the file in Unix environment. If you do not have expertise in using vi/emacs you might find it little difficult to edit the file.
If you have X11 enabled, You can use a number of Linux editors like gvim, kate, kwrite, kdevelop etc.
Kwrite is my personal favorite in Linux.
Or, less or more . See the man pages for more information. 🙂
cat Works fine with txt or html. (or less or more if you want tosee it page by page) or any text ediotr. (vi, emcas, gedit. ).
Also know that if it's a binary file it's may contain control char that will do some displeasing things with your terminal (like changing charset). If that happen use reset to put it back in sane state.
You can also use file on file before displaying it's content, the system will guess it's type (based on content not filename name) and show it to you.
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.