Visit Our UNIX and Linux User Community


check existence of files in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check existence of files in a folder
# 1  
Old 11-07-2011
check existence of files in a folder

Hi I am having a problem to verify existence of files. I need to know whether or not files in a folder that begins with a name.
For example all files that start with The_File_ *.
I was doing it this way, but gives me error.

Code:
if text -f /work/The_File_*
then
...
else
..
fi


Last edited by radoulov; 11-07-2011 at 04:25 PM.. Reason: Code tags!
# 2  
Old 11-07-2011
Isn't it:
Code:
if [ -f /work/The_File_* ]
then
     echo "Exists!"
fi

Or you can use, because the above can give you errors like: "too many arguments" when the shell expands "The_File_*":
Code:
numFiles=`find /work -name "The_File_*" | wc -l`
if [ ${numFiles} -gt 0 ]
then
     echo "Exists!"
fi


Last edited by felipe.vinturin; 11-07-2011 at 02:52 PM..
# 3  
Old 11-07-2011
Quote:
Originally Posted by felipe.vinturin
Isn't it:
Code:
if [ -f /work/The_File_* ]
then
     echo "Exists!"
fi

Or you can use, because the above can give you errors like: "too many arguments" when the shell expands "The_File_*":
Code:
numFiles=`find /work -name "The_File_*" | wc -l`
if [ ${numFiles} -gt 0 ]
then
     echo "Exists!"
fi

Quote:
work=/work/input/telefonia/archivos

if [ -f $work/PN_MOV_* ]
then
echo "Exists!"
fi
I put what you said and it shows me this error
Code:
./existe: line 3: [: too many arguments


Last edited by radoulov; 11-07-2011 at 04:25 PM..
# 4  
Old 11-07-2011
That's what I said in my post! Try the "find" solution...
# 5  
Old 11-07-2011
Quote:
Originally Posted by felipe.vinturin
That's what I said in my post! Try the "find" solution...
Work Work! Thanks!
# 6  
Old 11-07-2011
Code:
is_file()
{
  for _file
  do
    [ -f "$_file" ] && return
  done
  return 1
}

if is_file /path/to/dir/The_File_*
then
  echo "File found"
else
  echo "No files"
fi

These 2 Users Gave Thanks to cfajohnson For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #610
Difficulty: Medium
Compared to Python, Perl has a relatively small Comprehensive Perl Archive Network (CPAN) library.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check in a folder all files which doesnt have the specifc string?

I have a folder with 100s of dat files, with delimiter "|", in some files they didn't provide this delimiter. how to automatically check those list of files in a folder which doesnt have a delimiter or string this way "|" is it possible? Thank you very much for the helpful info. (3 Replies)
Discussion started by: cplusplus1
3 Replies

2. Shell Programming and Scripting

Script to check the files existence inside a directory.

Hello Folks, On Below Script, I want to apply condition. Condition that it check the directory for files if not found keep checking. Once directory have files and founded do the calculation and finish the code. Script to check the files existence inside a directory, If not then keep... (16 Replies)
Discussion started by: sadique.manzar
16 Replies

3. Shell Programming and Scripting

Check the Files existence

Hi I have a requirement to check whether the files exists, then it will call other steps in shell script. I did ls *.csv|wc -l if then checking the count of the files should be more than 1 then it will call other steps. I am getting the error that too many arguements as there n... (13 Replies)
Discussion started by: cnrj
13 Replies

4. Shell Programming and Scripting

Check for the existence and add them from 2 different files

Hi, I have two files file1: ALEX DANY GARY TOM MARY HARRIS file2: ALEX 3 ALEX 5 ALEX 0 ALEX 1 ALEX 0 DANY 2 (2 Replies)
Discussion started by: Diya123
2 Replies

5. Shell Programming and Scripting

Check folder existence using wildcard

Hi I would like to know how I can check whether there is one or more folders in the current directory which begins with e.g. 2011-11. Initially I figured that this could easily be done simply by: if ; then ... However if there is more than one folder which begins with 2011-11 then it... (1 Reply)
Discussion started by: aknu
1 Replies

6. Shell Programming and Scripting

Check existence of a number of files and call other scripts

Hi, I am new to unix scripting and am jus getting to learn about it.. I need to know on how to check for the existence of a number of files in a path..i.e the files are ftp'ed from several other servers, should check if all the files have arrived, if not wait till they arrive..can i use a flag... (5 Replies)
Discussion started by: yohasini
5 Replies

7. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

8. Shell Programming and Scripting

HOW TO CHECK ONLY .C FILES EXISTS OR NOT IN A FOLDER using IF in C shell script?

Hi friends.. I hav a problem.... I dont know how to check .c files exists r not in a folder using IF in C shell script actually i tried like this if(=~ *.c) even though some .c files or there in the current folder..it is not entering int o the if control statement...... (17 Replies)
Discussion started by: p.hemadrireddy
17 Replies

9. SuSE

Regarding Readable check for all the files in the folder

Currently we are doing the migration to unix to linux. I am facing the new problem kganeshb@its04489:~/scripts $ ls -l | more total 340 -rw-r----- 1 kganeshb users 9038 Oct 22 13:23 109_db.txt -rw-rw---- 1 dlc users 1413 Oct 10 17:40 1.txt -rw-rw---- 1 kganeshb users 45 Jan 28 13:46 a... (2 Replies)
Discussion started by: kingganesh04
2 Replies

10. Shell Programming and Scripting

check for FILES existence

hi, I have a list of filenames and I want to verify if they all exist. I know "if filename" would do the trick but how do I go about a list of files? thanks (3 Replies)
Discussion started by: mpang_
3 Replies

Featured Tech Videos