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
563 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

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…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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