Linux Administrator

How to Display Contents of a File in CentOS/RHEL/Ubuntu

How to Display Contents of a File in CentOS/RHEL/Ubuntu. Whether you are a ordinary consumer or an experienced system administrator, finally, you’ll want to engage with file in Linux via the command line. For example, you may want to troubleshoot an trouble by means of checking the log files, viewing your system’s info, or maybe customizing it by way of editing the configuration files.

How to Display Contents of a File in CentOS/RHEL/Ubuntu

Knowing how to display the contents of a file in Linux can make your life less complicated and prevent time from constantly opening text editors. Many integrated capabilities make viewing files easy, rapid, and tailor-made in your wishes.

Cat

The most effective manner to view textual content files in Linux is the cat command. It displays the entire contents inside the command line without the usage of inputs to scroll via it.

Here is an instance of the usage of the cat command to view the Linux version via displaying the contents of the /proc/version file.

# cat /proc/version
Linux version 3.10.0-1160.36.2.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Wed Jul 21 11:57:15 UTC 2021

Head

Sometimes you also need to see the the first lines of a file. In that case, use the head command to view the primary ten lines of a file in Linux. For example, you can show primary facts approximately the CPU used by viewing the start of the /proc/cpuinfo files.

Like the tail command, use the –n flag with the head command to display desired number of lines , starting from the start of a given files. For instance, head -5 indicates the first five lines of a given file.

# head /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 85
model name	: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
stepping	: 7
microcode	: 0x5003005
cpu MHz		: 2499.996
cache size	: 36608 KB
physical id	: 0

Tail

Display contents of a file in CentOS/RHEL/Ubuntu is tail is a also good command. While the cat command is helpful while handling a small file, it isn’t always the pleasant manner to view huge log files. The tail command permits viewing the last ten lines of a file through default instead of filling your terminal window with a wall of textual content, making it the appropriate command to use in case you need to see the last log entries.

# tail /var/log/dmesg
[    6.500342] input: PC Speaker as /devices/platform/pcspkr/input/input4
[    6.514820] piix4_smbus 0000:00:01.3: SMBus Host Controller at 0xb100, revision 255
[    6.556234] parport_pc 00:03: reported by Plug and Play ACPI
[    6.566369] cryptd: max_cpu_qlen set to 1000
[    6.599945] type=1305 audit(1628251187.554:3): audit_pid=400 old=0 auid=4294967295 ses=4294967295 res=1
[    6.612008] AVX2 version of gcm_enc/dec engaged.
[    6.617829] AES CTR mode by8 optimization enabled
[    6.628713] ppdev: user-space parallel port driver
[    6.634078] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    6.640548] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)

Flags

You  can pick out what number of lines the command should show with the aid of passing the  –n flag (where n is an integer). For instance, the tail -n 15 command will output the last 15 lines on a given file.

Another helpful flag used with the tail command is –f. It outputs the ultimate ten lines of a file by default, but it also maintains displaying new entries because the document is up to date. This feature is beneficial while viewing the contemporary updates in log documents to troubleshoot an trouble. If you most effective need to apply this functionality, you could use the tail -f command, and it’ll best show the entries that regarded in the files after executing the command.

More

Another manner to view file contents in Linux is the more command. It shows a file inside the terminal, one page at a time. While the use of the more command, the Enter key scrolls through the report line through line, or the Space key scrolls one complete screen at a time. Finally, you may close the file by pressing the Q key.

Less

While more is a reachable command, it does come with a downside. After remaining a record, its contents live written within the terminal window, filling it with text, forcing customers to both clean the window or scroll back up to discover some thing. It also can be slow, because it masses the whole record though showing best one page at a time.

That’s wherein the much less command is available in handy. It’s very similar to more command, but with the advantage of now not preserving all the textual content within the terminal window. The less command additionally comes with a built-in search function, permitting you to focus on the components of the report for that you are looking. To search with much less, press the forward decrease key followed by using the text you want to go looking.

The following is what the search function looks as if if we look for /cpu inside the cpuinfo file.

Tac

Display contents of a file in CentOS/RHEL/Ubuntu. Another interesting way to show the contents of a file in Linux is in reverse order. To accomplish that, use the tac command. It is just like cat however reversed, reading and displaying the file starting from the last line. For instance, right here is what the output of tac looks like used to show the contents of the cpuinfo file.

# tac /proc/cpuinfo 

power management:
address sizes	: 46 bits physical, 48 bits virtual
cache_alignment	: 64
clflush size	: 64
bogomips	: 4999.99
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 ida arat pku ospke
wp		: yes
cpuid level	: 13
fpu_exception	: yes
fpu		: yes
initial apicid	: 1
apicid		: 1
cpu cores	: 1
core id		: 0
siblings	: 2
physical id	: 0
cache size	: 36608 KB
cpu MHz		: 2499.996
microcode	: 0x5003005
stepping	: 7
model name	: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
model		: 85
cpu family	: 6
vendor_id	: GenuineIntel
processor	: 1

power management:
address sizes	: 46 bits physical, 48 bits virtual
cache_alignment	: 64
clflush size	: 64
bogomips	: 4999.99

For higher clarity, pipe the tac command into much less to scroll via the file. Users accomplish piping using the desired command, the pipe with, and the other command. The syntax is as follows.

tac | less

Grep

While no longer used for showing the contents of a file, the grep command is on hand for filtering the output of commands. For example, grep works for looking for particular text in a file.

# grep test /var/log/dmesg
[    1.821355] MDS CPU bug present and SMT on, data leak possible. See https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/mds.html for more details.
[    1.895465] atomic64 test passed for x86-64 platform with CX8 and with SSE
[    6.634078] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    6.640548] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)

In addition, pipe the output of other commands thru grep, narrowing the search to what we’re searching out within the file.

Here is an example of piping the output from the head command into the grep command.

# head -20 /proc/cpuinfo |grep Intel
vendor_id	: GenuineIntel
model name	: Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz

head-command

Hope display contents of a file in CentOS/RHEL/Ubuntu article will help you.

You may also like:

FAQs:

How to view content of a file in Linux system?

To view the content of a file in Linux use: cat, tail, head, grep and tac command.

How to view file data in CentOS machine?

To view file data in CentOS machine use the less, cat, more, head, tail and nl command.

How to display output?

Print command is used to display output in the screen. Print command can be also used to display string and numeric expression.

What are the types of files in Linux?

In Linux the type of files recognized by the system are regular, directory and special.

Which command is used to display the output on screen?

F5 command is used to display the output command on the screen.

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment