0

I have lots of files that I want to archive/compress using 7zip utility. They all reside in the same folder. Each archive must have the same name as the file that is to be archived.

For example, if the files are 1.txt, 2.txt, 3.txt then the archives should be 1.7z, 2.7z and so on.

I have found some batch scripts, but I need a bash script.

I can list all the files using

for i in *.txt; do echo $i; done

but cannot make it work with 7zip command, i.e. 7z a 'archive.7z' 'file.txt'

heikrana
  • 131
  • 6

1 Answers1

1
for i in *.txt; do 7z a "${i%%.*}.7z" "$i"; done

This command seems to work. If your file name contains spaces, then try setting the delimiter to line-break. I achieved it by using this command IFS=$'\n'.

${i%%.*} this thing was used to remove the extension .txt in my case. If you want your archive to look like this .txt.7z then simply use $i.7z and it will work.

jesse_b
  • 35,934
  • 12
  • 91
  • 140
heikrana
  • 131
  • 6