Search code examples
bashshellzip

Bash script to zip files and folders with matching names to another directory


I have many folders and files of the following type:

├── name 1.htm
├── name 2.html
├── name 1_files 
│   ├── some files
├── name 2_files 
│   ├── some files

I need for each .htm or .html file to find the appropriate folder with the same name, but with a "_files" at the end.

Then I need to put the found file and folder in another folder, for example, '000' and zip it.

Then I need to move the resulting .zip file to another folder, for example, 'Found'.

I'm a newbie to bash and scripting in general so any help would be appreciated.


Solution

  • Here is a zip-by-prefix.sh script that you can run from the directory where the the htm* files are present :

    mkdir -p found
    ls *.htm* > htmlfiles.txt
    cat htmlfiles.txt | cut -d"." -f1 | uniq > htmlprefixes.txt
    PREFIXES=$(echo `cat htmlprefixes.txt`)
    for prefix in $PREFIXES
    do 
    mkdir -p 000$prefix
    cp $prefix* 000$prefix
    zip -r 000$prefix.zip 000$prefix
    mv 000$prefix.zip found
    done
    rm *.txt
    

    Explanation

    • Line 1 : Create the found directory if it does not exist
    • Line 2-4 : Extract the htm or html unique prefixes to a PREFIXES variable
    • Line 5-11 : Loop through PREFIXES variable array to:
      • create 000-prefix directory such as 0001,
      • copy the prefix htm or html files, and the prefix dir to the 0001 directory,
      • zip the 0001 directory to a 0001.zip file,
      • now move the 0001.zip file to the found directory.
      • Repeat the steps for every prefix such as 2,3,4, etc.

    Finally check if the "found" directory contains all the zip files