Find a file in a folder-please help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find a file in a folder-please help
# 1  
Old 08-02-2007
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  
Old 08-02-2007
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  
Old 08-02-2007
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  
Old 08-02-2007
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  
Old 08-02-2007
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  
Old 08-03-2007
Thanks so much shell life
# 7  
Old 08-03-2007
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common lines with one file and with all of the files in another folder

Hi! I would like to comm -12 with one file and with all of the files in another folder that has a 100 files or more (that file is not in that folder) to find common text lines. I would like to have each case that they have common lines to be written to a different output file and the names of the... (6 Replies)
Discussion started by: Eve
6 Replies

2. UNIX for Beginners Questions & Answers

UNIX utility to find difference in folder, file and contents of file against a base version

Hi, I am trying to find out whether there are any Unix utilities that compares folders, files and contents within the file and provides a comprehensive report. The comparison can be against base version of a folder and file with content. Can you please let me know of such a utility? Thanks,... (6 Replies)
Discussion started by: Sripathi_ks
6 Replies

3. Shell Programming and Scripting

Find folder within folder, then find other folder in same dir

Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do: I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory... (9 Replies)
Discussion started by: DC Slick
9 Replies

4. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

5. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

6. UNIX for Dummies Questions & Answers

Disk Usage in GB and Unix command to find the biggest file/folder

Hi All, Please help me out 1) Command to find the disk usage in GB. I know that du -k will give in kilobites. 2) How to find the Biggest file/folder in a given set of files/folders. Thanks in advance Regards, Manas (8 Replies)
Discussion started by: manas6
8 Replies

7. Shell Programming and Scripting

Script to find a file in the other folder

Hi, I have two folders in two different directories.Both the folders contain set of text files. The name of the files are same in both the folders. I need to write a script to compare all the files in both the folders having same name. I am new to unix. Can you help me out in giving a script to... (4 Replies)
Discussion started by: ragavhere
4 Replies

8. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies

9. Shell Programming and Scripting

Take a folder name and find it in another folder (Complicated)

Hi Script Experts, Here is my scenario: 1. /var/mqm/qmgrs folder will contain 11 folders as follows: 1. /var/mqm/qmgrs/Folder_Name1 ....................../Folder_Name2 ....................../Folder_Name3 ....... ...................../Folder_Name11 2. if Folder_Name1 exists... (5 Replies)
Discussion started by: hkhan12
5 Replies

10. UNIX for Dummies Questions & Answers

how to differentiate a file from a folder in a FIND?

I have to read a complete folder and if it is a file older that 7 days I have to copy it elsewhere and if it is a folder nothing to make. The way I do it: for I in `find /home/. -name "*" -mtime +7` do cp -Rf $I /home/elsewhere/. done Am I okay with the way I want to do it? Help... (3 Replies)
Discussion started by: denysQC
3 Replies
Login or Register to Ask a Question