
How to Read Contents of Text File in Linux
To read the contents of a text file in Linux, you can use several command-line tools. Here are a few commonly used commands:
1. **cat**: The `cat` command is used to concatenate and display the contents of files. To read the contents of a text file, you can simply run:
```
cat filename.txt
```
2. **less**: The `less` command allows you to view text files interactively. It provides the ability to scroll up and down through the file. To read a text file using `less`, enter:
```
less filename.txt
```
Once in `less` mode, you can use the arrow keys to navigate through the file. Press 'q' to exit `less`.
3. **more**: Similar to `less`, the `more` command is used to view files one page at a time. To read the contents of a file with `more`, use the following syntax:
```
more filename.txt
```
Press the spacebar to scroll down one page at a time. Press 'q' to exit `more`.
4. **head**: If you only want to read the beginning (head) of a file, you can use the `head` command. By default, it shows the first 10 lines, but you can specify a different number with the `-n` option. For example, to display the first 5 lines of a file:
```
head -n 5 filename.txt
```
5. **tail**: Conversely, the `tail` command shows the end (tail) of a file. By default, it displays the last 10 lines, but you can also specify a different number with the `-n` option. For example, to display the last 5 lines of a file:
```
tail -n 5 filename.txt
```
These commands provide different ways to read the contents of a text file in Linux. Choose the one that best suits your needs.
Date: 20 June 2023 Comments: 0