How To View a File(Text or PDF) in Linux

This post will guide you how to view a text file from the command line interface under Linux operating system. How do I open a PDF format file using a command at the shell prompt in your Linux system.

File View is one of the very common operation in Linux which help you to view file contents. And you can use one of the following command to view a text file or any other file from the command line of your Linux system.

Cat: concatenate files and print on the standard output
Less: display a file contents in screen page wise.
more: display a file contents in screen page wise.
head: output the first part of files
tail: output the last part of files
xdg-open: opens a file or URL in the user’s preferred application

view a file linux1

View a Text File Using Cat Command


If you want to view a text file called myshell.sh using cat command at the shell promt, type:

$ cat myshell.sh

outputs:

[root@mydevops ~]# cat myshell.sh
#! /bin/bash

echo "this is the first bash shell script\n"
df -h

View a Text File Using Less or More command


You can also use less or more comand to view a file content, type:

$ less /etc/passwd

or

$ more /etc/passwd

Outputs:

$ less /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
/etc/passwd

Note: if you want to view the file contents on screen from top to bottion, and you can press UP & Down keys on your keyboard. or you can use Page UP or Page Down keys on your keyboard.

View a File Using Head or Tail Command


If you want to print the first 10 lines of a given file, and you can use head command, type:

$ head /etc/passwd

Outputs:

[root@mydevops ~]# head /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

If you only want to print the first 5 lines from your file, and you can pass the -n option to the head command, type:

$ head -n 5 /etc/passwd

Outputs:

[root@mydevops ~]# head -n 5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

If you want to print the last 10 lines from a file, and you can use tail command, type:

$ tail /etc/passwd

If you want to print the last 5 lines from /etc/passwd file, and you can pass the “-n” option to the tail command, type:

$ tail -n 5 /etc/passwd

outptus:

[root@mydevops ~]# tail -n 5 /etc/passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
devops:x:1000:1000:devops:/home/devops:/bin/bash
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

View a PDF file Using xdg-open Command


If you want to open a pdf file from the command line, and you can use xdg-open command, type:

$ xdg-open pdfFile.pdf
$ xdg-open pdfSample.pdf

Note: xdg-open works in Gnome, KDE, xfce, LXDE and perhaps on other desktops.

Conclusion


You should know that how to view a text file or pdf file using cat/less/more/head/tail/xdg-open commands from the command line in your CentOS or RHEL or Ubuntu system.

You might also like:

Sidebar



back to top