Search code examples
linux7zip

Is it possible to compress files without keeping the structure in Linux?


I have the following directory structure:

A0
├── A1
│   ├── A1_B1
│   │   ├── A1_B1_1.docx
│   │   ├── A1_B2_2.pptx
│   ├── A1_B2
│   │   └── A1_B2_C1
│   │       ├── A1_B2_C1_D1
│   │       │   ├── A1_B2_C1_D1_1.docx
│   │       │   └── A1_B2_C1_D1_2.docx
│   │       └── A1_B2_C1.xlsx
├── A2
└── A0.txt

I want to create a .7z file that will contain only the files. I don't want to keep the folders. I have tried this answer and this answer but they don't work in Linux.
Is it possible to do it with 7z or I should extract files to a single directory first and then compress.


Solution

  • If for some reason the answers you reference to don't work try this instead.

    Create a directory

    mkdir flat_dir
    

    Link all files from the desired folder recursively in flat_dir, for me the desired folder was cpptest.

    for full_path_file in $(find ../cpptest -type f)
    do
    echo "$full_path_file"
    filename=$(echo "$full_path_file" | rev | cut -d '/' -f 1 | rev)
    echo "$filename"
    ln -s -T "$full_path_file" "$filename"
    done
    

    Zip the files

    7z a test.zip -l ~/flat_dir