[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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.
0
zhshqzyc
Asked:
zhshqzyc
  • 4
  • 3
1 Solution
 
simon3270Commented:
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

0
 
pilson66Commented:
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

0
 
pilson66Commented:

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

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pilson66Commented:
for t in $(seq 1 20); do TarApp ${t}; done
0
 
zhshqzycAuthor Commented:
TarApp is a batch file.
#!/bin/bash
tar cvzf $1.tgz $1

Open in new window

0
 
pilson66Commented:
You need to get dirnames from command line?
0
 
simon3270Commented:
Just change the script to

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

Open in new window

0
 
simon3270Commented:
Then call it as:
    TarApp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now