How Do I Read a Text File in Linux
Open File in Linux
There are diverse means to open up a file in a Linux system. It is a fairly direct forward process to view the contents of a file, but if you are a new user, it may bother you. It is not as easy as opening a file in Notepad. From the Linux terminal, you must accept some exposures to the Linux basic commands. There are some commands such as cat, ls, that are used to read files from the last.
In Linux, we tin display various file formats such equally text file, audio files, video, paradigm, physician, pdf, or whatever other file contents.
Post-obit are some useful means to open a file from the concluding:
- Open the file using cat control.
- Open up the file using less command.
- Open the file using more than command.
- Open the file using nl command.
- Open up the file using gnome-open command.
- Open up the file using head control.
- Open the file using tail command.
1. Open File Using cat Control
This is the about pop and like shooting fish in a barrel way to display the file content. It just prints the file content to the final. It provides many options to make it more specific. To go in-depth with cat command, visit Linux cat.
To brandish the file content, execute the cat command every bit follows:
Let'due south create a file to understand how to open up a file. Execute the beneath command:
The in a higher place command will create a text file 'Test.txt.' There are multiple ways to create a file, To larn, visit Linux Create File.
To display the file content of the to a higher place file, execute the control as follows:
cat Text.txt
Consider the below output:
The cat command is a very useful utility to open a file, but the main outcome with it that it displays the file content on the terminal. If we take a big file, then information technology is not an ideal way to open a file. In such a state of affairs, the less command will exist handy as information technology displays a page at a time.
2. Open File Using less Control
The less command allows us to view 1 page at a time.
To display the file content, execute the less command equally follows:
The above command volition brandish the file content as a folio at a fourth dimension. To view more, ringlet the output. Consider the below output:
To leave from the less command, press the 'q' central, and you will exist back to the terminal. To get in-depth with less control, visit Linux less.
3. Open File Using more Command
The Linux more command is also used to display the file content. As the less command automatically adjusts the peak and width of the terminal window, it cuts the content every bit the width of the final.
Utilise the following keys to gyre the output:
Press ENTER key to scroll down folio line by line.
Press the Infinite bar to go to the side by side page.
Press 'b' central to get to the dorsum page.
Printing '/key:' to search the string.
To display the file content by the more control, execute information technology as follows:
The above command will display the file content equally the width of the final window. Consider the below output:
To become in-depth with the more control, visit Linux more than.
iv. Open up File Using nl Command
The nl command displays the file content with the line number. Information technology is near the same equally the cat command. The major departure between both commands is that it prepends line numbers while displaying the output in the terminal.
To display the file content with nl command, execute the command as follows:
The higher up command will display the file content with the line number. Consider the beneath output:
five. Open File Using gnome-open Command
For the GNOME-based Linux surroundings, we can utilize the gnome-open command to open a file.
If the gnome utility is not installed on our motorcar, we tin can install it by using the following control:
Execute the in a higher place command and provide the system administrator password to install the GNOME utility.
Now, to display the file content, execute the command as follows:
The higher up control will open the file with the default file system. Consider the below snap of the file system:
At that place are some other pop Linux environments, such as KDE DESKTOP, Os Ten UNIX, and more.
For KDE Desktop, execute the command every bit follows:
or,
For OS Ten UNIX, execute the command as follows:
6. Open up File past Using caput Command.
We tin display the file content by using the head command, but it is slightly dissimilar than others. It displays the start part of files via standard input. By default, information technology displays the first ten lines of the files. It starts reading the file from the caput (first line).
To open a file by using the head command, execute the command every bit follows:
The above command will brandish the content of the specified file. To go in-depth with the head command, visit Linux Head.
vii. Open up the file by Using tail Command.
The tail command is similar to the head command. The departure betwixt both commands is that it starts reading the file from the tail (terminal line). Similar to head command, it also displays the output of the last 10 lines by default.
To open the file by using the tail command, execute the command as follows:
The higher up command volition display the content from the last line of the specified file. To become in-depth with the tail command, visit Linux Tail.
Source: https://www.javatpoint.com/open-file-in-linux
0 Response to "How Do I Read a Text File in Linux"
Post a Comment