1

I have a very large number of files to delete, which go by the following format:

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-08_02-39-14.Z_2017-Feb-09_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-13_02-39-08.Z_2017-Feb-16_02-39-05.Z_2017-Feb-18_02-39-3.Z_2017-Feb-20_02-39-02.Z_2017-Mar-02_12-06-57

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-08_02-39-14.Z_2017-Feb-09_02-39-14.Z_2017-Feb-16_02-39-05.Z_2017-Feb-18_02-39-03.Z_2017-Feb-19_02-39-03.Z_2017-Feb-20_02-39-2.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-09_02-39-14.Z_2017-Feb-12_02-39-10.Z_2017-Feb-15_02-39-06.Z_2017-Feb-16_02-39-05.Z_2017-Feb-17_02-39-04.Z_2017-Feb-19_02-39-3.Z_2017-Feb-20_02-39-02.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-10_02-39-12.Z_2017-Feb-15_02-39-06.Z_2017-Feb-18_02-39-03.Z_2017-Feb-19_02-39-03.Z_2017-Feb-20_02-39-02.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-07_02-39-15.Z_2017-Feb-10_02-39-12.Z_2017-Feb-19_02-39-03.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

esymac_logEvents.log.5_2017-Feb-06_02-39-17.Z_2017-Feb-08_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-12_02-39-10.Z_2017-Feb-14_02-39-07.Z_2017-Feb-15_02-39-06.Z_2017-Feb-17_02-39-4.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

esymac_logEvents.log.6_2017-Feb-09_02-39-14.Z_2017-Feb-13_02-39-08.Z_2017-Feb-17_02-39-04.Z_2017-Feb-19_02-39-03.Z_2017-Feb-21_02-38-55.Z

esymac_logEvents.log.6_2017-Feb-07_02-39-15.Z_2017-Feb-08_02-39-14.Z_2017-Feb-11_02-39-11.Z_2017-Feb-12_02-39-10.Z_2017-Feb-14_02-39-07.Z_2017-Feb-15_02-39-06.Z_2017-Feb-17_02-39-04.Z_2017-Feb-22_02-39-05.Z_2017-Feb-23_02-39-09.Z

I want to delete them, but the rm() command gives me the 'Argument list too large' error. Couldn't formulate myself a command after checking similar posts, is there any way I could get a command that would:

  • first create argument list with all esymac_logEvents.log.* files,
  • remove the files one by one, with a loop or exec command (maybe not
    one at atime, it would be preferable to remove the max number of arguments rm() function can receive)

Cheers.

3
  • Are there files in this directory you need to keep ? What is the criteria for selecting the files to remove, or is this the whole list? – X Tian Mar 6 '17 at 16:42
  • Possibly a diuplicate: stackoverflow.com/questions/11289551/… – Valentin Bajrami Mar 6 '17 at 16:45
  • @Xtian I want to keep all files that differ from that format. – EXHALEXHALE Mar 6 '17 at 16:57
0
find /search/dir -name esymac*whatever*pattern* -exec rm \{\} \;
2
  • What is the difference between: -exec rm \{\} \; -exec rm {} \; – EXHALEXHALE Mar 6 '17 at 16:59
  • Depending on your shell you have to escape the braces. Now did the command work for you? – Philippos Mar 6 '17 at 17:08
0

Since you want to keep all files that differ from the format mentioned, do:

  1. Execute this. This will populate FilesToDelete.txt with files/dir to be deleted. Review this to make sure that files listed in are indeed the files you intend to delete.

    find /path/to/dir/esymac_logEvents.log* | xargs ls -l > FilesToDelete.txt

  2. Once you verify the Step-1, do

    find /path/to/dir/esymac_logEvents.log* -type f | xargs rm -f

  3. If directories are involved as well that needs to be deleted, Skip 2 and do this instead:

    find /dir/that/contains/esymac_logEvents.log* | xargs rm -rf

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