Linux terminal show files

What is the shell command to display contents of a file?

For viewing HTML files you can also use lynx , links , elinks or w3m which are text-mode browsers. They can also be used to view .txt files.

file : Display the type of file cat : Display the content of the file and outputs it on stdout.

You can use vi, emacs command to edit the file in Unix environment. If you do not have expertise in using vi/emacs you might find it little difficult to edit the file.

If you have X11 enabled, You can use a number of Linux editors like gvim, kate, kwrite, kdevelop etc.

Kwrite is my personal favorite in Linux.

Or, less or more . See the man pages for more information. 🙂

cat Works fine with txt or html. (or less or more if you want tosee it page by page) or any text ediotr. (vi, emcas, gedit. ).

Also know that if it’s a binary file it’s may contain control char that will do some displeasing things with your terminal (like changing charset). If that happen use reset to put it back in sane state.

You can also use file on file before displaying it’s content, the system will guess it’s type (based on content not filename name) and show it to you.

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

6 Ways to View Linux File Content

Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

One of the most important ways you can make use of your Linux PC is through files.

Almost every file that you directly work on in the Linux terminal can be manipulated as a text file. This includes configuration files, log files about various services and processes in the system, and script files that come bundled with the Linux Distribution of your choice or which you write yourself.

Knowing how to quickly view the content of these files in the Linux terminal is an invaluable skill that not only saves you time but also helps you learn more about the system. This tutorial will show you how to view these kinds of files in the Linux terminal.

cat

The cat command is a simple and extremely useful command for viewing file contents on Linux.

You can use the cat command to quickly print file content to the standard output in the terminal or alternatively concatenate the output.

The syntax for the cat command is as follows:

For example, if you have a file called simple-list.txt , which contains a list of some items, you can view the contents of this file by using the command

$ cat simple-list.txt bananas strawberries grapes apples watermelons oranges blueberries lemons peaches avocados pineapples cherries cantaloupe raspberries pears limes blackberries clementine mangoes plums 

You can also list multiple files at the same time to get the output of all files in the terminal at once, as shown below:

$ cat testfile1.txt testfile2.txt these are the contents of testfilel.txt. and this is the end of testfilel.txt these are the contents of testfile2.txt. and this is the end of testfile2.txt

nl

If you want to be able to see the line number for each line in a file, the nl command does exactly that. You can use this command in the same way as the cat command, and the only difference is that nl has line numbers enabled by default.

Читайте также:  Где лежат ярлыки linux

Using the cat command, you can get the same result with the -e option. Here is an example output of the same simple-list.txt file output with nl

$ nl simple-list.txt 1 bananas 2 strawberries 3 grapes 4 apples 5 watermelons 6 oranges 7 blueberries 8 lemons 9 peaches 10 avocados 11 pineapples 12 cherries 13 cantaloupe 14 raspberries 15 pears 16 limes 17 blackberries 18 clementine 19 mangoes 20 plums 

The line numbers can be changed to be left justified by using the -nln option. There are other options as well you can use with the nl command, listed under man nl

more

So far, you have needed to scroll through the output using your mouse scroll wheel or Shift+Page-up and Shift+Page-down buttons on the keyboard.

However, more command provides a much easier way to view longer files that do not fit completely within the terminal window.

The same simple-list.txt file is shown when viewed through more commands:

Navigation in the viewing window of more commands can be either line by line or page by page. You can use Enter key to navigate by one line at a time or the Space key to navigate by one page at a time.

The B key is used to go back to the previous page. More command also tells you when you reach the end of the file as well, as shown below:

You can quit the viewing window of more commands at any time by pressing q, which returns you to the terminal screen. In this way, more commands can be used to view long config or log files without breaking the flow of the terminal.

less

Less is a more modern take on more command. Back when fewer commands were introduced in the Linux ecosystem, most commands did not support scrolling back up.

In addition to letting users scroll up and down, less command also supports horizontal scrolling, supports better handling for binary files, and can perform a search through text.

Let’s look at our simple-list.txt file through less:

You can use arrow keys on the keyboard to navigate, in addition to the navigation keys for more commands. While you are in the viewing window of less, you can use /word to search through the file contents for word.

This is shown in the screenshot below where the user searches for apple in the file contents:

Less command also supports more advanced features such as being able to view file contents at the first occurrence of a specific word. To do this, the command syntax is as follows:

$ less +/target-word /path-to-file

For example, to open the apples.txt file at the first occurrence of the word cultivar, the following command would be used:

If you just want to quickly view the first ten lines of a file, you can do that through

Читайте также:  Astra linux apt команды

The head command as shown below:

$ head simple-list.txt bananas strawberries grapes apples watermelons oranges blueberries lemons peaches avocados

By default, only the first ten lines are shown in the terminal, but you can change the number of lines displayed by using the -n option, as shown below:

$ head -n 5 simple-list.txt bananas strawberries grapes apples watermelons

Similarly, the -c option can be used to print a specific number of bytes from the file to the terminal.

tail

The tail command works like the head command, with the only major difference being that it shows the last ten lines of the file instead of the first ten lines.

$ tail simple-list.txt cherries cantaloupe raspberries pears limes blackberries clementine mangoes plums tangerines 
$ tail -n 5 simple-list.txt blackberries clementine mangoes plums tangerines

In addition, both head and tail commands can be used with other file viewing commands shown in the tutorial to produce better output for users.

For example, the nl command can be used to first show the file with line numbers. Then the result can be piped to less to view the last three lines of the file, as shown below:

$ nl simple-list.txt | tail -n 3 19 mangoes 20 plums 21 tangerines 

Frequently asked questions about viewing Linux files

You can check the type of a file through the file command, the syntax of which is as follows:

How can I view the contents of a really big log file, for example, an apache or Nginx log file, to find a specific line?

You can combine multiple commands through piping to not only search through big log files but to display the search result in a well-organized output.

For example, using grep with nl and tail , you can find out when was the last few times a specific file was requested on your web server, as shown below:

$ nl /var/log/apache/mywebsite.log | grep target-file.jpg | tail -n 5

The above command will show the last file times target-file.jpg was requested on your web server.

The space key is used to scroll down a full page with more and fewer commands.

To scroll down line-by-line in less, use the Enter key on the keyboard.

To scroll back up a page, use the b key.

How can I search for a specific term or word in the viewing window of more or less command while I am reading?

To search for a specific word in the contents of the file you are currently viewing in more or less, use the syntax /word-to-search.

If I want to open an image or rich text document file through the terminal, what command can I use?

You can use the general command xdg-view , or any variant of it for the specific distribution you have installed, for example, gnome-view or kde-view , to open a rich text or image file.

This will open the file in the default application for the target file type.

Источник

5 Commands to View the Content of a File in Linux Command Line

Here are five commands that let you view the content of a file in Linux terminal.

If you are new to Linux and you are confined to a terminal, you might wonder how to view a file in the command line.

Reading a file in Linux terminal is not the same as opening file in Notepad. Since you are in the command line mode, you should use commands to read file in Linux.

Don’t worry. It’s not at all complicated to display a file in Linux. It’s easy as well essential that you learn how to read files in the line.

Here are five commands that let you view the content of a file in Linux terminal.

Читайте также:  Puppy linux eee pc 700

5 commands to view files in Linux

Before you how to view a file in Unix like systems, let me clarify that when I am referring to text files here. There are different tools and commands if you want to read binary files.

cat command example

Cat becomes a powerful command when used with its options. I recommend reading this detailed tutorial on using cat command.

The problem with cat command is that it displays the text on the screen. Imagine if you use cat command with a file that has 2000 lines. Your entire screen will be flooded with the 200 lines and that’s not the ideal situation.

So, what do you do in such a case? Use less command in Linux (explained later).

2. nl

The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.

nl command example

There are a few options with nl command that allows you to control the numbering. You can check its man page for more details.

3. Less

Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.

less command example

I strongly recommend learning a few options of the Less command so that you can use it more effectively.

There is also more command which was used in olden days but less command has more friendly features. This is why you might come across the humorous term ‘less is more’.

4. Head

Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.

Head command example

You can change this behavior by using options with head command but the fundamental principle remains the same: head command starts operating from the head (beginning) of the file.

5. Tail

Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.

By default, tail command displays the last 10 lines of a file.

Tail command in Linux

Head and Tail commands can be combined to display selected lines from a file. You can also use tail command to see the changes made to a file in real time.

Bonus: Strings command

Okay! I promised to show only the commands for viewing text files. And this one deals with both text and binary files.

The Strings command displays the readable text from a binary file.

No, it doesn’t convert binary files into text files. If the binary file consists of actual readable text, the strings command displays those text on your screen. You can use the file command to find the type of a file in Linux.

Conclusion

Some Linux users use Vim to view the text file. Of course, you can easily move from the beginning to the end of the lines and edit the file but it’s overkill for just reading a file. My favorite command to open a file in Linux is the less command. It leaves the screen clear and has several options that makes viewing text file a lot easier.

Since you now know ways to view files, maybe you would be interested in knowing how to edit text files in Linux. Cut and Paste are two such commands that you can use for editing text in Linux terminal. You may also read about creating files in Linux command line.

Which command do you prefer?

Источник

Оцените статью
Adblock
detector