linux - Bash grabbing directory name to use in loop -
i want grab names of directorys create tar.gz file each
for example, directorys beginning a:
count = 10 ((k = 1 ; k < $count ; k++)); tar -cf adirname.tar.gz adirname sleep 1 done
how can this?
this create tarball each a*
directory.
for dir in a*/; tar -cf "${dir%/}".tar.gz "$dir" done
the wildcard expands list of directory names loop over. trailing slash sets matches off directories included in expansion; trim tarball's filename simple variable substitution (${variable%suffix}
expands variable's value suffix
trimmed end if present; there corresponding ${variable#prefix}
, number of other substitutions; see shell's man page.)
the double quotes mandatory, although script work without them long there no file names whitespace in them. common oversight in many shell scripting tutorials.
Comments
Post a Comment