0

I want to compress multiple files at once (one command) but I don't want to put them into same directory or same file. How'd I do it?

  • If you consider a loop in one line as one command, you can loop over the to-be-compressed files, and in the loop compress each file individually. Otherwise, it depends on the compression-tool you're using, if it allows compressing multiple files individually. Tha manpage for that tool might help. – rathier Oct 02 '23 at 08:40

1 Answers1

2

One tool to compress a file is gzip. To compress lots of files you can check the documentation, man gzip, where it writes

SYNOPSIS
gzip [ -acdfhklLnNrtvV19 ] [-S suffix] [ name ... ]

DESCRIPTION
Gzip reduces the size of the named files […]. Whenever possible, each file is replaced by one with the extension .gz, while keeping the same ownership modes, access and modification times.

The last part of the synopsis, [ name … ] shows that it can take zero or more file names. (See man man for an explanation of the meta characters used in the Synopsis.) So if the files to compress are wheat, oats, and barley you could do this

gzip wheat oats barley

producing three separate compressed files, wheat.gz, oats.gz, and barley.gz.

Of course you can also use a wildcard, which the shell will attempt to match and expand to a list of files.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287