TAM
TAM

Reputation: 347

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

Upvotes: 1

Views: 293

Answers (3)

sampson-chen
sampson-chen

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

Olaf Dietsche
Olaf Dietsche

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

ryanbwork
ryanbwork

Reputation: 2153

Here's a pretty good example of using the if statement in a bash script to check if a file exists (under the 7.1.1.3 heading)

Upvotes: 1

Related Questions