7

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?

mattdm
  • 39,535
  • 18
  • 99
  • 133
user1762571
  • 553
  • 5
  • 7
  • 12
  • 1
    Well this works... `ifconfig -a | grep 'flags' | wc -l` – orion Jul 02 '15 at 20:18
  • 1
    @orion, I would recommend using `ip` instead as it is a successor of `ifconfig` - `ip -o link show | wc -l`. See [this](http://serverfault.com/q/458628/145512) question – VL-80 Jul 02 '15 at 23:54
  • 1
    You might look at [`netdevice(7)`](http://linux.die.net/man/7/netdevice)). – o11c Jul 03 '15 at 02:15
  • @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. – orion Jul 03 '15 at 06:39
  • 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. – Alex Apr 24 '17 at 01:52

1 Answers1

8

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
111---
  • 4,424
  • 3
  • 27
  • 50