Manually trigger MediaScanner to scan files on Internal Storage on Nougat/Oreo I often manage my files in Termux with scripts that use the mv command to move or rename them. I want to manually trigger MediaProvider to scan my internal stroage (/storage/emulated/0) or a given directory in it with command line, on a phone running Nougat or Oreo.

When I use a GUI file manager application, all moves/renames are immediately reflected in other MediaScanner-dependent apps like Gallery, but changes done in a terminal don't, until a reboot. So I have to manually call MediaScanner to update my changes in the filesystem.

Before KitKat, this worked well on /storage/sdcard0 (a separate partition named glow):

am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard


The above command doesn't work now (/storage/emulated/0), obviously.

I also tried this, but it doesn't seem to be working, either.

am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard


Just tested the commands again, they seem to be working on removable storage like /storage/sdcard1, /storage/usba or /storage/{volume id} (it gets re-scanned), but not my phone's internal (where /sdcard points to, or /storage/emulated/0).

Anyone have some insights? Rebooting my phone always causes a rescan but that's inconvenient in some cases.

Note: the commands are run as root, so no concerns about permissions.
Note: It is possible the following commands do not work out for you with just USB debugging /  shell (uID 2000) user. In that case, you must have root access to make them work.


Before KitKat, this worked well:
am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard

The above command doesn't work now, obviously.

This works perfectly well on my stock Android 7.1.1 (Nexus 6) having only Internal Storage. But starting since Oreo implicit broadcasts are not allowed, so you have to send an explicit broadcast to Media Storage. This can be done using the argument -n followed by component name or simply by -p followed by package name. In Android 9.0 (OnePlus 6), here's what worked for me:
am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///sdcard/ -p com.android.providers.media

I believe the above command would work for Android Oreo as well.
Note: shell (user 2000) cannot run that command, so you must elevate privilege to system (1000) or root (0). In short, run the command as root or it won't work.


I also tried this, but it doesn't seem to be working, either.
am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard


Because that intent expects a FILE as data, not a directory (I understand everything is a file in Linux, but that's not the case for an intent). Provide the full path of a file and it would work. Furthermore, as stated above, for Oreo and above, make the broadcast explicit.
If you have multiple files and you intend to use this intent only, you need to get all the file names and pass them on to the intent recursively (per Aritra Roy on Stack Overflow). It may be possible to pass an array but I don't know how to do it with adb.
Furthermore, my Android behaved rather erratically for file names containing [].