LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash Scripts for Dir listing (https://www.linuxquestions.org/questions/linux-general-1/bash-scripts-for-dir-listing-221537/)

jchristman 08-23-2004 09:03 PM

Bash Scripts for Dir listing
 
I am needing to figure out how to see if a file exists inside of a directory. lets say I have

/test
I need to see if test.txt or any file is in the directory if a file is in the directory then do something. like execute a wall statement saying there is a file there.

can anyone help

wapcaplet 08-23-2004 09:09 PM

Check out the Linux Cookbook section on Finding Files. The 'find' command is very versatile; it can do what you're asking, including the part about executing a command after finding something.

jchristman 08-23-2004 09:46 PM

That has a lot of helpful information but I am not that good at bash scripting yet, If you can I need help incorporating this into a script.

#! /bin/sh

find /test *.txt

if *.txt then wall " The file *.txt is in the file"

exit

or something like this

#!/bin/sh

ls /test/*.txt

if *.txt exists then wall "The file *.txt is in the file"

exit

This is the basics for what I am trying to do.

Mephisto 08-23-2004 10:18 PM

I am a far cry from a sh script expert but this should work:

Code:

if [ -r *.txt]; then
  <do something>
fi

I can't recall using a wildcard in a conditional like this though. What the statement says is if there is a readable (-r) file ending in .txt (*.txt) then do something. (note this is not tested, i usually test for a specific file not a wildcard.)

wapcaplet 08-24-2004 07:28 AM

Quote:

Originally posted by jchristman
That has a lot of helpful information but I am not that good at bash scripting yet, If you can I need help incorporating this into a script.
Did you read the link I gave you? There is no need for a script; the find command alone is enough to do what you're asking.

jchristman 08-24-2004 10:29 AM

I read over most of it and tried to get it to work with what I was doing I am still working on it though thanks wapcaplet.

Mephisto, thank you for the input it will help me as the script becomes more completed.

Currently what I am trying to do is just a small piece of the overall script

jchristman 08-26-2004 08:58 AM

Mephisto, thanks I have that section of my script running now.

I used this to check a directory for files that have been placed into a certain directory then it emails me a notification.


All times are GMT -5. The time now is 03:40 AM.