Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

bash script check file and directory

How to check file and directory in directory, to use bash script if file exists.
To write simple bash script check folder /home/huupv/Desktop all file and directory.
bash script check file and directory







  1. The first, use for loop /home/huupv/Desktop/*
  2. The secondary, to check if a directory then echo, elif a file then echo.

My bash scripts check file and directory
#!/bin/bash
for file in /home/huupv/Desktop/*
do
    if [ -d "$file" ]
    then
        echo "$file is directory"
    elif [ -f "$file" ]
    then
        echo "$file is file"
    fi
done
The running bash script check file and directory
$ ./studyscript.sh 
The output as bellow
/home/huupv/Desktop/Barbarpower Template.xml is file
/home/huupv/Desktop/HuuPV is directory
/home/huupv/Desktop/like_fb.sh is file
/home/huupv/Desktop/post-fb.docx is file
/home/huupv/Desktop/Untitled Document 1 is file

post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com