0

I am making a little .desktop creator thing, where users provide a path. Path can be a link application or directory. Is there any direct way to test if is app? I can do test if its a directory.

Thanks

2 Answers 2

5

See man bash, and you can do things like:

   -a file
          True if file exists.
   -d file
          True if file exists and is a directory.
   -e file
          True if file exists.
   -f file
          True if file exists and is a regular file.
   -u file
          True if file exists and its set-user-id bit is set.
   -x file
          True if file exists and is executable.
    -L file
          True if file exists and is a symbolic link.

and many, many more.

You can also look "inside" the file with the file command (see man file) to gain more information.

Rather than parsing the output of ls (which always leads to eventual confusion), one should use /usr/bin/stat -c "%a %n" filename:

$ stat -c "%a %n" .bashrc
700 .bashrc
0
-3

Test if it's an application? You can check to see if it's executable:

ls -l {path} | cut -d " " -f 1 | grep -c x

Will return 0 if not executable, or a positive integer if executable.

5
  • Thanks @Daniel, but what about .sh files that launch? So like binary files?
    – Noitidart
    Sep 6, 2015 at 3:32
  • 2
    -1 This is terrible. A simple [ -x /path/to/file ] is all that's needed, not this Rube Goldberg machine.
    – muru
    Sep 6, 2015 at 4:49
  • What @muru said. Not to mention all sorts of problems with whitespace. Or that it will consider any directory you have access to as an executable. This is really not a good solution at all.
    – terdon
    Sep 6, 2015 at 11:34
  • But it works and is simple to remember or reuse/script
    – Daniel
    Sep 7, 2015 at 0:37
  • -1 Don't parse the output of ls! smallo.ruhr.de/award.html#ls Sep 7, 2015 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .