How to Read Ascii a File in Linux
In Linux, we constantly work with text files such as configuration files, source codes, web pages, and many others. Hence, information technology is essential to take a quick view of the contents of a text file in the command line before editing the file.
This quick guide aims to show you various approaches you can use to list the contents of a text file in the terminal.
Beginning:
What is a text file?
The chances are high that you are familiar with a text file. However, to epitomize, a text file is a digital file that contains raw text; this ways the file should not contain whatever formatting such equally bold, italics, underline, or such. In add-on, text files do not take any form of media such equally pictures, videos, or audio.
By default, text files utilize the.txt extension. However, they take other forms such as source code in programming languages such as C (.c), C++ (.cpp, .h), Python (.py), and many more. Moreover, they practise not necessarily have to end with an extension. For instance, a configuration file such as /etc/vim/vimrc does not have any extension.
NOTE: Nosotros also call text files ASCII text files.
To view the file type in Linux, use the file control:
file /var/log/kern.log
/var/log/kern.log: ASCII text
# one – Cat
Cat is a popular and straightforward command for list the contents of a file to the terminal.
To use the cat command, laissez passer the proper name of the file to the true cat control equally:
Y'all can laissez passer the accented path to the file, every bit shown in the example above.
Cat is elementary yet powerful when used with other options. To learn how to use the cat control, read -> how to apply the true cat command.
When using the cat command to dump the contents of a large text file to the concluding, information technology will mess upwards your last, making it very hard to navigate.
To resolve such as event, we use the less control.
# 2 – Less
If yous have always read a transmission page for any Linux command, and so y'all have interacted with less.
Less allows y'all to view the contents of a file 1 folio at a time. Using the space central, y'all tin can scroll through the text file. Ii colons at the bottom of the screen bespeak each page of the text file.
For example, a large file such every bit /var/log/kern.log would not work out great with a true cat. To use less, nosotros practise:
Once you reach the end of the file, you lot can scroll upwards and down using the Up and Downwards arrow keys.
To quit the less command, press Q. Less exits its session without messing upward the terminal.
Combining less with a few options gives you control and functionality; for more, please read ->How to use less command with examples.
# 3 – More
Another command you can use to show the contents of a text file is the more command. More than is very similar to the less command; yet, it dumps the file's contents to the concluding and exits at the end of the file.
Here is an case: (Aforementioned command as the other one?)
# 4 –Head and Tail
Yeah, in that location are such commands. The head and tail commands are very similar and used to testify the first and last ten lines of a file, respectively.
However, you can modify how many first and concluding lines the head and tail control prints using the -due north option.
For case, to evidence the first xv lines of the kern.log file:
head -n xv /var/log/kern.log
Similarly, to prove the last fifteen lines of the kern.log file:
tail -n xv /var/log/kern.log
# five – Misc
If—for some reason—yous practice not accept either of the commands discussed above, you can use a text editor such as nano to show the contexts of a file. Still, this is more like editing the file than viewing the contents.
Hither is the control for that:
nano /var/log/kern.log
# Not sure why you want to edit a log file
Conclusion
Now you know how to brandish the contents of a file on the Linux Command line. Thank yous for reading.
Source: https://linuxhint.com/display-text-file-contents-linux/
Belum ada Komentar untuk "How to Read Ascii a File in Linux"
Posting Komentar