I have multiple files in a folder like:


And I want to use 7z to compress single files, generating 1.7z, 2.7z 3.7z... I'm trying to use this:

for file in {1..100} ./{1..100}.txt; do 7z a $file; done

But with no luck.

Alternatively, is there a way to automatic use the txt names?

  • The cheap and dirty way to do it would be to have your target archives look like 1.txt.7z instead of 1.7z. I'm sure you could script something using basename file .txt if you're not cheap and dirty – infixed Jun 6 '16 at 19:13

The simplest way is likely

for file in {1..100}; do 7z a "$file.7z" "$file.txt"; done

Alternatively, you could use parameter expansion of the form ${var%suf} to remove the suffix e.g.

for file in {1..100}.txt; do 7z a "${file%.txt}.7z" "$file"; done
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.