How to tar all directories in a directory to tar files named after tared directories
Clash Royale CLAN TAG#URR8PPP
up vote
-2
down vote
favorite
I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh
dir___
|- dirA
|- dirB
|- tar-all.sh
becomes
dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh
linux tar
add a comment |Â
up vote
-2
down vote
favorite
I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh
dir___
|- dirA
|- dirB
|- tar-all.sh
becomes
dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh
linux tar
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh
dir___
|- dirA
|- dirB
|- tar-all.sh
becomes
dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh
linux tar
I want to tar all directories in a single directory to tar files named after themselves, so for example this directory of directories with tar-all.sh
dir___
|- dirA
|- dirB
|- tar-all.sh
becomes
dir___
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh
linux tar
linux tar
asked Aug 10 at 23:18
the_prole
1153
1153
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32
add a comment |Â
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
Use a for
loop:
#!/bin/bash
for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done
Results in:
> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz
add a comment |Â
up vote
0
down vote
The following will find and archive all the dir
directory's subdirectories (deleting the original subdirectories).
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
done
The pattern dir/*/
will only match the pathnames of the subdirectories under dir
and no non-directories, due to the /
at the end.
Alternatively, using the -C
option of tar
instead of an explicit cd
:
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
done
Testing:
$ tree
.
`-- dir
|-- a
| `-- file
|-- b
| `-- file
|-- c
| `-- file
`-- complicated name
`-- file
5 directories, 4 files
(run any of the two loops here)
$ tree
.
`-- dir
|-- a.tgz
|-- b.tgz
|-- c.tgz
`-- complicated name.tgz
1 directory, 4 files
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Use a for
loop:
#!/bin/bash
for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done
Results in:
> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz
add a comment |Â
up vote
0
down vote
Use a for
loop:
#!/bin/bash
for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done
Results in:
> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use a for
loop:
#!/bin/bash
for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done
Results in:
> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz
Use a for
loop:
#!/bin/bash
for f in *
do
echo "Creating tarball of $f..."
tar -zcf $f.tar.gz $f
done
Results in:
> ./tar-all.sh
> Creating tarball of tar-all.sh...
> Creating tarball of dirA...
> Creating tarball of dirB...
> DONE!
|- dirA.tar.tgz
|- dirB.tar.tgz
|- tar-all.sh.tar.gz
edited Aug 11 at 8:18
confetti
25513
25513
answered Aug 10 at 23:36
Eminent
112
112
add a comment |Â
add a comment |Â
up vote
0
down vote
The following will find and archive all the dir
directory's subdirectories (deleting the original subdirectories).
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
done
The pattern dir/*/
will only match the pathnames of the subdirectories under dir
and no non-directories, due to the /
at the end.
Alternatively, using the -C
option of tar
instead of an explicit cd
:
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
done
Testing:
$ tree
.
`-- dir
|-- a
| `-- file
|-- b
| `-- file
|-- c
| `-- file
`-- complicated name
`-- file
5 directories, 4 files
(run any of the two loops here)
$ tree
.
`-- dir
|-- a.tgz
|-- b.tgz
|-- c.tgz
`-- complicated name.tgz
1 directory, 4 files
add a comment |Â
up vote
0
down vote
The following will find and archive all the dir
directory's subdirectories (deleting the original subdirectories).
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
done
The pattern dir/*/
will only match the pathnames of the subdirectories under dir
and no non-directories, due to the /
at the end.
Alternatively, using the -C
option of tar
instead of an explicit cd
:
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
done
Testing:
$ tree
.
`-- dir
|-- a
| `-- file
|-- b
| `-- file
|-- c
| `-- file
`-- complicated name
`-- file
5 directories, 4 files
(run any of the two loops here)
$ tree
.
`-- dir
|-- a.tgz
|-- b.tgz
|-- c.tgz
`-- complicated name.tgz
1 directory, 4 files
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The following will find and archive all the dir
directory's subdirectories (deleting the original subdirectories).
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
done
The pattern dir/*/
will only match the pathnames of the subdirectories under dir
and no non-directories, due to the /
at the end.
Alternatively, using the -C
option of tar
instead of an explicit cd
:
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
done
Testing:
$ tree
.
`-- dir
|-- a
| `-- file
|-- b
| `-- file
|-- c
| `-- file
`-- complicated name
`-- file
5 directories, 4 files
(run any of the two loops here)
$ tree
.
`-- dir
|-- a.tgz
|-- b.tgz
|-- c.tgz
`-- complicated name.tgz
1 directory, 4 files
The following will find and archive all the dir
directory's subdirectories (deleting the original subdirectories).
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
( cd "$dir" && tar -cz -f "$dirname.tgz" "$dirname" && rm -rf "$dirname" )
done
The pattern dir/*/
will only match the pathnames of the subdirectories under dir
and no non-directories, due to the /
at the end.
Alternatively, using the -C
option of tar
instead of an explicit cd
:
for subdir in dir/*/; do
dirname="$( basename "$subdir" )"
tar -cz -f "dir/$dirname.tgz" -C dir "$dirname" && rm -rf "$subdir"
done
Testing:
$ tree
.
`-- dir
|-- a
| `-- file
|-- b
| `-- file
|-- c
| `-- file
`-- complicated name
`-- file
5 directories, 4 files
(run any of the two loops here)
$ tree
.
`-- dir
|-- a.tgz
|-- b.tgz
|-- c.tgz
`-- complicated name.tgz
1 directory, 4 files
answered Aug 12 at 8:49
Kusalananda
106k14209327
106k14209327
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461918%2fhow-to-tar-all-directories-in-a-directory-to-tar-files-named-after-tared-directo%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Go ahead and post the code you've written so far, so we can point you in the right direction.
â Emmanuel Rosa
Aug 10 at 23:32