Link to home
Create AccountLog in
Avatar of advcom
advcom

asked on

Need bash shell script that will loop through /mnt and check if file exists on any of the drives

I need a simple bash shell script that will check if a file exists on any of the attached hard drives. Then it will dump a list of the drive paths with a * next to any that have this file.

The text file should look like this, with the * being the drive with the file we checked for:
/mnt/sda1
/mnt/sda2
/mnt/hda1 *

The file name is source and will always be at the root of the drive. We do not need to read the contents of the file, just need to know if it exists.

Here is what I have so far, pretty much nothing:
ls /mnt > drive_list.txt

Just not sure how to check if the file exists

Thanks
ASKER CERTIFIED SOLUTION
Avatar of advcom
advcom

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Tintin
Tintin

You could do
#!/bin/bash
for dir in /mnt/*
do
  if [ -f $dir/source ]
  then
     echo "$dir *"
  else
     echo "$dir"
  fi
done

Open in new window