• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

View files using – cat, more, tail, head and wc commands

By admin

There are several commands that display information about a file in the read-only format. The file-viewing commands include the following:

  • cat
  • more
  • tail
  • head
  • wc

cat Command

The cat command displays the content of one or more text files on the screen without pausing.

$ cat filename

For Example:

# cat data.txt
northwest       NW      Joel Craig          10
western         WE      Sharon Kelly        40
southwest       SW      Chris Foster        33
central         CT      Sheri Watson        44

Do not use the cat command to read binary files. Using the cat command to read binary files can cause a terminal window to freeze. If your terminal window freezes, close the terminal window, and open a new terminal window.

Note: Before you attempt to open a file with the cat command, it is recommended that you first run the file command to determine the file type.

more Command

The more command displays the content of a text file one screen at a time.

$ more filename

The

--More--(n%)

message appears at the bottom of each screen, where n% is the percentage of the file that has been displayed. When the entire file has been displayed, the shell prompt appears.

When the –More–(n%)prompt appears at the bottom of the screen, you can use the keys described in the table to scroll through the file.

Keyboard Command Action
Space bar Moves forward one screen
Return Scrolls one line at a time
b Moves back one screen
h Displays a help menu of features
/string Searches forward for pattern
n Finds the next occurrence of pattern
q Quits and returns to the shell prompt

head Command

The head command displays the first 10 lines of a file.

$ head -n filename

You can change the number of lines displayed by using the -n option. For example, to display the first five lines of the /var/log/messages file, enter the head command with the -n option set to 5.

$ head -5 /var/log/messages

tail Command

The tail command displays the last 10 lines of a file.

$ tail –n/+n filename

You can change the number of lines displayed by using the -n or +n options.
– The -n option displays n lines from the end of the file.
– The +n option displays the file from line n to the end of the file.

For example, to display the last four lines of the /var/log/messages file, enter the tail command with the -n option set to 4.

$ tail -4 /usr/dict/words

For example, to display line 10 through the end of the data.txt file, enter the tail command with the +n option set to 10.

$ tail +10 data.txt

wc Command

The wc command displays the number of lines, words, and characters contained in a file.

$ wc -options filename

You can use the following options with the wc command.

Symbol Description
-l Line count
-w Word count
-c Byte count
-m Character count

When you use the wc command without options, the output displays the number of lines, words, and characters contained in the file. For example, to display the number of lines, words, and characters in the dante file, use the wc command.

$ wc data.txt
32      223     1319    data.txt

For example, to display the number of lines in the dante file, enter the wc command with the -l option.

$ wc -l data.txt
32 data.txt

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL : How to configure vsftpd to use ports other than the default ports 20 and 21
  2. How to configure CentOS/RHEL 6 system to not used last 3 passwords used
  3. How to enable the automatic extension for a thin LVM volume
  4. How To Create An Almost Root Equivalent Users But Not Root Identical User in Linux
  5. How to split iso or file using ‘split’ command in Linux
  6. How to disable “Alt+Ctrl+Del” causing system reboot in CentOS/RHEL 7
  7. ‘ip’ Command cheat sheet (Command Line Reference)
  8. Understanding Variables in Bash Shell Under Linux
  9. How to Restrict Active Directory Users and Groups to Login to CentOS/RHEL 7 Client
  10. UNIX / Linux : Examples of bash history command to repeat last commands

You May Also Like

Primary Sidebar

Recent Posts

  • What are different Oracle Database Vault Roles
  • Unable to export realm protected table using data pump
  • Beginners Guide to Oracle Database Vault
  • How to Disable IPv6 on Ubuntu 18.04 Bionic Beaver Linux
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary