Software⏱️ 3 min read📅 2026-06-19

How to Fix: Error with find and ls

Error with find and ls command not sorting files by date

Quick Answer: The issue is likely due to the use of exec, which runs the ls command in a subshell. Try using -execdir instead.

The issue with 'find' and 'ls' not sorting files based on date is affecting users who are trying to manage their backups. This can be frustrating, especially when dealing with large amounts of data.

This problem is caused by the incorrect usage of the '-t' option in the 'find' command. The '-l' option works fine without the '-t', but when used together, it causes the sorting issue.

⚠️ Common Causes

  • The primary reason for this error is that the '-t' option is not correctly interpreted by the 'exec' function in the 'find' command. When used with '-exec', the '-t' option is ignored, causing the sorting to fail.
  • Another possible cause could be the file system or directory structure issues, which might prevent the 'find' command from accessing the files correctly.

✅ Best Solutions to Fix It

Correctly using the '-t' option with 'exec' in find

  1. Step 1: To fix this issue, you need to use the '-print0' option instead of '-exec' to avoid the sorting issue. Replace the '-exec' part with '-print0'. This will ensure that the files are sorted correctly based on date.
  2. Step 2: Additionally, make sure that the file system or directory structure is not causing any issues. Check if there are any permissions or access problems preventing the 'find' command from accessing the files.

Using an alternative method to sort files

  1. Step 1: As a backup, you can use the 'ls -t' command directly without using 'find'. This will give you the same results as using 'find' with the correct options.
  2. Step 2: You can also consider using other file management tools or scripts that are designed to handle sorting and organization of files based on date.

💡 Conclusion

In summary, the issue with 'find' and 'ls' not sorting files based on date is caused by incorrect usage of the '-t' option. By using the correct options and alternative methods, you can resolve this issue and manage your backups efficiently.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions