The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Disk Usage in GB and Unix command to find the biggest file/folder manas6 UNIX for Dummies Questions & Answers 8 07-30-2008 07:54 AM
Script to find a file in the other folder ragavhere Shell Programming and Scripting 4 04-11-2008 02:38 AM
Parse the .txt file for folder name and FTP to the corrsponding folder. MeganP Shell Programming and Scripting 3 07-03-2007 01:54 PM
Take a folder name and find it in another folder (Complicated) hkhan12 Shell Programming and Scripting 5 09-06-2006 12:25 PM
how to differentiate a file from a folder in a FIND? denysQC UNIX for Dummies Questions & Answers 3 06-06-2006 06:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-02-2007
bsandeep_80 bsandeep_80 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 122
Find a file in a folder-please help

HI ,

I have a very small requirement here. I need to find a file say which ends with .gz in a folder. If i found this file then i need to echo" file found" and do a word count of the file and if not i need to echo file not found and exit from the loop. i have written this script but i am only getting INPUT FILE FOUND FOR even if i don't have the ".gz " file in the folder.

Can anyone help to see what is wrong in this script that i have written

The script is below:

##!/usr/bin/sh
FROM_DIR=/home/sandy; export FROM_DIR
cd $FROM_DIR
for filename in $FROM_DIR/*.gz
do
find . -name '$FROM_DIR/*.gz'
if [ "$?" -ne 0 ]
then
echo " INPUT FILE NOT FOUND FOR ......." >>
else
echo " INPUT FILE FOUND FOR ......." >>
fi
wc -l *.cc3
done
exit 0
  #2 (permalink)  
Old 08-02-2007
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
  
 

Join Date: Nov 2006
Location: Bulgaria
Posts: 1,322
Since I'm not very familiar with ksh, i did it in bash, the following works for me :
cat run.bash
Code:
#!/bin/bash

FROM_DIR=/home/sysgate/builds
cd $FROM_DIR
for filename in *.gz
do
find . -name '*.gz'
if [ "$?" -ne 0 ]
then
echo " INPUT FILE NOT FOUND FOR ......."
else
echo " INPUT FILE FOUND FOR ......."
fi
wc -l *.cc3
done
exit 0
result :
Quote:
./test.gz
INPUT FILE FOUND FOR .......
I guess you should remove the $FROM_DIR, thus the forward slash will be removed also, just "*.gz".
  #3 (permalink)  
Old 08-02-2007
benefactr benefactr is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 71
Unsure what you mean by doing a count, I am thinking you are wanting to put the file names found into a file called something.cc3.

Here's something that may help you... You could probably modify it for your needs.

FOUNDFILE=/u/data/gzfilesfound
rm ${FOUNDFILE}
for filename in `ls ${FROMDIR}/*.gz` #If over or around 1000 files will not work
do
echo "${FILENAME}" >> ${FOUNDFILE}
done

if [ -s ${FOUNDFILE} ]
then
COUNT=`cat ${FOUNDFILE} | wc -l | tr -d " "`
echo "There were ${COUNT} Files found"
else
echo "No files Found"
fi
  #4 (permalink)  
Old 08-02-2007
bsandeep_80 bsandeep_80 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 122
Hi benefactr,

sorry......... it was not .gz and but .cc3..............i am trying word count of the file

what i trying do is trying to find the .cc3 file .....and if i find any i should do a word count of the file .......if not found then say find not found


Please let me know if the above script that u gave is good enough or any changes are required
  #5 (permalink)  
Old 08-02-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
bsandeep,
Your script will always find the files, as you are looping thru
existing files in the directory:
Code:
...
for filename in $FROM_DIR/*.gz
...
If you are checking for one specific file, there is no need for a loop.
One way to check if a file exists:
Code:
mFileName='YourFile'
if [ -f ${mFileName} ]; then
  echo 'File '${mFileName}' exists number records = '`wc -w ${mFileName}`
else
  echo 'File '${mFileName}' does not exist.'
fi
Also if you want a "word count", the option in the 'wc' command is '-w', not '-l'.
  #6 (permalink)  
Old 08-03-2007
bsandeep_80 bsandeep_80 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 122
Thanks so much shell life
  #7 (permalink)  
Old 08-03-2007
bsandeep_80 bsandeep_80 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 122
Hi what if i have multple .cc3 files in the folder. How do i check for them and also the number of records. Th e script above is for only one file

PLEASE HELP
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0