How to Sort Files by Non‑Executable in Terminal
Sorting files based on their executable status can be a handy way to organize a directory, especially when you are preparing backups, analyzing security permissions, or simply cleaning up a cluttered workspace. In this guide we will explore several solid methods to sort files by non‑executable directly from the terminal, using tools that are available on most Linux and Unix‑like systems. Each approach is explained with clear steps, practical examples, and tips for handling edge cases Simple, but easy to overlook. Nothing fancy..
Understanding Non‑Executable Files
Before diving into the commands, it’s useful to know what makes a file non‑executable. So in Unix‑like operating systems, a file is marked as executable if it has the execute permission set for the user, group, or others. This permission is represented by the letters x in the permission string shown by ls -l.
A non‑executable file lacks any x bits in its permission field. Because of that, this includes regular files, directories (unless they have the execute bit for traversal), symbolic links, and special files such as device nodes. Identifying these files programmatically is the first step toward sorting them.
Why Sorting Non‑Executable Files Matters
- Security audits – Spotting files that should not be executable helps detect potential malware or misconfigurations.
- Backup efficiency – Separating executable binaries from data files can streamline backup scripts and reduce restore time.
- Project organization – Keeping source code, configuration files, and documentation distinct from binaries improves workflow clarity.
- Automation – Many scripts rely on knowing which files are safe to process without triggering accidental execution.
By mastering the techniques below, you can quickly generate lists, move, copy, or delete non‑executable files, or feed them into further processing pipelines.
Methods to Sort Files by Non‑Executable in Terminal
Several ways exist — each with its own place. Choose the method that best fits your environment, the size of your directory tree, and the level of detail you need.
Using find and sort
The find command can locate files and evaluate their permissions, while sort orders the output alphabetically It's one of those things that adds up. Less friction, more output..
find /path/to/directory -type f ! -perm -111 -printf "%p\n" | sort
-type f– Limit to regular files (you can replacefwithdfor directories).! -perm -111– Negate the test for any execute permission (user, group, or others).-111means “no x bits set anywhere”.-printf "%p\n"– Print each matching path on a new line.| sort– Alphabetically sort the list.
Result: A clean, sorted list of all non‑executable files under the specified path Practical, not theoretical..
Using ls and grep
ls can display permissions in a human‑readable format, and grep can filter out lines that contain an x Simple, but easy to overlook..
ls -l /path/to/directory | grep '^-.*---' | sort
ls -l– Long listing with permissions.grep '^-.*---'– Keep only lines that start with a dash (regular file) and have three hyphens in the permission slots (no execute bits).sort– Sort the filtered list.
Result: Sorted non‑executable files, shown with their permissions for quick visual verification.
Using stat and awk
When you need more granular control—such as distinguishing between files that are executable only for the owner versus those executable for everyone—stat combined with awk provides flexibility.
find /path/to/directory -type f -exec stat -c "%n %A" {} + | awk '$2 !~ /x/ {print $1}' | sort
stat -c "%n %A"– Outputs the filename and full permission string.awk '$2 !~ /x/ {print $1}'– Print the filename only if the permission string contains nox.sort– Alphabetical ordering.
Result: A sorted list of non‑executable files with optional inclusion of permission details for debugging Simple, but easy to overlook..
Using find with xargs for Batch Operations
If you want to perform an action on each non‑executable file (e.g., copy them to another location), find can pipe matches to xargs.
find /path/to/directory -type f ! -perm -111 -print0 | xargs -0 -I {} cp {} /target/directory/
-print0and-0handle filenames with spaces or newlines safely.-I {}inserts each file into thecpcommand.
Result: All non‑executable files are copied to the target directory, preserving the original order as listed by find Which is the point..
Step‑by‑Step Guides
1. Quick List Generation (Beginner Friendly)
-
Open a terminal and deal with to the directory you want to examine.
-
Run the following command to see a sorted list of non‑executable files:
find . -type f ! -perm -111 -printf "%p\n" | sort -
Review the output. If you need to limit depth (e.g., only the current folder), add
-maxdepth 1:find . -maxdepth 1 -type f ! -perm -111 -printf "%p\n" | sort -
To save the list to a file, pipe it to
tee:find . On the flip side, -type f ! -perm -111 -printf "%p\n" | sort | tee non_executable_files.
2. Visual Inspection with Permissions
-
Use
lsto display permissions and filter out any line containing anx:ls -l | grep '^-.*---' | sort -
For a recursive view, add
-R:ls -lR | grep '^-.*---' | sort -
The output will show each non‑executable file alongside its permission bits, making it easy to spot files that accidentally have execute rights.
3. Advanced Filtering (Owner/Group Specific)
If you need to isolate files that are non‑executable for the owner only, you can test