How to find out the number of network interfaces available in a linux system?

12,446

You can get a list of these interfaces on most systems from the following:

ls -A /sys/class/net

But beware of parsing the output from ls in your script.

Edit

To get a total number of network interfaces pipe the output of this command into wc as recommended in Nikolay's comment as in:

ls -A /sys/class/net | wc -l
Share:
12,446

Related videos on Youtube

user1762571
Author by

user1762571

Updated on September 18, 2022

Comments

  • user1762571
    user1762571 over 1 year

    I need to loop over the network interfaces available in Linux. I'm interested in all kinds of interfaces (loopback, ethernet, vlan, bridge) - whatever shows up in ifconfig -a.

    Is there a way to enumerate the interfaces in Linux? By any command or by reading a file?

    • Admin
      Admin almost 9 years
      Well this works... ifconfig -a | grep 'flags' | wc -l
    • Admin
      Admin almost 9 years
      @orion, I would recommend using ip instead as it is a successor of ifconfig - ip -o link show | wc -l. See this question
    • Admin
      Admin almost 9 years
      You might look at netdevice(7)).
    • Admin
      Admin almost 9 years
      @Nikolay of course, that's what I would use, and others already posted that answer (and removed it). I just wanted to point out that from what OP did to the answer to his question isn't very far.
    • Admin
      Admin about 7 years
      Just in case it helps anyone, adding the option "1" to the ls command will list output one item per line, e.g: ls -A1 /sys/class/net eth0 lo usb0 wlan0 In fact, I was surprised to find the count using "wc -l" still works on the single line version of the command (ls -A). But there are likely other situations where the item-per-line output from ls will be preferable, or necessary.
  • Stéphane Chazelas
    Stéphane Chazelas almost 9 years
    Linux doesn't allow creating network interfaces with newline in their name, but it allows network interfaces whose name starts with ., so you'd need ls -A here.
  • 111---
    111--- almost 9 years
    Noted sir , ty :)
  • user1762571
    user1762571 almost 9 years
    This shows all the interfaces. But i am interested in the count.(total no of interfaces)