Finding executable files using ls and grep
Do you need to use ls? You can use find to do the same:
find . -maxdepth 1 -perm -111 -type f
will return all executable files in the current directory. Remove the -maxdepth flag to traverse all child directories.
You could try this terribleness but it might match files that contain strings that look like permissions.
ls -lsa | grep -E "[d\-](([rw\-]{2})x){1,3}"
How to get the name of the executables files in bash with ls
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.
Search for executable files using find command
On GNU versions of find you can use -executable
:
find . -type f -executable -print
For BSD versions of find, you can use -perm
with +
and an octal mask:
find . -type f -perm +111 -print
In this context "+" means "any of these bits are set" and 111 is the execute bits.
Note that this is not identical to the -executable
predicate in GNU find. In particular, -executable
tests that the file can be executed by the current user, while -perm +111
just tests if any execute permissions are set.
Older versions of GNU find also support the -perm +111
syntax, but as of 4.5.12 this syntax is no longer supported. Instead, you can use -perm /111
to get this behavior.
How to use grep command to list all the files executable by user in current directory?
OK -- if you MUST use grep:
ls -l | grep '^[^d]..[sx]' | awk '{ print $9 }'
Find all executable files that depend on the specified library in the specified directory
The trick is to execute a shell script rather than a single command to be able to re-use the file name.
finddepend() {
# Arg 1: The directory where to find
# Arg 2: The library name
basedir=$1
libname=$2
find "$basedir" \
\( -perm -100 -o -perm -010 -o -perm -001 \) \
\( -type f -o -type l \) \
-exec sh -c '
# Arg 0: Is a dummy _ for this inline script
# Arg 1: The executable file path
# Arg 2: The library name
filepath=$1
libname=$2
objdump -p "$filepath" 2>/dev/null |
if grep -qF " NEEDED $libname"; then
printf %s\\n "${filepath##*/}"
fi
' _ {} "$libname" \;
}
Example usage:
finddepend /bin libselinux.so
mv
systemctl
tar
sed
udevadm
ls
mknod
systemd
mkdir
ss
dir
vdir
cp
systemd-hwdb
netstat
Using the ls command to hide non-executable files
The way to go :
for file in *; do test -x "$file" || echo "$file"; done
Thanks to not parsing ls
output
List files with certain extensions with ls and grep
Why not:
ls *.{mp3,exe,mp4}
I'm not sure where I learned it - but I've been using this.
List all executables in the cwd with grep
ls -F | grep -E "[*]\>"
This won't do what you are expecting because *
is not a "word" character and looking for an end-of-word boundary immediately after it makes no sense.
ls -F -a | grep "[*]$"
Will yield all lines ending with *
as specified. This makes sense.
You should heed @Adam's advice: You should not parse the output of ls
.
Related Topics
Nginx as Cache Proxy Not Caching Anything
What Is the Use of Various Qt Platform Plugins
Is There a Good Way to Detect a Stale Nfs Mount
Getting Message "Sudo: Must Be Setuid Root", But Sudo Is Already Owned by Root
Track the Time a Command Takes in Unix/Linux
Getting Sudo and Nohup to Work Together
How to Run R Scripts on Servers Without X11
Differencebetween .Got and .Got.Plt Section
"Couldn't Find a File Descriptor Referring to the Console" on Ubuntu Bash on Windows
List File Using Ls Command in Linux with Full Path
Finding Executable Files Using Ls and Grep
Given a Linux Username and a Password How to Test If It Is a Valid Account
Simple Way to Convert Hh:Mm:Ss (Hours:Minutes:Seconds.Split Seconds) to Seconds
Linux Rename Files to Uppercase
How to Export Dbus_Session_Bus_Address
Isolate Kernel Module to a Specific Core Using Cpuset