Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to Count Lines in a file in Linux

    How to Count Lines in a file in Linux

    By RahulAugust 7, 20212 Mins Read

    Sometimes you may be required to count total lines in a file in Unix/Linux systems. This tutorial helps you with multiple methods to count the number of lines in a file in a Linux system via the command line.

    Advertisement

    Count lines with wc Command

    The wc command is the “word counter” for the Unix/Linux systems. This is a widely used command among Linux users for counting the lines in a file. It is also useful for counting words and characters in a file.

    Open a terminal and type command to count lines:

    wc -l myfile.txt 
    

    You can also count the number of lines on piped output.

    cat myfile.txt | wc -l  
    

    Using grep Command

    Grep is another frequently used Linux command used for searching content with regular expressions. You can also use the grep command to count the number of lines in a file.

    grep -c ".*" myfile.txt  
    

    Using sed Command

    The sed is known as stream editor for Unix/Linux systems. Mainly it is used for search and replace content from a file or a stream.

    Use the sed command to count number of lines in a file:

    sed -n '$=' myfile.txt  
    

    Using awk Command

    AWK is a useful data processing and reporting tool. It is default available on all major Linux distributions. You can also use awk for counting the number of lines from a file.

    awk 'END{print NR}' myfile.txt  
    

    Here NR keeps the number of the current input records.

    Conclusion

    This tutorial helped you to count lines in a file in the Linux system.

    count file wc
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Top 10 JQ Commands Every Linux Developer Should Know

    How to Check if a Program Exists in Linux

    How to Create a Directory If It Does Not Exist in Linux

    View 2 Comments

    2 Comments

    1. Pavlov on August 13, 2020 10:10 am

      I think you got a spelling mistake :

      “Please change (..) it will return number of files in file as output.”
      Shouldn’t it be “it will return number of lines in file as output?”

      Besides that, thanks for the article!

      Reply
      • Rahul on August 13, 2020 11:00 am

        Thanks Pavlov.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.