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

  • 2
  • 1
    we have couple files to exclude , this post talk about one file to exclude
    – yael
    Aug 7, 2018 at 8:33
  • this is not duplicate as I mentioned the post talk about one file for exclude and I need couple file to exclude
    – yael
    Aug 7, 2018 at 8:45

3 Answers 3


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

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 {} +

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.