Skip to content

[Linux] Compress Files And Contain Symbol Links

I have respectively noted about some compression commands and how to use symbol link to point to file:

Today, what I want to record is how to "compress the file and contain the symbol link"


It will be problematic to use the zip command to compress directly

As a demonstration, I already have an "A folder" pointing to myself infinitely today


Originally, in the A folder, there will be both "A folder's own symbol link" and a file named test.txt.



If we go to the next level of the symbol link, we will also see this folder and a test.txt.

In this way, it can be proved that the symbol link really points to the original A folder.

So if we directly use the zip command to compress, what will happen to the symbol link folder?

mkdir B
zip -r B/A.zip A/
unzip B/A.zip -d B/

In this way, we compressed a copy of the original A folder and decompressed it in the B folder. Now we use the du command to see the difference between the two folders?

du A/

Output:

This is the original folder. Since the symbol link underneath points to itself, it does not take up space. And what about the decompressed ones?

du B/A/

Output:

very impressive! And each symbol link folder is now a separate folder, so it will take up a lot of space.

The reason why it looks good today is because there is only one test.txt file under my folder, if I put it in the data today Are there pictures and videos in the folder?

This is the danger of compressing folders with symbol links.


Use the -ry parameter

Back to the topic, when using the zip command to compress a folder with a soft link, we should add the -ry parameter.

rm -r B/*

zip -ry B/A.zip A/
unzip B/A.zip -d B/

So what is the difference?

du A/

Output:

這張圖片的 alt 屬性值為空,它的檔案名稱為 image-20.png

Here is the original file. The following is the newly unzipped folder A:

du B/A

Output:

As you can see, the symbol link is effective.


References

Tags:

Leave a Reply