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 [ -f *.csv ]

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 number of files which has .csv.

Is there any other way we can check file existence? I don't need the count of the files, just need whether the files are there to process.

Thanks in advance

Maybe something like this:

for file in *.csv; do
  if [ -f $file ]; then
  ...
  fi
done

Thanks Bartus,

It works if i do process one by one file until there are no files.
I don't want to pick any files which arrive after my process is invoked. So I am checking whether the files exists, If exists i write all files into one one file, then i take the file name from that file and do the actual process.

The latter might work if the shell can take more arguments than ls.
Allow unlimited files:

\ls |
while read file
do
  if [ -f "$file" ]
  then
    case $file in
    *.csv) ;;
    *) continue;;
    esac
    ...    
  fi
done

I did \ls to escape an alias in interactive shells.
Some shells allow a more comprehensive match than the case statement.

My Script which i am using:

cd /var/opt/input
filecount=$(find . -type f -name *.csv | xargs wc -l)
if [ $filecount -gt 0 ]
then
echo "Number of Files are $filenumber"
else
echo "No Files"
fi

When the Input locations has huge number of files it is giving error saying

/usr/bin/find: arg list too long
find . -type f -name "*.csv" | wc -l

This gives only a number, and can be stored in a variable as you did.
But storing the list in a variable can lead to a "too long" error in the shell.

1 Like

Thanks

But i don't want to store list in variable i just need to store count value into Variable. still getting the same error.

Any other way to get this done?

This give the number of csv files in current directory tree.

count=$( find . -type f -name "*.csv" | wc -l )
echo $count
1 Like

It gives the number of files, But my problem is it is giving the error saying "arg list too long" as there are numerous files

When encountering that msgs, you need to escape the * in your find command (as do MadeInGermany and kshji)

Try this

filecount=$(find . -type f -name "*.csv" | wc -l)
1 Like

Thanks bean,

Could you please give some info about using the command with $ and `` as i used `` in same command it took long time so cancelled, when i use $() it worked quickly and got the answer.

Thanks

---------- Post updated at 05:31 AM ---------- Previous update was at 04:10 AM ----------

Again, there is problem in writing all these file names into one text file. It is giving me the same problem(arg list too long).

Alos please advise how do we write all these file name into one file which will be readable by While in script for processing each file.

Thanks

---------- Post updated at 08:41 AM ---------- Previous update was at 05:31 AM ----------

I have done this acheived it but still there is one problem while writing filenames in text file. File names are written as

I dont want ./ with the filename.

My Command

find . -type f -name "*.csv">FILE.LIST

Search the forums. Exactly this question has been answered a few days ago.

1 Like

Used basename in the next step to get rid of the path.

Thanks everone