r/linuxquestions • • 6d ago

Resolved What GUI archive methods on Linux also keep the file's file extension?

Hi.

I notice in GNOME & KDE that when I compress a file, it drops the file extension. I'd rather keep the file extension as it makes searching for files a lot easier. I can type it in manually in the archive name just after the archive is created but that's tedious.

Any ideas?

Thanks.

0 Upvotes

26 comments sorted by

6

u/[deleted] 6d ago edited 6d ago

On Gnome you can add an entry to the Nautilus popup menu. See https://wiki.ubuntu.com/Nautilus_scripts

mkdir -p ~/.local/share/nautilus/scripts
nano ~/.local/share/nautilus/scripts/Compress-Gzip

Contents of the script:
------------------------------
#!/bin/bash
 
for file in "$@"; do
    if [[ -f "$file" ]]; then
        gzip -k -- "$file"
    fi
done
------------------------------

chmod u+x ~/.local/share/nautilus/scripts/Compress-Gzip

Then, right-click on the files you want to compress, select Scripts ⇨ Compress-Gzip.

You can adapt the script to use another compression command, or to remove the original...

2

u/Royaourt 6d ago

Perfect! Thank you. 👍🏻

1

u/Royaourt 6d ago

To clarify what I mean. I would like for example, somefile.html to become somefile.html.zip and not somefile.zip.

2

u/el_crocodilio 5d ago

There is a command switch to give the output any file name you like (with zip it's the first positional argument, or the -O option, etc)

Most utilities pipe their output to stdout so you can pipe this to a file named whatever you want.

1

u/Royaourt 5d ago

Ok, thank you.

2

u/truethug 6d ago

What if you have more than 1 file?

1

u/Royaourt 6d ago

I suppose you could have a script to archive each individually or use PeaZip or just put them into a folder and compress that.

0

u/truethug 6d ago

What extension would the folder have?

1

u/Royaourt 5d ago

None. Folders don't have extensions.

-1

u/truethug 4d ago

You have a very odd request.

0

u/el_crocodilio 4d ago

Just cause the OP's use case is not the same as yours, it's not necessarily "odd". Be kind.

4

u/ScratchHistorical507 6d ago

What do you mean by "it drops the file extension"? I mean sure, the resulting file will have the file extension of the resulting file and not of the files inside, but that's the whole point of file extensions, to tell the user what type of file it is. And with a quick test with File Roller that's exactly the behavior you're getting.

3

u/DP323602 6d ago

Aren't file extensions a DOS and Windows feature not required in Linux?

-1

u/Random_Dude_ke 6d ago

File extensions are extensively used, because for your documents you need to know whether you have a text file or music file or a spreadsheet.

Where Linux doesn't need extensions is programs you run. So in Windows the file must end in .exe (and some other extensions) for the system to know it is an executable file. In Linux you have the "executable" bit set and the first few bytes of the file tell it whether it is a binary (ELF file) or a script or whatever. But you still CAN name a shell script something.sh or python program myProgram.py so that it is obvious to you - the user - what it is without looking inside. Also text editors, for example, rely on a file extension to know that syntax highlighting to use.

6

u/Swedophone 6d ago

File extensions are extensively used, because for your documents you need to know whether you have a text file or music file or a spreadsheet.

Linux supports magic file patterns https://man.archlinux.org/man/magic.5 used by for example Gimp which only uses file extensions as a last resort.

While opening a file, GIMP must determine the file type. Unfortunately, the file extension, such as .jpg, is not reliable: file extensions vary from system to system; any file can be renamed to have any extension; and there are many reasons why a file name might lack an extension. GIMP first tries to recognize a file by examining its contents: most of the commonly used file formats have “magic headers” that permit them to be recognized. Only if the magic yields no result does GIMP resort to using the extension.

https://docs.gimp.org/2.10/en/gimp-using-fileformats-opening.html

1

u/Random_Dude_ke 6d ago

I - as an user - need to know what type of document the file is at a first glance in the file manager (krusader is my favorite) and I am not going to investigate the "magic number" at the beginning of the file. That is the last resort for me (been there, done that).

2

u/Swedophone 6d ago

The default file manager in Ubuntu (GNOME Files) doesn't require file extensions anyway.

1

u/knuthf 6d ago

It's from Unix. Here the file content is identified by a tag, ":/bin/bash" in the files. For all image files and video, this is used, except for Microsoft that refused and started to use "file extensions". The command to use is "file", see "man file". The extension is superfluous, just a possible confusion.

2

u/jr735 6d ago

A file manager, graphical or otherwise, tends to open your file with the right program, irrespective of the existence of a file extension.

1

u/DP323602 5d ago

I can usually remember documents types because I use descriptive file names!

3

u/ipsirc 6d ago

What is the purpose of archiving single files?

2

u/WetMogwai 6d ago

Compression. If you have a big file and you need it to take up less space, use a compressing archive type. Depending on what you're compressing, you could end up with a much smaller file.

2

u/ipsirc 6d ago

Ok, but you can enable implicit on-the-fly compression in filesystem nowadays if disk space matters to you.

2

u/WetMogwai 6d ago

That doesn’t help if I need to upload a file to a service that has a file size limit.

0

u/calraith 6d ago

You could create a zfs or btrfs partition to store the crap you want to archive.