How to zip multiple files into separate archives?

1,733

The solution is pretty easy. If you want to do this for every file, recursively, use find. It will list all files and directories, descending into subdirectories too.

find . -type f -execdir zip '{}.zip' '{}' \;

Explanation:

  • The first argument is the directory you want to begin in, .
  • Then we will restrict it to find files only (-type f)
  • The -execdir option allows us to run a command on each file found, executing it from the file's directory
  • This command is evaluated as zip file.txt.zip file.txt, for example, since all occurrences of {} are replaced with the actual file name. This command needs to be ended with \;

Of course, find has more options. If instead you just want to stay in your current directory, not descending into subdirectories:

find . -type f -maxdepth 1 -execdir zip '{}.zip' '{}' \;

If you want to restrict it to certain file types, use the -name option (or -iname for case-insensitive matching):

find . -type f -name "*.txt" …

Anything else (including looping with for over the output of ls *) is pretty ugly syntax in my opinion and likely to break, e.g. on files with spaces in their name or due to too many arguments.

Share:
1,733
Tarun Maganti
Author by

Tarun Maganti

Updated on September 18, 2022

Comments

  • Tarun Maganti
    Tarun Maganti over 1 year

    This seems highly unlikely, but is there a way I could mock a static method in a certain way and the second time in another way?

    A case scenario -

    if(StringUtils.isEmpty("")) {
       throw Exception();
    }
    ...
    if(StringUtils.isEmpty("")) {
       doSomething();
    }
    

    The test is written in following way.

    mockStatic(StringUtils.class);
    when(StringUtils.isEmpty("")).thenReturn(false);
    /*Is it possible to make this behaviour run only once such that second time
    when it is called we can mock it again.*/
    limitStaticMock(times(1));//Is this possible?
    when(StringUtils.isEmpty("")).thenReturn(true);//Setting behaviour again.
    
    • chrylis -cautiouslyoptimistic-
      chrylis -cautiouslyoptimistic- about 7 years
      You really shouldn't be altering what amounts to a pure function. Assuming your string ultimately comes from a parameter, run with differing parameters in different test cases.
    • Tarun Maganti
      Tarun Maganti about 7 years
      @chrylis But, to make it a true unit test, shouldn't I mock any external dependencies and StringUtils is from another framework (apache.commons)
    • chrylis -cautiouslyoptimistic-
      chrylis -cautiouslyoptimistic- about 7 years
      No, you should only mock external behavior that can change, such as the response from a database call. You wouldn't mock 1 + 2. The use of StringUtils rather than an inline check (or, for example, using Groovy's asBoolean) is an implementation detail. Test the different cases--"" will never be anything but empty.
    • Tarun Maganti
      Tarun Maganti about 7 years
      @chrylis Got ya. I found a link related to what you said. Mocking Objects in FP
    • GhostCat
      GhostCat about 7 years
      Thanks for the quick acceept!