Check if multiple directories exist with bash script

5,747
#!/bin/bash

while read -r dir; do
    if [[ -d $dir ]]; then
        echo "Dir exists"
    else
        echo "Dir $dir does not exist"
    fi  
done < dirs

Output on execution with this file as infile (dirs):

cat dirs
/usr/bin
/usr/sbin
/bin
/sbin

./checkDirs.sh 
Dir exists
Dir exists
Dir exists
Dir exists
Share:
5,747

Related videos on Youtube

popcornuk
Author by

popcornuk

Updated on September 18, 2022

Comments

  • popcornuk
    popcornuk almost 2 years

    I have a file with a list of directories and I need to find out if they actually exist on the system. It's quite a large list so I'd like to figure out how to automate the check.

    The file is formatted with each directory on a new line:

    /usr/bin
    /usr/sbin
    /bin
    /sbin
    

    Any suggestions? Thanks!