Sending adb command to all of your connected devices

Raymond Chan
1 min readOct 2, 2023

--

Sometimes you may need to perform adb commands on all of the connected devices during development. For example, I sometimes need to install APKs to different devices at the same time. The script is like this:

adb devices | tail -n +2 | cut -sf 1 | xargs -I {} -P4 adb -s {} install -r ~/Downloads/android-app.apk

Breaking down the command:

  • adb devices — list out all the Android devices connected.
  • tail -n +2 — removing the first line of the input, i.e. “List of devices attached”.
  • cut -sf 1 — Extracting the device or emulator serial number.
  • xargs -I {} -P4 — reading items from standard input and executes a command with those items as arguments. -I {} specifies a placeholder {} that will be replaced with each item read from standard input.; -P4 specifies that up to four instances of command can be executed at once. (Adjust this value if you have more than four devices.)
  • adb -s {} install -r ~/Downloads/android-app.apk — Nothing but just your normal adb -s xxxx ;)

Enjoy!

https://gist.github.com/raymondctc/9f1d3841e5af573a3be6

--

--

Raymond Chan

Hello World Engineer @9GAG #android #Hongkonger #StandWithHongKong