chown all files based on file name pattern in current directory
chown hadoop:hadoop ChownFileNames*
This should work for what you need:
find . -maxdepth 1 -iname 'ChownFileNames*' -exec chown hadoop:hadoop -- {} . \;
The |
(“pipe”) symbol means to pass the output of the command on the left to the command on the right. The command find . -maxdepth 1
lists the files in the current directory (plus .
itself). The command grep 'ChownFileNames*' -exec chown hadoop:hadoop -- {} . \;
doesn't make any sense: you're passing find
options to the grep
command.
find
itself has a way to match file names, the -name
predicate. It takes a shell wildcard pattern as argument.
You could use grep
to filter the list, but you'd have to provide a valid command line to grep
. To retain only file names matching the shell pattern ChownFileNames*
, you would need to use the regular expression /ChownFileNames[^/]$
. Then you would have to parse the output of grep
to transform the list of matching names into command line arguments. Assuming that you're running Linux and the file names do not contain any newlines, you could use xargs
:
find . -maxdepth 1 |
grep '/ChownFileNames[^/]*$' |
xargs -d '\n' chown hadoop:hadoop
It is a lot simpler and more robust to use find
's -exec
action. Note that you can use -exec … {} +
to run the command for a batch of files at once instead of once per file. You can also make find
list the current directory.
find . maxdepth 1 \( -name . -o -name 'ChownFileNames*' \) -exec chown hadoop:hadoop {} +
There's usually no point in running find
when you're only matching files by name (and not e.g. by date) in the current directory (without traversing subdirectories). Unless there are so many files in the current directory that this makes the command line too long, just use
chown hadoop:hadoop . ChownFileNames*