Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Use one line command to run compress directoies

Hi, I have an application to compress each directory. There are 20 directoies.
TarApp 1
TarApp 2
TarApp 3
...
TarApp 20

Open in new window

Of course we can use a loop in a batch file.
#!/bin/bash
for (( i=1; i<21; i++ ))
do
	TarApp $i
done

Open in new window


Now I just want one line command to accomplish it.
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

I suppose that you don't mean:
for (( i=1; i<21; i++ )); do TarApp $i; done

Open in new window


If TarApp is a shell script, then you could modify that to accept multiple arguments.  If it is the "tar" command, then it already accepts mutlitple arguments.  In either case, you can then say:
TarApp 1 2 3 4 5 6 7 8 9 010 11 12 13 14 15 16 17 18 19 20

Open in new window

for t in $(find /home/ -type d -mindepth 1 -maxdepth 1); do tar -czf /path/to/archives/${t}.tar.gz ${t}/; done

Open in new window


for t in $(find /home/ -type d -mindepth 1 -maxdepth 1); do TarApp ${t}/; done

Open in new window

for t in $(seq 1 20); do TarApp ${t}; done
Avatar of zhshqzyc
zhshqzyc

ASKER

TarApp is a batch file.
#!/bin/bash
tar cvzf $1.tgz $1

Open in new window

You need to get dirnames from command line?
Just change the script to

#!/bin/bash
for name in $*
do
  tar cvzf ${name}.tgz $name
done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial