Linux - Test if a Path exists, or is a directory, or is a block device

My Blog | Mike (Nerd)'s Blog



Sunday, April 9th, 2017, 4:19pm
Categories: Tech / Nerdery

Hello friends,

Here are a few tests you can run on paths in Linux shell scripts.

Use the "-e" test, to tell if a path exists.
Use the "-d" test, to tell if a path is a directory.
Use the "-b" test, to tell if a path is a block device.

The following script will test a few paths for you:
#!/bin/sh


do_tests()
{

#
echo
echo "####################"
echo "### Testing: $1"

# Exists?
if [ -e "$1" ];then
TEST_EXISTS="YES"
else
TEST_EXISTS="NO"
fi
echo "Exists? $TEST_EXISTS"

# Directory?
if [ -d "$1" ];then
TEST_DIR="YES"
else
TEST_DIR="NO"
fi
echo "Directory? $TEST_DIR"

# Block Device?
if [ -b "$1" ];then
TEST_BLOCK="YES"
else
TEST_BLOCK="NO"
fi
echo "Block Device? $TEST_BLOCK"

#
echo
}

#
do_tests "/dev/sda"
do_tests "/home"
do_tests "/dev/sdz"


Enjoy!

Next in 'Tech / Nerdery' : How to Force Ubuntu Release Upgrades >>>


Link to This Entry

 

Share With Your Friends


Subscribe to Mike (Nerd)'s Blog

1. Your Email 2. Your Name 3. Click "Subscribe" !

Comments

Be the first to comment!
Your Name
Your name is required since you are not logged in.
Your Email
Your email is required since you are not logged in.
Word Verification
Word verificaiton is required since you are not logged in.
Your Comment


(Don't fill this one out)

Submit

Booking | Newsletter | Contact | RSS

Photos

  • Instagram
  • Register / Community

  • Register (FREE)
  • Login
  • Find Other Fans
  • Mobile

  • Point your mobile browser to: MikePeralta.com/m or simply scan this code











  • Still need to fix CL_Hook_Blog_PublicInterface_ViewEntry_BeforeTemplate()