Find out File and Directory Exist with Conditional Expressions

AID: 262
  • Status: Published

2580 points

With the help of BASH shell and IF command it is possible to find out if file exists or not. Generally, this is known as conditional expressions. Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons. General syntax:
[ parameter FILE ]
OR
test parameter FILE
Where parameter can be any one of the following:
-e: Returns true value if file exists
-f: Return true value if file exists and regular file
-r: Return true value if file exists and is readable
-w: Return true value if file exists and is writable
-x: Return true value if file exists and is executable
-d: Return true value if exists and is a directory
Examples
Find out if file /etc/passwd file exists or not
Type the following commands:
$ [ -f /etc/passwd ] && echo "File exists" || echo "File does not exists"
$ [ -f /tmp/fileonetwo ] && echo "File exists" || echo "File does not exists"

                                    
1:
2:

Select allOpen in new window



Find out if directory /var/logs exists or not
Type the following commands:
$ [ -d /var/logs ] && echo "Directory exists" || echo "Directory does not exists"
$ [ -d /dumper/fack ] && echo "Directory exists" || echo "Directory does not exists"

                                    
1:
2:

Select allOpen in new window



You can use conditional expressions in a shell script:
#!/bin/bash
FILE=$1
 
if [ -f $FILE ];
then
   echo "File $FILE exists"
else
   echo "File $FILE does not exists"
fi

                                    
1:
2:
3:
4:
5:
6:
7:
8:
9:

Select allOpen in new window



Save and execute the script:
$chmod  x script.sh
$./script.sh /path/to/file
$./script.sh /etc/resolv.conf

                                    
1:
2:
3:

Select allOpen in new window

Asked On
2008-11-21 at 08:52:11ID262
Tags

Linux

,

Bash

,

Shell Scripting

Topic

Linux Administration

Views
2041

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top Linux Admin Experts

  1. maeltar

    4,300

    0 points yesterday

    Profile
    Rank: Guru
  2. xterm

    4,000

    0 points yesterday

    Profile
    Rank: Sage
  3. noci

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  4. duncan_roe

    2,800

    0 points yesterday

    Profile
    Rank: Genius
  5. pcsmitpra

    1,800

    0 points yesterday

    Profile
    Rank: Wizard
  6. testez

    1,800

    0 points yesterday

    Profile
    Rank: Guru
  7. Nopius

    1,470

    30 points yesterday

    Profile
    Rank: Genius
  8. abbright

    1,000

    0 points yesterday

    Profile
    Rank: Guru
  9. torakeshb

    1,000

    0 points yesterday

    Profile
    Rank: Master
  10. woolmilkporc

    800

    0 points yesterday

    Profile
    Rank: Genius
  11. dbauermann

    600

    0 points yesterday

    Profile
  12. wasimibm

    600

    0 points yesterday

    Profile
    Rank: Guru
  13. unassassinable

    420

    0 points yesterday

    Profile
    Rank: Guru
  14. Anton74

    390

    0 points yesterday

    Profile
    Rank: Guru
  15. kosarajudeepak

    340

    0 points yesterday

    Profile
    Rank: Wizard
  16. abolinhas

    320

    0 points yesterday

    Profile
    Rank: Guru
  17. viju2008

    300

    0 points yesterday

    Profile
    Rank: Master
  18. farzanj

    300

    0 points yesterday

    Profile
    Rank: Genius
  19. OP_Zaharin

    300

    0 points yesterday

    Profile
    Rank: Sage
  20. ahmedhamdy27

    290

    0 points yesterday

    Profile
  21. allen-davis

    280

    10 points yesterday

    Profile
    Rank: Master

Hall Of Fame