LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Search for files w/extension, then zip them to a different directory? (https://www.linuxquestions.org/questions/linux-newbie-8/search-for-files-w-extension-then-zip-them-to-a-different-directory-4175656434/)

xpac 06-26-2019 12:31 PM

Search for files w/extension, then zip them to a different directory?
 
Hi, basically I'm trying to find files with a certain extension in a directory (this part works fine), and then zip those files to a different location.

This works fine for zipping them to the location they are found in:

Code:

find ../somedirectory -type f -name "*.txt" -exec zip "$(basename {})"zip {} \;
However I need to send the zip file to a different directory so I'm trying this with no luck:

Code:

find ../somedirectory -type f -name "*.txt" -exec zip "/someotherdirectory/$(basename {})"zip {} \;
I get this:
Code:

zip I/O error: No such file or directory
I'm sure this is something simple I am missing?

Turbocapitalist 06-26-2019 01:30 PM

The command substitution seems to be not getting the {} placeholder filled in time. Try it in a shell:

Code:

find ../somedirectory -type f -name "*.txt" \
    -exec sh -c 'zip "/someotherdirectory/$(basename {})".zip {}' \;

It will slow it down but it should work.

BW-userx 06-26-2019 03:17 PM

puts it all into one zip file
Code:

find path -type f -name "*.txt" -print | zip path/nameOfFile.zip -@

xpac 06-26-2019 04:32 PM

Quote:

Originally Posted by Turbocapitalist (Post 6009336)
The command substitution seems to be not getting the {} placeholder filled in time. Try it in a shell:

Code:

find ../somedirectory -type f -name "*.txt" \
    -exec sh -c 'zip "/someotherdirectory/$(basename {})".zip {}' \;

It will slow it down but it should work.

Thanks, I got it working using this :)


All times are GMT -5. The time now is 07:24 AM.