It’ll run if no other action is specified. This action prints the resulting paths with newline characters in between. The action part in this example is the default action, -print. This command will result in a list of mp3 files in the Music directory and all its subdirectories. Music/Gustav Mahler/06 - Der Abschied.mp3 Music/Gustav Mahler/05 - Der Trunkene im Frühling.mp3 Music/Gustav Mahler/04 - Von der Schönheit.mp3 Music/Gustav Mahler/03 - Von der Jugend.mp3 Music/Gustav Mahler/02 - Der Einsame im Herbst.mp3 Therefore, if find returns a large number of files, we should consider piping the result to the xargs command.Music/Gustav Mahler/01 - Das Trinklied vom Jammer der Erde.mp3 Comparing to the find -exec rm approach, using xargs on this test is 120 times faster! Wow! This time, it took only 0.05 seconds to remove the files. We’ll delete 3000 files using each command and measure their execution time using the time command.įirst, let’s test with the find -exec rm approach: $ touch. Next, let’s do the same performance test with each approach and benchmark their performance. Therefore, if our find command returns a large number of files or directories, find | xargs COMMAND will be much faster than the find -exec COMMAND approach. 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 |