0

I am wondering how to check if a directory (on my RaspberryPi) is on an external drive or not.

I think I mounted my WD MyPassport correctly, and was able to write to it via /media/pi/MyPassport. However, something happened and it created a MyPassport1 folder, which seemed to actually be the external drive. So I have /media/pi/MyPassport and /media/pi/MyPassport1 when the external is connected.

I removed my external drive and still can see (and access/read/write) to /media/pi/MyPassport...so something happened that moved the external directory, and created /media/pi/MyPassport locally.

I'd like to include in the script I have that copies data to MyPassport to first check if /media/pi/MyPassport is indeed on an external drive, before copying.

Is that possible?

Edit: I know, now, that the external drive is called .../MyPassport1, so could just see if that directory exists, then keep copying...but I'm asking more generally, is there a way to check if a directory is indeed an external drive connected via USB?

  • What is the output of df -h /media/pi/Passport1, when USB is mounted. – Prvt_Yadv Mar 25 at 3:16
  • @Prvt_Yadv - The filesystem is /dev/sda1. When I run that command on /media/pi/"My Passport", it's /dev/root. I have been learning about Unix and see that sda1 refers to an external drive, correct? (I know /dev/root is the root file system, aka local). – BruceWayne Mar 25 at 3:32
1

That is simple, if you use the command df -h folder_name, it will show you the filesystem. e.g.

I have drives mounted in /mnt/xxx, folder, if I use command

df -h /mnt
/dev/sda1        96G   81G   11G  89% /

because /dev/sda1 is mounted on / folder, and mnt, is a directory inside it.

df -h /mnt/uuid
/dev/sda5        98G   57G   42G  58% /mnt/uuid

So, in your script you can use:

if [ "$(df -h /media/pi/MyPassport | grep -o /media/pi/USB)" = "/media/pi/USB" ]
then
echo "USB is mounted"
else
echo "USB is not mounted"
copy....
fi 

You can also reduce the if statement to:

if [ "$(df -h /media/pi/MyPassport | grep -o /media/pi/USB)" ]

As it will check the exit status.

  • Thanks for the great detail! As I commented, the external drive filesystem is showing /dev/sda1, so am I correct in thinking that it means, explicitly that it's an external drive? (because /dev/root is explicitly the local filesystem, correct?) – BruceWayne Mar 25 at 3:34
  • 1
    @BruceWayne That is right. – Prvt_Yadv Mar 25 at 3:34
1

You can check where the device is mounted using the command 'df -h' . Then you can navigate to that folder and access the contents of that device.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.