Check if a directory contains another directory

bash check if directory contains files
bash check if directory exists
bash check if subdirectory exists
d in shell script
check if directory exists linux command line
bash check if directory exists if not create
check if directory does not exist bash
python check if directory is empty

How do I check if a given directory contains another directory in shell. I want to pass 2 full path directories. (I know this is stupid, but just for learning purposes). Then I want to see if any one of those 2 paths is contained in the other one.

parent=$1
child=$2

if [ -d $child ]; then
    echo "YES"
else
    echo "NO"
fi

this however makes no use of the parent directory. Only checks if the child exists.

You can use find to see if one name is contained within another:

result=$(find "$parent" -type d -name "$child")
if [[ -n $result ]]
then echo YES
else echo NO
fi

How to check if a given directory contains any other directory in , To check if a directory contains any directory or not, simply check the reverse, ie, if it contains any entry that is not a file using the isfile method. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more How to check if a directory contains another directory in Powershell?

Create a file (ex: dircontains.sh) with this code:

#!/bin/bash

function dircontains_syntax {
    local msg=$1
    echo "${msg}" >&2
    echo "syntax: dircontains <parent> <file>" >&2
    return 1
}

function dircontains {
    local result=1
    local parent=""
    local parent_pwd=""
    local child=""
    local child_dir=""
    local child_pwd=""
    local curdir="$(pwd)"
    local v_aux=""

    # parameters checking
    if [ $# -ne 2 ]; then
        dircontains_syntax "exactly 2 parameters required"
        return 2
    fi
    parent="${1}"
    child="${2}"

    # exchange to absolute path
    parent="$(readlink -f "${parent}")"
    child="$(readlink -f "${child}")"
    dir_child="${child}"

    # direcory checking
    if [ ! -d "${parent}" ];  then
        dircontains_syntax "parent dir ${parent} not a directory or doesn't exist"
        return 2
    elif [ ! -e "${child}" ];  then
        dircontains_syntax "file ${child} not found"
        return 2
    elif [ ! -d "${child}" ];  then
        dir_child=`dirname "${child}"`
    fi

    # get directories from $(pwd)
    cd "${parent}"
    parent_pwd="$(pwd)"
    cd "${curdir}"  # to avoid errors due relative paths
    cd "${dir_child}"
    child_pwd="$(pwd)"

    # checking if is parent
    [ "${child_pwd:0:${#parent_pwd}}" = "${parent_pwd}" ] && result=0

    # return to current directory
    cd "${curdir}"
    return $result
}    

Then run these commands

. dircontains.sh

dircontains path/to/dir/parent any/file/to/test

# the result is in $? var 
# $1=0, <file> is in <dir_parent>
# $1=1, <file> is not in <dir_parent>
# $1=2, error

Obs: - Tested only in ubuntu 16.04/bash - In this case, the second parameter can be any Linux file

Python: Check if a directory is empty, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and  Check if directory contains files using python Finding if a directory is empty or not in Python can be achieved using the listdir() method of the os library. OS module in Python provides functions for interacting with the operating system.

Pure bash, no external commands used:

#!/bin/bash

parent=$1
child=$2
[[ $child && $parent ]] || exit 2 # both arguments must be present
child_dir="${child%/*}"           # get the dirname of child
if [[ $child_dir = $parent && -d $child ]]; then
  echo YES
else
  echo NO
fi

Bash Shell Check Whether a Directory is Empty or Not, How can I check from bash/ksh shell script if a directory contains files? Here is another example using bash for loop to check for any *.c files in the ~/projects/  How do I check if a given directory contains another directory in shell. I want to pass 2 full path directories. (I know this is stupid, but just for learning purposes). Then I want to see if any o

You can accomplish this in pure bash. Loop over every file in $1 and see if "$1/$2" is a dir, like so:

parent=$1
child=$(basename $2)
if [ -d $parent ] && [ -d $child ]; then
    for child in $parent; do
        if [ -d "$parent/$child" ]; then
            echo "Yes"
        else
            echo "No"
        fi
    done
fi

Check if a directory exists in Linux or Unix shell, One can check if a directory exists in a Linux shell script using the following syntax: [ -d "/path/dir/" ] && echo "Directory /path/dir/ exists." You can  I am having script in which i want to check if directory has any file in it or not. If directory contains a single or more files then and only then it should proceed to further operations P.S.: Directory might have thousand number of files.

Working With Files and Directories – The Unix Shell, We now know how to explore files and directories, but how do we create them in the to our data-shell directory on the Desktop and use ls -F to see what it contains: Let's create a new directory called thesis using the command mkdir thesis  10 Years Ago. when a file is uploaded by a user it is stored in a folder.. if he uploades other file in the same folder, the file which was already uploaded by him should be deleted and newly uploaded file must be saved. files are uploaded by users.. so we dnt know the filename..

Test-Path, The Test-Path cmdlet determines whether all elements of the path exist. any files in the Commercial Buildings directory other than .dwg files. In this case, because the directory contains only .dwg files, the result is $False . Joseph's nullglob approach is the most elegant but if you don't want to or can't use that (non-bash shell or older bash version for example), you can also do this (assuming your file names contain no newlines):

Learning the shell - Lesson 2: Navigation, The root directory contains files and subdirectories, which contain more files and While drive letters split the file system into a series of different trees (one for  If you want to check if the directory the script/program is currently in contains a subdirectory, you can use the trick I demonstrate below - where I check if there's a subdirectory called "Windows". In PowerShell, the namespace "System" doesn't have to be typed in explicitly, so you can omit it.

Comments
  • You want to see if $parent is a parent of $child at some level? That's a string prefix check (assuming you aren't worried about symlinks, ../ games, etc.).
  • Use find -type d -name "$child".
  • The reason why this won't work is because in the second if stattement, you are appending the entire child path to the parent path. This results in a non-existent directory and it will always result in NO.
  • @Kanny Bros, use child=$(basename $2) if you'd only like the folder name of $child instead of its absolute path.