+ Reply to Thread
Results 1 to 5 of 5

Thread: Linux/Bash: Check if a file exists

  1. #1
    Tor
    Tor is offline Programming Expert
    Join Date
    Oct 2007
    Posts
    488
    Rep Power
    0

    Linux/Bash: Check if a file exists

    I use this little script in some of my larger bash scripts and thought I would share:

    Code:
    #!/bin/bash
    # Check if a file exists
    #
    # Usage: ./file.sh <filename>
    #
    
    if [ -f $1 ]
    then
        echo "$1 file exists"
    else
        echo "$1 file does not exist"
    fi
    Usage: ./file.sh <filename>

    Example:
    Code:
    # ./file.sh test
    Output:
    ./file.sh test
    Code:
    test file exists
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Linux/Bash: Check if a file exists

    -e checks if a file exist too.

  4. #3
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: Linux/Bash: Check if a file exists

    What does -e and -f even mean?

  5. #4
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Linux/Bash: Check if a file exists

    Those options belong to test.
    Run man test and you'll see a list of possible options and its meanings.
    Code:
    if [ -f $1 ]
    In shell script the [ ] indicate a test so you can omit the word test.

  6. #5
    relapse's Avatar
    relapse is offline Programming Expert
    Join Date
    Jul 2009
    Location
    Intrawebs
    Posts
    479
    Blog Entries
    2
    Rep Power
    0

    Re: Linux/Bash: Check if a file exists

    Oh, I see. There are several options that test a file for bash scripting:

    Code:
           FILE1 -ot FILE2
                  FILE1 is older than FILE2
    
           -b FILE
                  FILE exists and is block special
    
           -c FILE
                  FILE exists and is character special
    
           -d FILE
                  FILE exists and is a directory
    
           -e FILE
                  FILE exists
    
           -f FILE
                  FILE exists and is a regular file
    
           -g FILE
                  FILE exists and is set-group-ID
    
           -G FILE
                  FILE exists and is owned by the effective group ID
    
           -h FILE
                  FILE exists and is a symbolic link (same as -L)
    
           -k FILE
                  FILE exists and has its sticky bit set
    
           -L FILE
                  FILE exists and is a symbolic link (same as -h)
    
           -O FILE
                  FILE exists and is owned by the effective user ID
    
           -p FILE
                  FILE exists and is a named pipe
    
           -r FILE
                  FILE exists and read permission is granted
           -s FILE
                  FILE exists and has a size greater than zero
    
           -S FILE
                  FILE exists and is a socket
    
           -t FD  file descriptor FD is opened on a terminal
    
           -u FILE
                  FILE exists and its set-user-ID bit is set
    
           -w FILE
                  FILE exists and write permission is granted
    
           -x FILE
                  FILE exists and execute (or search) permission is granted

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Check if multiple remote files exists HELP! (Using curl)
    By SeanStar in forum PHP Development
    Replies: 7
    Last Post: 12-17-2010, 09:06 AM
  2. Check if a file exists?
    By Lop in forum Pascal and Delphi
    Replies: 3
    Last Post: 01-06-2007, 10:54 PM
  3. Check if a file exists
    By Lop in forum C and C++
    Replies: 4
    Last Post: 08-07-2006, 08:03 PM
  4. Check if a File Exists
    By NeedHelp in forum C# Programming
    Replies: 4
    Last Post: 08-07-2006, 03:20 PM
  5. Check if a URL Exists
    By Chan in forum PHP Development
    Replies: 1
    Last Post: 07-13-2006, 10:54 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts