Reputation: 4930
I'm writing a bash script that should be able to count the number of json files within a specified folder.
What I'm doing right now is this:
hdfs dfs -ls /path/to/files/*.json | grep -E '^-' | wc -l
When there is at least one file, it returns the number of results, but when there is no json file, I'd expect the result to be 0, since the path exists but it doesn't contain any files matching the *.json
pattern.
What I'm getting however, is an error:
ls: `/path/to/files/*.json': No such file or directory
Is this the expected behavior?
Upvotes: 3
Views: 4485
Reputation: 850
Yeah , that is the default behaviour. Since these are error messages, they are sent to stderr not stdout.
In case you need to suppress the messages, you can use it like :-
hdfs dfs -ls <hdfs_location>/*.json -R 2>/dev/null | grep -E '^-' | wc -l
(It would show only 0 (with no error) if no files with the matched extension is found ).
You can also refer : - https://unix.stackexchange.com/questions/82698/how-to-mute-ls-command
Upvotes: 2