Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

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
0
advcom
Asked:
advcom
1 Solution
 
advcomAuthor Commented:
Figured out my own fix while waiting...
I just have it echoing, but could just as well send it to a text file.
#!/bin/bash
clear

for i in $(ls -d /mnt/*);
do
	if [ -f ${i%%/}"/source" ]
	then
		echo ${i%%/}" *"
	else
		echo ${i%%/}
	fi
done

Open in new window

0
 
TintinCommented:
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

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now