Under /var/log/hive we have a lot of log files
we want to remove all files exclude the following that should be not removed
hivemetastore.log
hiveserver2-report.json.tmp
hivemetastore-report.json.tmp
Under /var/log/hive we have a lot of log files
we want to remove all files exclude the following that should be not removed
hivemetastore.log
hiveserver2-report.json.tmp
hivemetastore-report.json.tmp
As stated in the question 153862 mentioned by Kamaraj, use find. Just use multiple entries of ! -name
:
$ ls
hivemetastore.log hivemetastore-report.json.tmp hiveserver2-report.json.tmp 1 2 3 4
$ find . ! -name 'hivemetastore.log' ! -name 'hivemetastore-report.json.tmp' ! -name 'hiveserver2-report.json.tmp' -type f -exec rm -f {} +
$ ls
hivemetastore.log hivemetastore-report.json.tmp hiveserver2-report.json.tmp
Try this,
To get the list of files other than hivemetastore.log ,hiveserver2-report.json.tmp, hivemetastore-report.json.tmp
:
find /var/log/hive -type f ! -name "hivemetastore.log" ! -name "hiveserver2-report.json.tmp" ! -name "hivemetastore-report.json.tmp" -print
To delete and get the list of deleted files:
find /var/log/hive -type f ! -name "hivemetastore.log" ! -name "hiveserver2-report.json.tmp" ! -name "hivemetastore-report.json.tmp" -delete -print
Going off of @Kamaraj's comment - you can use find
to look for multiple files using the -o
switch, and then negate it and use -exec
to rm
the rest.
List all files except the ones named:
find . -type f ! \( -name 'hivemetastore.log' -o -name 'hiveserver2-report.json.tmp' -o -name 'hivemetastore-report.json.tmp' \)
Chain an exec
to rm
those files:
find . -type f ! \( -name 'hivemetastore.log' -o -name 'hiveserver2-report.json.tmp' -o -name 'hivemetastore-report.json.tmp' \) -exec rm -f {} +