Unzip files which contain "/" in file name

Hello everyone - just scratching my head and cannot find a proper solution…

I have got a zip file that includes many files with a / inside the file name,
e.g. foo [comment1 / comment2].txt. When i unzip it creates the folder

foo [comment1 and inside that folder there is comment2].txt

so now i get many folders with files inside that needs to be renamed to the original name. I tried unzip -j which only eliminates the folder structure but the filenames are still messed up. Some files even got several / in their names as separator which makes it even worst. There are too many to manually do the work. And as well there must be a solution to keep the / inside the file name instead of the Linux subdirectory separator.

Thanks a lot for any suggestions.

What filesystem are you targeting?

Isn’t / a reserved character in POSIX filesystems?

Yeah but it will result in creating directory file/ with file name.txt inside, instead of file file/name.txt

Ideally he should somehow replace / with something else in the process before writing filename containing /

P.S. @OsZ i don’t know the answer to help yet, but this zip really SUCK :laughing:
Who the hell uses characters like this…

You would either have to rename them before unzipping or re-assemble and rename them after unzipping.

You could try something like this to replace the / characters before unzipping.

I would definitely make a backup of the zip file before trying that.

People who aren’t using a POSIX filesystem where it works fine. :cowboy_hat_face:

1 Like

I use EXT4 but the zip files are stored on a Windows network at work. No chance to change the file names on the Windows side as they are automatically exported from another program. So the .zip file which includes these files is given.

Now I need to find a way how I can extract it reasonably to my Endeavour system at home to keep working on them. And yes, the Linux system does exactly what it should when recognizing /.

Maybe during the unzip operation the / could be changed into a &. As those files don’t go back into the system at work the separator symbol doesn’t matter really but for the analysis I need the full information that unfortunately is given by the file name that now splits up into different folders. I am not good at shell and pipes so would have no idea how to make it.

Do the zip files have actual sub-directories in them? If they don’t, it would be trivial to write a script that fixed the filenames and pulled them out of the directories after extraction.

If not, you would have to do something like I posted in the link above.

1 Like

There are no folders at all in the zip file, only text files.

If you give me a few minutes, I should be able to write something that does that for you.

2 Likes

@dalto may I?

find . -mindepth 2 -type f | while read file; do new_file=$(echo "${file}" | sed 's,/,_,g' | sed 's,^\._,,'); mv "${file}" "./${new_file}"; done

@OsZ when you run the above code form the top-most directory of your extracted filed it should bring every file there and rename all / to _ (the chatacter _ is defined in sed sequences).
Be careful I did not test it thoroughly. You can test the output first by for example replacing
mv "${file}" "./${new_file}"
with
echo "${file}"; echo "./${new_file}"

So it looks like

find . -mindepth 2 -type f | while read file; do new_file=$(echo "${file}" | sed 's,/,_,g' | sed 's,^\._,,'); echo "${file}"; echo "${new_file}"; done

All you have to do after that is to delete all folders (they should be empty).

Sadly I don’t think you can have / in file’s name.

1 Like

Thanks a lot. This brings at least the files names together again. Gonna work more on this tomorrow. Will retort back then. Cheers!

1 Like

May I ask why is it so important to have / as a part of the file’s name?
If you want to reference to the file from some application then foo/bar.txt expects bar.txt file inside foo directory. Therefore what happened at first is the correct state (even though it may look like a mess).

I know some industrial PLC systems do not have a directory structure - everything is in one directory. Then someone may have a tendency to use / as a valid character. In this case I would expect you would implement download/upload sequence which would replace slash character with some valid-not-so-common character (like = for instance) when you download the file and turn them all back when you upload it.