-1

I'm on linux an I want to know how to find the latest executable file in a directory? I already know how to find the latest with:

ls -rt1 | tail -1

but how to filter out executable files?


EDIT: I found a solution:

find path/to/dir/myfile* -perm /u=x,g=x,o=x -mtime 0 | tail -1

is this save? or is there a better solution??

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user1895268
  • 1,559
  • 3
  • 11
  • 23

1 Answers1

1

Given the basic find command to look for files starting on current directory:

find . -type f

Let's add functionalities:

  • To find executables you can use the -executable option:

    find . -type f -executable
    
  • To just find on one level of depth, that is, not within subdirectories, use the -maxdepth 1 option:

    find . -maxdepth 1 -type f
    
  • To find last modified file in a directory, you can use How to recursively find the latest modified file in a directory?:

    find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
    

All together, this looks for last modified executable file in one level depth:

find . -maxdepth 1 -type f -executable -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Ok but i want to get the **latest** executable of a specific directory not all the executables. i found a solution i added to my post look at the edit note. I want to ask if this is save or is there a better solution – user1895268 Sep 03 '14 at 14:41
  • Did you read the full answer? Last command returns a single file, you just have to replace `find .` for `find /your/dir`. – fedorqui Sep 03 '14 at 14:42