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

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

4 Linux commands to get number of lines in file

September 18, 2013 by Sharad Chhetri Leave a Comment

To know about how many lines exist in file is important for System Admin.There are sometimes very huge files which has lot of line numbers hence it is not advisable to open the file using cat command or any file editor like vi,vim,nano etc., because it takes lot of time to open it. Hence it is important to check how many lines exist in file.
In this post we will know 4 linux commands through which you can get number of lines in file. Most popular command is wc -l filename.In other words you can also say 3 other alternate method of wc -l filename command

4 Linux commands to get number of lines in file

(1) wc command: wc command can be used with different switches to get different output.To get the number of lines in file,we use the switch -l

Syntax: wc -l filename

Example:

linux@tuxworld:/tmp$ wc -l test 
25 test
linux@tuxworld:/tmp$

(2) awk command: awk is pattern scanning and processing language and with the help of many arguments it is widely use in command line. One of the example with awk command is to get number of lines in file

Syntax: awk 'END { print NR }' filename

Example:

linux@tuxworld:/tmp$ awk 'END { print NR }' test
25
linux@tuxworld:/tmp$

(3) sed command: Another powerful command sed, it is stream editor for filtering and transforming text.By using some arguments with sed command we can also find out number of lines in file.

Syntax: sed -n '$=' filename

Example:

linux@tuxworld:/tmp$ sed -n '$=' test 
25
linux@tuxworld:/tmp$

(4) mapfile command: The bash builtin mapfile reads from standard input and copies lines to the given array. By this method we can also get to know number of lines in file

Example:

linux@tuxworld:/tmp$ mapfile i < test
linux@tuxworld:/tmp$ echo "${#i[@]}"
25
linux@tuxworld:/tmp$ 
linux@tuxworld:/tmp$ unset i

Note: "unset i" will unset the value of variable called i which we have used in command mapfile in above given example.

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Unix / Linux : How to print duplicate lines from file
  2. cat,sed and awk commands to display file contents
  3. grep command to remove blank lines from file
  4. NOTICE: nagios.cmd file not found. Please specify the location of this file in your /etc/vshell.conf file
  5. 4 different commands to check the load average in linux
  6. 4 Different commands to find system uptime in linux
  7. list files and directories by 4 commands in linux
  8. Linux Basic Commands With Examples For Every Beginner
  9. grep command to show lines after and before the keyword
  10. sed : remove lines between two keywords [ including keywords ]

Filed Under: Linux Tagged With: bash

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

How to empty Trash through command line in Ubuntu

Change default editor of crontab in Ubuntu

How to change smtp port number 25 in postfix

Change the default port number of ssh server

Forward all incoming emails to other SMTP server or gateway

How to add/install Ubuntu fonts in CentOS/Red Hat Linux

Show visitor ip address php code

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy