0

So I followed solution here: Script to create folder with same name as file and move file into folder. I want to create folder with same name as file in a folder, and move them into that folder.

for file in *; do if [[ -f "$file" ]]; then mkdir "${file%.*}"; mv "$file" "${file%.*}"; fi; done

And it works perfectly fine.
But after doing that, I want to zip those folders into individual zip files, and I followed solution here: command to zip multiple directories into individual zip files.

for file in *; do zip -r "${file%.*}.zip" "$file"; rm -R "$file"; done

The question is, how can I combine those 2 into 1-time command? I'm new to coding and I still don't know how to combine those 2 into 1 command.

0

1 Answer 1

0

You can combine them in a script file. However second command delete files after compressing. So you need to add a location parameter.

Create a file text file called zipfoldered.sh (with gedit or nano) make it executable

sudo chmod +x zipfoldered.sh

paste this into the file

#!/bin/bash
(cd $1
for file in *; do if [[ -f "$file" ]]; then mkdir "${file%.*}"; mv "$file" "${file%.*}"; fi; done
for file in *; do zip -r "${file%.*}.zip" "$file"; rm -R "$file"; done )

You can now call the script with a location to run :

./wherever_it_is/zipfoldered.sh myFolder

myFolder being the folder where the files to be zipped are located ...

you can remove location parameter removing (cd $1 and the ) at the end. So that the script will run locally but surely it will compress and delete itself too ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .