Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
565 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 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