Solved

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

Posted on 2010-09-09
2
559 Views
Last Modified: 2013-12-26
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
Comment
Question by:advcom
2 Comments
 

Accepted Solution

by:
advcom earned 0 total points
ID: 33643525
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
 
LVL 48

Expert Comment

by:Tintin
ID: 33643628
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now