Blog

03/06/2019

Linux And Unix Command To View File



I am a new Linux bash shell command line user. How can I view a text file using command line on a Linux or Unix-like operating systems? How can I view (open) binary files such as PDF or images on a Linux or Unix like system from command line? Also, How do I list the files in a directory stored on my Unix box?

You can use any one of the following command to view a text file or any other files such as PDF, doc, image, video, music/mp3 and more.

Linux And Unix Command To View File

[donotprint][/donotprint]

  • cat command
  • less command
  • more command
  • gnome-open command or xdg-open command (generic version) or kde-open command (kde version) – Linux gnome/kde desktop command to open any file.
  • open command – OS X specific command to open any file.

View a text file called foo.txt on a Linux or Unix-like systems

Open the Terminal application and type the following command to view a text file called foo.txt using cat command:
cat foo.txt
OR
cat /etc/resolv.conf
Sample outputs:

nameserver 101.1.2.3
nameserver 102.2.3.4

You can also use more or less command as follows:

less filename
more filename-here

gnome-open: Open files and directories/urls

The gnome-open command opens a file (or a directory or URL), just as if you had double-clicked the file’s icon. The syntax is:

gnome-open file.pdf
gnome-open foo-bar.mp3
gnome-open mars-video.mp4
gnome-open https://sxi.io/

See our faq “Open Gnome Nautilus File Manager For The Current Linux / Unix Command Line” for more information.

If you are using KDE desktop try kde-open command as follows:

kde-open file.pdf
kde-open foo-bar.mp3
kde-open mars-video.mp4
kde-open http://sxi.io/

Another option is to try out xdg-open command on Linux and Unix desktop:

xdg-open file.pdf
xdg-open foo-bar.mp3
xdg-open mars-video.mp4
xdg-open http://sxi.io/

If you are using OS X Unix try open command as follows:

open file.pdf
open sai-ram-bhjan.mp3
open mars-video.mp4
open http://sxi.io/

How do I list the files in a directory on Unix?

To see or list the files in a directory on Linux, run ls command:
ls
ls -l

If you would like to see and list files in another directory, use the ls along with the path to the directory:
ls /usr/
ls -l /etc/
## just list resolv.conf file #
ls -l /etc/resolv.conf

Common ls command options

The syntax is:
ls [options] file
Where options for ls are:

  • -l : Use a long listing format to display Linux/Unix file names.
  • -a : Do not ignore entries starting with . (period). Display all hidden files.
  • -d : List Linux directories themselves, not their contents
  • -R : List sub-directories recursively on Linux
  • -F : Append indicator for file. For example, / for directories, * for executable Linux files, @ for symbolic links and more

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...