Loading
View RSS Feed

Goldsonam's Blog

How To Indentify Files or Directory Using Shell Script?

Rating: 4 votes, 5.00 average.
The following program in shell scripting takes a name from the user and checks whether it is a file or a directory.
If directory, it shows its contents its contents(files and folders in it)
If file, it checks whether it is readable or writable.
If readable,checks if it has a non zero size
If size is non-zero,it prints the file.



The following are the syntax's which will help you understanding the code:

[ -f $name ] : If name is a valid file name,it returns true.

[ -d $name ] : If name is a valid directory name,it returns true.

[ -r $name ] : If name is a valid readable file name,it returns true.

[ -w $name ] : If name is a valid writable file name,it returns true.

[ -x $name ] : If name is a valid executable file name,it returns true.

[ -s $name ] : If name is a valid non zero size file name,it returns true.


Code:
Code:
echo "ENTER A NAME"
read a
if [ -d $ a ]
  then
      ls  $a

elif if [ -f $ a ]
  then
      if [ -r $ a ]
         then
              if [ -s $ a ]
                   then 
                          cat $a
              fi
      fi

else
     echo "INVALID FILE/DIRECTORY NAME"
fi

Submit "How To Indentify Files or Directory Using Shell Script?" to Digg Submit "How To Indentify Files or Directory Using Shell Script?" to del.icio.us Submit "How To Indentify Files or Directory Using Shell Script?" to StumbleUpon Submit "How To Indentify Files or Directory Using Shell Script?" to Google

Updated 03-03-2011 at 06:36 PM by Harsh

Categories
Unix

Comments




Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.