0

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
yael
  • 13,106
  • 2
    https://unix.stackexchange.com/questions/153862/remove-all-files-directories-except-for-one-file – Kamaraj Aug 07 '18 at 08:30
  • 1
    we have couple files to exclude , this post talk about one file to exclude – yael Aug 07 '18 at 08: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 07 '18 at 08:45

3 Answers3

1

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
1

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
ss_iwe
  • 1,146
0

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 {} +
Vidur
  • 131