LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-23-2004, 09:03 PM   #1
jchristman
Member
 
Registered: Mar 2003
Distribution: Fedora Core 3
Posts: 125

Rep: Reputation: 15
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
 
Old 08-23-2004, 09:09 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 08-23-2004, 09:46 PM   #3
jchristman
Member
 
Registered: Mar 2003
Distribution: Fedora Core 3
Posts: 125

Original Poster
Rep: Reputation: 15
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.

Last edited by jchristman; 08-23-2004 at 09:52 PM.
 
Old 08-23-2004, 10:18 PM   #4
Mephisto
Member
 
Registered: Oct 2002
Location: Washington D.C, USA
Distribution: Slack 12, Etch, Gutsy
Posts: 453

Rep: Reputation: 30
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.)
 
Old 08-24-2004, 07:28 AM   #5
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 08-24-2004, 10:29 AM   #6
jchristman
Member
 
Registered: Mar 2003
Distribution: Fedora Core 3
Posts: 125

Original Poster
Rep: Reputation: 15
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
 
Old 08-26-2004, 08:58 AM   #7
jchristman
Member
 
Registered: Mar 2003
Distribution: Fedora Core 3
Posts: 125

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash - listing directories tarja Programming 8 12-03-2005 04:03 PM
dir color listing nash Linux - General 5 11-14-2004 01:05 AM
shell scripts & change dir pit1516 Linux - Newbie 6 06-22-2004 10:42 AM
-bash no such file or dir slackwarefan Slackware 3 06-12-2004 07:49 AM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM


All times are GMT -5. The time now is 02:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration