简体   繁体   中英

How to get the name of the executables files in bash with ls

I try to get the name of the executable files using ls -l. Then I tried to get the lines of ls -l which have ax using grep -wx but the result is not right : some executable files are missing (the .sh).

I just need the name of the executable files not the path but I don't know how ...

    user@user-K53TA:~/Bureau$ ls -l
total 52
-rwxrwxrwx 1 user user   64 oct.   6 21:07 a.sh
-rw-rw-r-- 1 user user   11 sept. 29 21:51 e.txt
-rwxrwxrwx 1 user user  140 sept. 29 23:42 hi.sh
drwxrwxr-x 8 user user 4096 juil. 30 20:47 nerdtree-master
-rw-rw-r-- 1 user user  492 oct.   6 21:07 okk.txt
-rw-rw-r-- 1 user user 1543 oct.   6 21:07 ok.txt
-rw-rw-r-- 1 user user  119 sept. 29 23:27 oo.txt
-rwxrwxr-x 1 user user 8672 sept. 29 21:20 prog
-rw-rw-rw- 1 user user  405 sept. 29 21:23 prog.c
-rw-rw-r-- 1 user user    0 sept. 29 21:58 rev
drwxrwxr-x 3 user user 4096 sept. 29 20:51 sublime

 user@user-K53TA:~/Bureau$ ls -l | grep -w x
drwxrwxr-x 8 user user 4096 juil. 30 20:47 nerdtree-master
-rwxrwxr-x 1 user user 8672 sept. 29 21:20 prog
drwxrwxr-x 3 user user 4096 sept. 29 20:51 sublime

Don't parse ls . This can be done with find .

find . -type f -perm /a+x

This finds files with any of the executable bits set: user, group, or other.

Use find instead:

 find -executable
 find -maxdepth 1 -type f -executable
 find -maxdepth 1 -type f -executable -ls

One can use a for loop with glob expansion for discovering and manipulating file names. Observe:

#!/bin/sh 

for i in *
do # Only print discoveries that are executable files
   [ -f "$i" -a -x "$i" ] && printf "%s\n" "$i"
done

由于接受的答案根本不使用ls

ls -l | grep -e '^...x'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM