check if a given string is a file or directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if a given string is a file or directory
# 1  
Old 02-16-2012
check if a given string is a file or directory

hi i want to know how to do this

if the given is /tmp/ and it is a valid directory then it will echo directory

if the given is /tmp/file.txt and is a valid file then it will echo file..

thanks!
# 2  
Old 02-16-2012
MySQL

Assuming you are passing the direct or file name in command line as below

./test "/tmp"
Code:
#! /usr/bin/ksh

inpu=$1
 [ -d "$inpu" ] && echo "directory"
 [ -f "$inpu" ] && echo "file"

Output:
Code:
./test "/tmp"
directory

Thanks,
Kalai
# 3  
Old 02-16-2012
Code:
str="/tmp/abc"
if [ -f $str ]
then
  echo $str is a file
elif [ -d $str ]
then
  echo $str is a directory
else
  echo $str is neither a file nor directory
fi

# 4  
Old 02-16-2012
The -e -f and -d checks for test could be your friends here

Code:
#!/bin/bash
if [ -e $1 ] ; then
    if [ -f $1 ] ; then
        echo $1 is a file
    elif [ -d $1 ] ; then
        echo $1 is a directory
    else
        echo $1 exists but is neither a normal file nor a directory
    fi
fi


skrynesaver@busybox ~/tmp$ ./test.sh /etc/passwd
/etc/passwd is a file
skrynesaver@busybox ~/tmp$ ./test.sh /tmp
/tmp is a directory
skrynesaver@busybox ~/tmp$ ./test.sh /dev/sda
/dev/sda exists but is neither a normal file nor a directory
skrynesaver@busybox ~/tmp$

# 5  
Old 02-16-2012
just another one

Code:
ksh
chk(){
[[ $2 -eq 0 ]] && echo "$1 valid" || echo "$1 not valid"
}

[[ -d /tmp ]] && return 0 || return 1
chk "dir" "$?"

[[ -f /tmp/file.txt ]] && return 0 || return 1
chk "file" "$?"

Check the possible operand here
# 6  
Old 02-16-2012
Code:
 
a="/tmp"
b="tmp/file.txt"
 
 [ -f $a ] && echo "File" || ([ -d $a ] && echo "Directory")
 [ -f $b ] && echo "File" || ([ -d $b ] && echo "Directory")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check that at least one file exists in the directory.

There are some files with suffix dates like abc_20032019.dat abc_17032019.dat If at least one file exists then perform some operation else exit from execution. Korn shell ---------------------------------- array=($inputdir/abc*.dat) If ] ] then echo " file exits" else echo " file does... (10 Replies)
Discussion started by: Rajesh123
10 Replies

2. Shell Programming and Scripting

Check for file with a particular extension in a particular directory

Hi, I have a directory which I am passing in my script as a parameter. Parameter name has been set to $TCH_FILE_DIRECTORY. I want to know if there's atleast 1 (or more) files in this directory with the extension '.tch'. How can I find this using ksh. (4 Replies)
Discussion started by: Bhavesh Sharma
4 Replies

3. Shell Programming and Scripting

Check whether file exists in directory

Hi guys, I am beginner trying to learn unix. So any help is welcomed. My requirement is to check whether is a file exists in a particular directory or not. The directory path and filename are taken dynamically with user interaction. So the program should continue only if the $filename... (1 Reply)
Discussion started by: maris_markur
1 Replies

4. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

5. Shell Programming and Scripting

How to check if a filename in a directory starts with a certain string

Hello, Trying to iterate over set of file in current directory and check if the file name in that folder matches certain string. This is what I have so far. Here I am checking if the file name starts with nexus, if so echo file name to log file. Getting weird syntax errors. Any help is... (7 Replies)
Discussion started by: scorpioraghu
7 Replies

6. Shell Programming and Scripting

How to check if something is a file, directory or other?

I want to know how you would go about checking if something is either a file or a directory. mostly for argument validation stuff. I know -d is to see if its a directory but im guessing -f is for files?? (1 Reply)
Discussion started by: Waffles
1 Replies

7. Shell Programming and Scripting

How to check if a file exists in a directory?

I want to perform SQL *Loader operation only if a file named "load.txt" exists in a directory "/home/loc/etc". Please help how to check this with a if condition. (8 Replies)
Discussion started by: vel4ever
8 Replies

8. Shell Programming and Scripting

check if directory and file exists

cp $PATHLOGS/$DATE/*.* $TMP/logs_tmp/ cp $PATHLOGS/$DATE1/*.* $TMP/logs_tmp/ Before copying the files I have to check if the directory $DATE1 and $DATE2 exists. If directory exists then, check if the folder contains some files. if the file exists then, check if the file size is greater... (3 Replies)
Discussion started by: sandy1028
3 Replies

9. Programming

how to check if directory/file exist using c/c++

Hi there, how to check if directory/file exist using c/c++ under linux/unix. Thanks. Steven (2 Replies)
Discussion started by: steven88
2 Replies

10. Shell Programming and Scripting

how to check if directory/file exist using c/c++

Hi there,, how to check if directory/file exist using c/c++ under unix/linux? I can use access() under Window MFC. Thanks. Steven (1 Reply)
Discussion started by: steven88
1 Replies
Login or Register to Ask a Question