For performance reasons, I want to use find
to list a large number of files, but also include a counter on each line. The following is what I have so far:
local root_dir="."
local ouptut_file="/tmp/foo.txt"
File_Number=99 ## Initial value
echo "" > "${ouptut_file}" ## Initialize file to empty
find "$root_dir"
-type f
-depth 2
-name '*.xxx'
-print0
| sort -z
| xargs -0 printf "DoSomething %5d '%s'\n" $[File_Number++] --
> "${ouptut_file}"
This outputs
DoSomething 99 '--'
DoSomething 0 'dirA/dirB/file1.xxx'
DoSomething 0 'dirA/dirB/file2.xxx'
DoSomething 0 'dirA/dirB/file3.xxx'
what I want is
DoSomething 100 'dirA/dirB/file1.xxx'
DoSomething 101 'dirA/dirB/file2.xxx'
DoSomething 102 'dirA/dirB/file3.xxx'
How do I include the counter and also eliminate the first spurious line in the output.
System: Am using bash
on macOS Ventura 13.0.1
find ... > file
and the file will be created if it doesn't exist and will be emptied and its contents replaced with the output of the command if it did exist. – terdon Nov 30 '22 at 12:10noclobber
option is set (withset -o noclobber
in~/.bashrc
), usefind ... >| file
which will clobber any already existing file upon redirecting output to them, something practical but unsafe by design ... – Cbhihe Nov 30 '22 at 18:21nl
command like thatfind ... | nl --starting-line-number=100
. It doesn't support zero terminated strings and also has no way of prepending output lines with custom text. – legolegs Nov 30 '22 at 19:09