Saturday, May 9, 2015

How to check the files existing in particular directory in bash script (UNIX)

 

In order to verify list of file names are existing  in particular location in Unix  we can use the if function to verify if the file is exists in the given path or not.

We need to read the list files which we need to verify . To just read the file names only on a given location we can use the ls command with additional option like below.

ls --file-type –1 <path location> | grep -v '/' >file_list

when the above command is executed the file_list will have all the filenames on the path location mentioned on the command

Once we read the list of file names we can verify those files exists in particular location or not by using below function

for filename in `cat file_list`
do
    if [ -a ${path}/$filename ];
        then   
                echo “ File exists in the $path”
        else        
                echo " File is NOT  found "
    fi
done

--------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment