ls inside bash script didn't found just created file

07
2014-07
  • user710818

    bash script contains like:

    ./java_application
    ls logs/app.log
    

    java_application deletes and creates new log file: app.log

    when I run this bash script I receive:

    ls: logs/app.log  No such file or directory
    

    Is workaround exists?

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question

    Bash command for full path to a file
  • Questioner

    I would like an easy way to get the full path to a file. I currently type this:

    echo `pwd`/file.ext
    

    Trying to shorten it, I made a bash alias:

    alias fp='echo `pwd`/'
    

    But now if I type "fp file.ext' there is a space that appears between the "/" and the "file.ext".

    Does such a command already exist and I am missing it? If not, how would I go about creating such an alias or function in bash?


  • Related Answers
  • akira

    On linux systems, you should have readlink from the GNU coreutils project installed and can do this:

    readlink -f file.ext
    

    Debian/ubuntu systems may have the realpath utility installed which "provides mostly the same functionality as /bin/readlink -f in the coreutils package."

  • Gilles

    Instead of the pwd command, use the PWD variable (it's in POSIX as well):

    fp () {
      case "$1" in
        /*) printf '%s\n' "$1";;
        *) printf '%s\n' "$PWD/$1";;
      esac
    }
    

    If you need to support Windows, recognizing absolute paths will be more complicated as each port of unix tools has its own rules for translating file paths. With Cygwin, use the cygpath utility.

  • akira

    to answer your question with what you use right now:

    the alias expands at the position where you are typing right now. you typed:

    % fp<SPACE>file.ext
    

    this becomes

    % echo `pwd`<SPACE>file.exe
    

    you could use a function to avoid that:

    function fp() {
        echo `pwd`/"$1"
    }
    

    you can use that as usual:

    % fp file.ext
    
  • cYrus

    You can use:

    realpath file.ext