

( basename takes only 1 path argument but xargs will send them all (actually 5000) without -n1. It gives the correct result and it's the fastest ¯\_(ツ)_/¯ $ alias f='time find /Applications -name "*.app" -type d -maxdepth 5' \į -print0 | xargs -0 -n1 basename | wc -l \į -print0 | xargs -0 -n1 -P 8 basename | wc -l \Ġm01.17s real 0m00.20s user 0m00.93s systemĠm01.16s real 0m00.20s user 0m00.92s systemĠm01.05s real 0m00.17s user 0m00.85s systemĠm00.93s real 0m00.17s user 0m00.85s systemĠm00.88s real 0m00.12s user 0m00.75s systemįunnily enough i cannot explain the last case of xargs without -n1.

exec and -execdir are slow, xargs is king. If you want to use grep (but I think it's. maxdepth 1 -name 'string' -name ':' -print. If you want to avoid file containing ':', you can type: find. $ time sh -c 'find /usr/lib -type f -print0 | xargs -0 -n 1 basename | cksum'Īs you can see, it really is substantially faster to avoid launching basename every time. It will find all files in the current directory (delete maxdepth 1 if you want it recursive) containing 'string' and will print it on the screen. $ time sh -c 'find /usr/lib -type f -print0 | xargs -0 basename -a | cksum' Open the ERRORLOG file in the instance root directory using Notepad or other text editors. (For sake of a like-with-like comparison, the timings reported here are after an initial dummy run, so that they are both done after the file metadata has already been copied to I/O cache.) I have piped the output to cksum in both cases, just to demonstrate that the output is independent of the method used. Here is a timing comparison, between the xargs basename -a and xargs -n1 basename versions. Here I've included the -print0 and -0 (which should be used together), in order to cope with any whitespace inside the names of files and directories. dir1/dir2/file.txt and I want to get file.
#UBUNTU FIND FILE WITH NAME HOW TO#
If you use the -a option on basename, then it can accept multiple filenames in a single invocation, which means that you can then use xargs without the -n 1, to group the paths together into a far smaller number of invocations of basename, which should be more efficient.Įxample: find /dir1 -type f -print0 | xargs -0 basename -a How to only get file name with Linux 'find' Ask Question Asked 12 years, 3 months ago Modified 3 years, 1 month ago Viewed 382k times 352 I'm using find to all files in directory, so I get a list of paths. | xargs -n 1), which may potentially be slow. As others have pointed out, you can combine find and basename, but by default the basename program will only operate on one path at a time, so the executable will have to be launched once for each path (using either find.
