I am using the following command to retrieve the number of files which names contains sv
or json
in a given directory in a remote server:
nbs_files=`ssh -q -i ${sshkey} ${user}@${server} "find ${path}/ -maxdepth 1 -mindepth 1 -type f -name '*sv*' -o -name '*.json' -exec basename {} \; | wc -l"`
This command returns only the number of .json
files, whereas files with sv
in their names exist in the ${path}
.
When I remove the -o -name '*.json'
part, the command works well, and returns the number of files containing the 'sv' in their names.
Does anyone know how can I modify the command in order to retrieve the files containing sv
in their names and the files with the extension .json
as well?
-exec basename {} \;
will give the same results but without the cost of running a process per matching file. If the OP is using gnu find - likely given thelinux
tag, thennbs_files=$(ssh -q -i ${sshkey} ${user}@${server} "find ${path}/ -maxdepth 1 -mindepth 1 -type f '(' -name '*sv*' -o -name '*.json' ')' -printf '%f\n' | wc -l")
will avoid the process per matching file even with embedded newlines in the directory names. – icarus Jul 05 '20 at 07:02wc
. Just something likefind ... -printf '\n' | wc -l
would do – ilkkachu Jul 05 '20 at 15:14