Your command does not work as expected because of rm *PC-*-*.json
! it would expands by shell and includes all PC-x-x.json
files.
Actually you're using xargs
in a wrong way.
Something like this should do the job:
ls -Q1 | grep -v $hostname | grep -E "PC-1-[0-9]+\.json" | xargs echo
change echo
with rm
when you where sure that it works.
A better solution would be using find
:
find -type f -name "PC-*-*.json" -not -name "*$hostname*" -exec rm -i {} +
- remove
-i
fromrm
after you were sure that command is working correctly.