i want to grab the names of directorys to create a tar.gz file for each
for example, all directorys with beginning with A:
count = 10
for ((k = 1 ; k < $count ; k++));
do
tar -cf ADIRNAME.tar.gz ADIRNAME
sleep 1
done
how can i do this?
i want to grab the names of directorys to create a tar.gz file for each
for example, all directorys with beginning with A:
count = 10
for ((k = 1 ; k < $count ; k++));
do
tar -cf ADIRNAME.tar.gz ADIRNAME
sleep 1
done
how can i do this?
This will create a tarball for each A*
directory.
for dir in A*/; do
tar -cf "${dir%/}".tar.gz "$dir"
done
The wildcard expands to a list of directory names to loop over. The trailing slash which sets the matches off as directories will be included in the expansion; we trim it from the tarball's filename with a simple variable substitution (${variable%suffix}
expands to the variable's value with suffix
trimmed from the end if present; there is also a corresponding ${variable#prefix}
and a number of other substitutions; see your shell's man page.)
The double quotes are mandatory, although the script will work without them as long as there are no file names with whitespace in them. This is a common oversight even in many shell scripting tutorials.
it's not exactly clear what you need to do, but here are some bits to build upon:
find all directories:
find -maxdepth 1 -type d
find all directories starting with a certain letter:
find -maxdepth 1 -type d -name 'X*'
now you can get the list of the first letters:
for n in `find -maxdepth 1 -type d`; do echo ${n:2:1}; done | sort -u
and finally, perform tar'ing with those letters.