Search code examples
shellfindzipxargs

Why cannot compress all files with find and zip?


I have the following folder structure

.
├── 5.avi
├── a
│   ├── 1.avi
│   ├── b
│   │   └── 2.avi
│   └── c
│       └── 3.avi
└── b
    ├── 4.avi
    ├── l
    └── s

I am using the following command to compress all avi files:

find .  -iname *avi | xargs zip videos.zip

The problem is that find only returns 5.avi, but if the 5.avi doesn't exist, find returns the rest of the files.

How can solve it and comprime all avi files?


Solution

  • The shell expands asterisks (*) before it passes the arguments to the command (find in your case). The actual command will look like

    find . -iname 5.avi
    

    You can test that by prepending echo to your command, e.g.

    echo find . -iname *avi
    

    What you want to do is passing *avi as-is to the command. For that, you must put the asterisk in quotes, or escape it with a backslash. Your command should be:

    find . -iname '*.avi' | xargs zip videos.zip