Do you often deal with compressed archives? It is easy to do so in your desktop, but in the server environment, it can be difficult to manage compressed files. With Midnight Commander, you can quickly select a bunch of files and folders and compress them to a GZ archive. You can also extract the contents of a BZ archive or enter those archives and either add or extract specific files to and from them. Let’s see how.
Although Midnight Commander (MC for short) is not preinstalled in most Linux distributions, you can find it in their repositories. To install it on Ubuntu, Debian, and compatible distributions, enter your favorite terminal and use:
sudo apt install mc
If you are on Red Hat, you can install it with:
sudo yum install mc
Arch fans can bring it onboard using:
sudo pacman -S mc
For Suse, try:
sudo zypper install mc
Windows fans aren’t left out since there is a native port of Midnight Commander for for the Microsoft OS, compatible with 32-bit Windows XP or newer. You can download the installer from its SourceForge page and install it as you would any other application on your computer.
After installing Midnight Commander, you can run it from your terminal by typing
Midnight Commander presents you with two panes. The active one you are currently on (by default, the left one) is used as the source for every file action. The other one works as the destination. If you select a bunch of files on the left pane and press F5, which maps to “copy,” the selected files will be copied to the directory presented in the right pane.
You can move between the two panes by pressing Tab on your keyboard. The 10 Function Keys work as shortcuts for the functions presented on the bottom of Midnight Commander’s screen.
By default, MC uses filenames for sorting. Since you want all your image files bundled together, sort contents based on file extensions.
Press F9 to access MC’s pulldown menus.
Choose “Sort order … ” and change it to “Extension” from the options that appear.
After that, all files in the left pane will be sorted based on their extensions instead of the filenames.
Compress Individual Files and Folders
Scroll up/down the list and press Insert to select (or deselect) entries. MC displays selected entries with a different color.
To compress all selected entries into an archive, press F2 to access the file menu. Move with the cursor keys to “Gzip or gunzip tagged files” or “Bzip2 or bunzip2 tagged files,” and press Enter to choose the format you prefer for your archives. It’s even quicker if you use the shortcut: Y for Gzip and B for Bzip2.
After a temporary jump to a terminal, you will find a compressed version of your files in the same folder.
Compressing all selected files to a single archive
What if you wanted to compress all the selected files into a single archive? Midnight Commander doesn’t include such a function by default, but you can do it semi-manually.
Select a bunch of files as before.
Press F2 to access the file menu once again. This time press @ or select “Do something on the tagged files.” This option allows you to manually input a command that will act on the file selection.
Use your preferred archiver’s command for adding multiple files into a single archive but replace the files themselves with
%s. This parameter corresponds to “everything selected in the active pane.”
For example, to compress files in 7z format, we used:
7z a ARCHIVE_NAME.7z %s
Soon after, an archive with the selected files appeared in the same folder.
Extracting compressed archives is even easier with MC. Thanks to using a virtual filesystem to mount them as volumes, it allows you to act on the contents without extracting and recompressing them.
To extract a whole archive, select it and then press F2. Press Z or select “Extract compressed tar files to subdirectories” to do that. With MC, though, you don’t have to extract everything if you only need specific files or folders.
With your archive highlighted, press Enter to enter it. Yes, we know this sounded redundant, but it’s precisely what happens since MC allows you to get inside your archives and manage the content.
Just like with any normal folder, while inside an archive, you can select individual files and folders and press F5 to copy (extract) them to the destination pane. You can also press F6 to move them there, which means that after being extracted to your destination pane, they will be removed from your archive. You can also press F8 to remove something from your archive.
It’s also worth noting that if you want to jump back to a typical terminal for a while, you don’t have to exit Midnight Commander. Press Ctrl + O simultaneously, and MC will hide in the background, leaving you in the terminal where you launched it.
You can move back to MC using the same shortcut. To really exit MC, press F10 on your keyboard.
Another way to better manage compressed files is to create a self-extracting archive so you don’t have to worry about installing additional applications.
Our latest tutorials delivered straight to your inbox