I want to compress log files by specific months. Listing all files with following commands

ll | grep 'Sep' | grep 'oim_server'

Then I need to compress with tar result of it How can I do it?

  • it is not clear what you want to do. And the 'Sep' and 'oim_server' strings are part of the logfile name ? Also, you want to compress the files individually, or you want to tar all files from one month together ? Oct 10 '16 at 13:24
  • Aim is to compress log files which are created in September and contains oim_server. " ll | grep 'Sep' | grep 'oim_server' " provieds what i want. Then i need to compress
    – Furkan
    Oct 10 '16 at 13:34
  • you did not answered my questions. You have just repeated what you said before. Oct 10 '16 at 13:37
  • Want to tar all files from one month together
    – Furkan
    Oct 10 '16 at 13:38
  • 2
    Please add those clarifications to your question. Don't leave them in the comments as it simply adds confusion ("which is more correct, the question or some changes in the comments?").
    – roaima
    Oct 10 '16 at 13:49

Find all the files modified in september :

find . -type f -newermt 2016-09-01 ! -newermt 2016-10-01

Append a file to an archive :

tar -rvf archive.tar filename

Now combine the two using exec :

find . -type f -newermt 2016-09-01 ! -newermt 2016-10-01 -exec tar -rvf logs_sept.tar {} +
  • I think you could also create the archive at once by ending the exec param by {} + instead of {} \; to use results as the parameters of a single tar call
    – Aaron
    Oct 10 '16 at 14:17
  • That's right, I'm editing the answer. However depending on the number of files, find may still call tar multiple times. Thus tar -r is still needed.
    – MikaDo-
    Oct 10 '16 at 14:21
  • Oh also, you haven't implemented the "filename to archive must contain oim_server" requirement, I suppose you should add a -name check
    – Aaron
    Oct 10 '16 at 14:26
  • That only archives the files for September for 2016, not other years. That also doesn't compress them. (though it may actually be doing what the OP wanted as opposed to what he asked for) Oct 10 '16 at 14:41

If you do want to archive the non-hidden files (of any type including directory) last-modified in September of any year and whose name contains oim_server (which looks like the intent of your ll | grep 'Sep' | grep 'oim_server' command though that one would also match on user/group/filename/symlink target containing Sep or oim_server), on a GNU system, you could do:

find . -mindepth 1 -maxdepth 1 ! -name '.*' \
       -name '*oim_server*' -printf '%Tm:%p\0' |
  sed -zn 's/^09://p' |
  tar --null --no-recursion -T - -cvvf - |
  xz > file.tar.xz

(--no-recursion being to not also archive the content of those directories last-modified in some September month).

Note that tar doesn't compress, it just archives (lumps files with data and metadata together onto a tape or file so they can be restored as-is later). You pipe its output to a compressor like xz above to obtain a compressed archive. Some tar implementations like GNU tar can call those compressors by themselves (-J/--xz for xz compression in recent versions of GNU tar).

If you wanted to archive the files that show-up in the output of your filtered ls, you'd do:

ll | grep Sep | grep oim_server |
  awk '{print $NF}' |
  tar  --no-recursion -T - -cvvf - |
  xz > file.tar.xz

Which you can simplify to:

ll | awk '/Sep/ && /oim_server/ {print $NF}' |
  tar  --no-recursion -T - -cvvf - |
  xz > file.tar.xz

However note that it has quite a few limitations:

  • The grep Sep searches for Sep on the whole line, not just the month field. That includes the user and group name, the file name and the target of symlinks.
  • Sep will be used for files last modified in September only in some English locales.
  • like for Sep above the oim_server could be found anywhere on the line
  • file names can contain newline characters, so assuming that a file name is only on one line is wrong in the general case.
  • worse, awk '{print $NF}' prints the last word of each line. That means it won't work for file names containing blanks or for symlinks (where it will output the last word of the target of the symlink instead)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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