TAM TAM - 2 months ago 6
Bash Question

Script to check contents of a directory in Linux

How could I write a Linux shell script to check the contents of a directory to see if a file with the same name already exists?

The directory/location to be checked would be obtained from a file /root/TAM/store using the grep function.

The contents of store is the directory's of files which I have moved to a dustbin in a previous script, It stores the directory they were in before the mv

the input is just the name of the file in dustbin that you want to restore to its original location, If i file exists it should ask you to rename or chose a new dir

Answer

If the file /root/TAM/store has just a line with the directory to search, you can do as follows

if ls `cat /root/TAM/store` | grep -q filename_to_look_for; then
    echo "filename_to_look_for exists"
else
    echo "filename_to_look_for doesn't exist"
fi
Comments