0

I have a directory that contain a single item. How can I check if that item is a directory or a file?

Darpan Sanghavi
  • 1,443
  • 2
  • 17
  • 32
Antonio
  • 9
  • 1

3 Answers3

1
stat -c %F yourItem

or use file to check: file yourItem

It will tell you if it is directory or file.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • No, the situation is that I have a directory called Dir where there is one file or one sub-directory. My actual CWD is Dir and i want to know if the only item in Dir is a file or is a directory – Antonio Apr 26 '18 at 11:33
0
ll test/ | egrep "^d" | wc -l

This returns 1 if the item in the directory "test" is a directory or 0 if it isn't.

Kokogino
  • 984
  • 1
  • 5
  • 17
0

You can do like this test.sh

#!/bin/bash

if [ -d "$1" ]; then
    echo "$1 is a directory";
elif [ -f "$1" ]; then
    echo "$1 is a file";
else 
    echo "$1 is not valid";
     exit 1
fi

Usage:

./test.sh /path/to/item
ntshetty
  • 1,293
  • 9
  • 20