Display file content
Displaying the contents of files
In this tutorial, we will look at different ways to display the contents of files. Here is a list of most popular command to display the file's contents:
||Display the contents of file.|
||Browse through a text file.|
||More features than more.|
||Output the beginning (or top) portion file.|
||Output the ending (or bottom) portion of file.|
You can use the
cat command to display the entire contents of a file. The
more command will allow you to browse through a file or page through a file.
less command is like
more except is has more features,
less is more. When using the commands
less, use the Space bar to advance the next page. Use the Enter key to advance once line and type q to quit viewing the file. The commands are based on the vim editor .
|Page Up or b||Scroll back on page|
|Page Down or space||Scroll forward on page|
|Up Arrow||Scroll up one line|
|Down Arrow||Scroll down one line|
|G||Move to the end of the text file|
|1G or g||Move to the beginning of the text file|
|/characters||Search forward to the next occurrence of characters|
|n||Search for the next occurrence of the previous search|
|h||Display help screen|
Head and Tail
tail only display 10 lines. If you want to see the last 15 lines of a file, you need to specify the
-n and n is the number of lines.
$ tail -15 file.txt
Viewing files in Real Time
cat command can be a fine way to view files that have static content. However, if you are trying to keep up with changes that are being made to a file in real time,
cat is not your choice. You should use
tail -f filename . You would use this to look at files that change often, for examples, log files. You may wanna start a program and look at the programe's log file to see exactly what it is doing.
Suppose there is a file test.txt with the following content:
line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12
To look at this file with the
$ cat test.txt line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12
head against it, you will see the top 10 lines:
$ head test.txt line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10
If you only want to see the top 2 lines, you can type:
$ head -2 test.txt line 1 line 2
tail command will display the last 10 lines:
$ tail test.txt line 3 line 4 line 5 line 6 line 7 line 8 line 9 line 10 line 11 line 12
To look at the last line:
$ tail -1 test.txt line 12
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync