Show file contents linux

How to display the contents of a text file on the terminal in Linux/Ubuntu

In this short tutorial, we cover see some simple commands that will help users learn how to open a file in linux or view its contents on the command line.

Using less

This is a pager which is useful to view long files for instance:

For instance the command below will display the content of the file output_report.txt :

Linux view text file

By passing the -F and -X switches, less can behave like cat when applied to small files but will act normally otherwise.

For instance :
less -X new_file

Using cat

applying cat to the same file that we used above, .i.e. :

Linux show file content

Using Echo

The parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name .

Linux display file contents

Using printf

Much like the previous command, the parenthesis ( ) are used to return the value of a given expression which in this case, is the contents of file_name .

Using tail

To display the last few lines

applying tail to the file output_in_html will give the following result :

which displays the last lines of the provided file.

Using head

Used to display the first few lines.

Applying head to the file output_in_html will give the following result :

Using more

The more command enables you to display the contents of text files in a scrollable manner. The text is shown one screenful at a time. You also have the possibility to scroll forwards and backwards through the text. You can even perform a search on the text.

Linux command to display contents of a file

Will show the following :

  • Space key : Used to scroll the display, .i.e. one screenful at a time
  • Enter key : Used to scroll the display one line
  • b key : Used to scroll the display backwards one screenful at a time
  • / key : Used to search the a given pattern much like in the vi/vim editor

Conclusion :

You have seen some simple utilities that allow you to display a text file on the command line. For small files, you can use the less command, or the echo command. If you know other similar tools, do not hesitate to write them in the comments section below.

If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

Читайте также:  Как клонировать репозиторий с gitlab ssh linux

Источник

Show contents of many files

From the command line, what is the easiest way to show the contents of multiple files? My directory looks like below.

Now I’d like to view the contents of all files except .htaccess . It might look something like:

I think this should be doable with a combination of find, xargs and cat, but I haven’t figured out how. Thanks for your time!

6 Answers 6

The two standard commands head and tail print a header with the file name if you pass them more than one file argument. To print the whole file, use tail -n +1 (print from the first line onwards, i.e. everything).

Here, it looks like you want to see every file except the single one whose name begins with a dot. Dot files are “hidden” under unix: they don’t appear in the default output of ls or in a wildcard match. So matching every non-hidden files is done with just * .

(Strictly speaking, tail -n +1 — * is needed in case one of the file names begins with a — .)

You can do it all in one with find :

That tells find to find all files ( -type f ) in the current directory ( . ) except ( -not ) one named .htaccess ( -name .htaccess ). Then it prints ( -printf ) a newline followed by the filename ( %p ), and then runs cat on the file ( -exec cat <> \; ). That will give you output like:

If you do this often it might be worth sticking it in a shell script or a function; I have one named cats that does exactly that:

It loops over each filename argument, prints out the filename (in bold green), and then cats the file:

Источник

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

Like that of .txt or .html

9 Answers 9

easiest are cat , head , tail , tac (for reverse output)

less and more let you scroll the text files ( less being more powerful)

you can also use text editors like nano , pico , vi , emacs

cat and less are good for this.

To Display the contents of a file use this command:

you can use cat , although its really meant for concatenation of files. more or less are 2 other tools you can use. Other’s include awk, eg using awk

using head/tail to display some parts of the file

Tools aside, you can also use just the shell to display your file

Try cat , head or tail .

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.

Читайте также:  Встроенные шрифты windows 10

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.

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 © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

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

Источник

how to use echo command to print out content of a text file?

I am trying to figure out what is the usage of this command:

According to text book it should redirect a programs standards input. Now I am redirecting a.txt to echo but instead of printing the content of the file it is printing out one empty line! Appreciate if anyone display this behaviour.

5 Answers 5

echo doesn’t read stdin so in this case, the redirect is just meaningless.

echo «Hello» | echo

To print out a file just use the command below

use below command to print the file content using echo,

here you can also get benefit of all echo features, I most like the removing of trailing newline character, (to get exact same hash as that of buffer and not the file)

In Unix, I believe all you have to do, assuming you have a file that isn’t hefty is: cat

No echo required.

cat command will display the file with CR or return:

you could use echo command with cat as command substitution. However, it will replace CR or return (unix: \n) with spaces:

Could be an interesting feature if you want to pipe to further data processing though. E.g. replacing spaces with sed command.

Not the answer you’re looking for? Browse other questions tagged unix or ask your own question.

Hot Network Questions

Subscribe to RSS

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

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

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

Источник

Get a list of all files in folder and sub-folder in a file

How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?

Читайте также:  Линукс для ноутбук леново

7 Answers 7

You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:

this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.

You can list directories other than the current one by specifying the full path eg:

will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.

You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:

Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.

Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use

Check the find manual manpage for the find command

Also check find GNU info page by using info find command in a terminal.

An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.

will give you the same as tree using other characters for the lines.

to display hidden files too

to not display lines

  1. Go to the folder you want to get a content list from.
  2. Select the files you want in your list ( Ctrl + A if you want the entire folder).
  3. Copy the content with Ctrl + C .
  4. Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.

This method will not include subfolder, content though.

You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).

You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .

(You can also get a list of files with their sizes by using ls -shR

Источник

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