![]() ![]() Benchmarking the Performance of find -exec and find | xargs To learn the answer, let’s discuss their performance. We may ask: If find -exec rm can solve the problem, why do we need to introduce an extra xargs process to do the same? Let’s restore the test directory and test it: $ find test -type d -name '.git' | xargs rm -rĪs the output above shows, all. Next, let’s see how to remove all whatever.txt files using this approach: $ find test -name 'whatever.txt' | xargs rm Alternatively, we can also pipe the result of the find command to xargsand let xargs call the rm command to delete those files. Now, we’ve learned that we can execute the rm command using find‘s – exec action. ![]() If the -delete action is at the first position, during its evaluation, it’ll delete the given directory and everything in it, which is the test directory in our example. However, we shouldn’t forget that find treats them as three expressions as well.Īn expression in the find command will be evaluated, returning a boolean value, and the -delete action always returns true. We can call the three options: -delete, -type d, and -name ‘.git’. However, when we check the result, we’ve found that the test directory has been deleted completely! Let’s understand why it has happened. ![]() It means the command has been executed successfully. Ls: cannot access 'test': No such file or directory Now, let’s re-order the options in our last find command by moving the -delete option to the first position and see what will happen: $ find test -delete -type d -name '.git' We know that the order of options of a Linux command doesn’t usually matter.įor example, the following two ls commands are identical, even though the options are in a different order: ls -F -a -l -color These commands ignore the Stdin.įor instance, when we execute the command “ rm file“, rm accepts the command-line argument file, which is indicating a file. Typical examples are those commands doing file handling, for example, cp, mv, and rm. However, not all Linux commands support reading from Stdin. We can see this kind of “command chain” pretty often in the real world. We can pipe the Stdout to further commands that support reading from Stdin, for example: $ ls -1 / | grep '^m' | sed 's/^m/OK_m/' This command works because the grep command accepts reading from the Stdin. Simply put, here, the pipe converts the standard output (Stdout) of ls to standard input (Stdin) of the grep command. In the simple example above, we pipe the ls command’s result to grep and find out the root directories whose names begin with “m”. First of all, let’s see an example: $ ls -1 / | grep '^m' We need to understand what the pipe does before we answer this question. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |