Before I recursively tar a directory i want to be able to delete all of the executables. In windows I would have simply deleted all of the files with .exe extensions. And I cannot simply delete all files with the executable mode since that would delete shell scripts as well. So is there any way to delete only non-shell script script files in a directory automatically?
feedback
|
|
I assume you mean binaries? If the number of executables is very large, this command may fail due to the command line for "file" and "echo" being too long. A quick example:
If you replace "echo" with "rm" in the above example, those files will be deleted. The "grep" command should prevent libraries from being deleted (you can test on /lib). (Note, I only have access to FreeBSD right now, so the output of linux's "file" command may differ, thus changing what you need to do.) This should give you an easy template for how to do it. You'll just need to be sure to know what to look for in the output given by "file" and grep accordingly. Be careful. You could totally bork your system if you run this as root on the wrong directory. | |||
|
feedback
|
Or perhaps:
;-) | |||
|
feedback
|
The magic here is that the -perm flag (for permissions) can take a / preceding the permission argument, which causes it to search for a logical OR on each of the bits. From the man page:
In case that wasn't clear, / specifies 111 specifies x in the user OR x in the group OR x in the others. And those are not XOR, so we're looking for at least one but up to 3. Since the unix file permissions are
And we care about the x bits, we get a mask of
or 111 It should be noted that in the command listed above, there's an echo to prevent you from shooting yourself in the foot. Feel free to take of the safety once you'd nailed the files correctly. EDIT OK, this blows away all shell scripts, too. There is no nice, neat way to do it with find that I've found, due to the relatively primitive regular expression matching. I can find all of the .sh files you want using regular expressions:
But I can't invert that at all. So I give up. I'm still leaving this up here, in case it's useful to someone. Write a shell script, or use someone else's suggestion, or if you really want to, just temporarily remove executable permissions to the shell scripts, remove all the executable files, then add the +x back. Good luck. | |||||||||
feedback
|
IMHO the short answer is that there is no 100% foolproof way to do this. Depending on permissions alone may miss or include things you don't want. Depending on filenames won't work. Even depending on the output of the file command probably isn't a good idea, I have seen it miss-identify things on several occasions. | |||
|
feedback
|
|
You need
You really want to delete the files replace the final Note: all the | |||
|
feedback
|
|
You probably have to write a shell script to do this. Off the top of my head, run file first, get the executables and exclude all the scripts. Then you can operate on the remainder which should be what you are looking for. | |||
|
feedback
|
|
Using bits from Matt's answer I came up with this:
| |||
|
feedback
|