Reputation: 347
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
Upvotes: 1
Views: 293
Reputation: 47317
There's a lot of details missing from your requirements, but in general you can do this to check if a specific file exists:
grep "pattern" location_file | xargs ls
Upvotes: 0
Reputation: 74078
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
Upvotes: 1