To explain this, let's make a short C program does nothing other than print out the parameters that are passed to it: #include int main( int argc, char *argv) | xxd Using quotes solves our problem, but why did it ever work in the first place? After all, we didn't use any quotes the first time, but we still got exactly the result that we wanted. ![]() We would have gotten the desired result of deleting only files that don't end with the. If the command was re-written to enclose the wildcard in single quotes like this: find. The key problem here is the lack of quoting around the wildcard used to specify the file extension. What Just Happened Here? Almost all of your files are now gone forever! py extension, with only a single file remaining. not -name *.py -deleteīut to your surprise, you find that you've just deleted way more files than you expected to, including most of the ones that end with the. So again, you decide to issue the exact same command that we just used with the intention of deleting all files that aren't python source files: find. However, this time the repository contains the following list of files that is slightly different. Later, you decide that you need to perform the exact same task of deleting any file that doesn't end in a. īecause this process worked so well, you decide to save this command so you can use it again in the future to impress your coworkers. not -name *.py -deleteĪfter inspecting the results, you find that this command successfully deleted exactly the set of files that you intended to. Now, because you want to actually delete the set of files that we just found, you decide to add the -delete flag and issue the command again: find. Which shows you the opposite set of files. Then, because you're interested in deleting any file that doesn't end in. py extension, you issue this command: find. To review a list of all files that are present in the repository, you decide to issue this command at the root of the project: find. Here are some commands to generate the specific listing of files we'll consider in this example: touch readme.txt ![]() py extension, and delete all other types of files. In this case, your goal is to keep only the python source files that end with a. The Happy CaseĪssume you're working within a source code repository, and you'd like to do some cleanup to get rid of unused files. For everyone else, let's review an example to illustrate what the problem is. For those of you who know about globbing, you already know what the error is and you don't need to read this article. What's even worse is that the error isn't likely to be obvious every time you use it. Unfortunately, there is one very subtle problem with this command that can cause you to make very serious errors if you use it. ![]() Right now, as I'm typing this, 4 of the top 4 Google results for "find command linux" contain at least one example of the pattern illustrated by the command above. A Surprisingly Common Mistake Involving Wildcards & The Find Command - By Robert Elderĭo you notice anything wrong with the following Linux command? find.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |