- How can I display the contents of a text file on the command line?
- 9 Answers 9
- Using cat
- Using less
- Using od
- How to read file line by line in Bash script
- Example -1: Reading file content from command line
- Example -2: Reading file content using script
- Example -3: Passing filename from the command line and reading the file
- Example – 4: Reading file by omitting backslash escape
- About the author
- Fahmida Yesmin
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
How to read file line by line in Bash script
How would you write a Bash script that can process a text file one line at a time. First you need a syntax and approach to read the file line by line. The methods for this approach are shown in this tutorial.
Suppose, you have a file named company.txt which contents the company names. This file contains the following content.
Example -1: Reading file content from command line
Suppose, you want to read the file, company.txt, line by line from the command line without ‘cat’ command. Run the following command to do the task. while loop will read each line from the file company.txt in each step and store the content of the line in $line variable which will be printed later.
Example -2: Reading file content using script
Create a bash file and add the following code to read the content of a particular file. Here, an existing filename is stored in $filename variable and $n variable is used to keep the value of the line number of that file. Like previous example, while loop is used to read this file with line number.
#!/bin/bash
filename = 'company.txt'
n = 1
while read line; do
# reading each line
echo "Line No. $n : $line "
n =$ ( ( n+ 1 ) )
done < $filename
Run the following command to execute the script.
Run ‘cat’ command with company.txt file to display the original content of company.txt file.
Example -3: Passing filename from the command line and reading the file
Create a bash file and add the following script. This script will take the filename from the command line argument. First argument value is read by the variable $1 which will contain the filename for reading. If the file exists in the current location then while loop will read the file line by line like previous example and print the file content.
Run the above script with employee.txt file as argument value. The output will show the content of employee.txt file by removing extra space. You can show the original content of employee.txt file by using ‘cat’ command.
Example – 4: Reading file by omitting backslash escape
If you want to read each line of a file by omitting backslash escape then you have to use ‘-r’ option with read command in while loop.
Create a file named company2.txt with backslash and run the following command to execute the script. The output will show the file content without any backslash.
You will need to read the file for many programming purposes. For example, you can search or match any particular content easily from any file by reading each line separately. So, it is an essential task for any programming. Some simple examples of reading file in bash script are shown in this tutorial. These will help you to get the idea of reading file content line by line using while loop in bash script and apply in your script more efficiently. For more information watch the video!
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.