I am using Git bash and wanted to archive my files with the "tar" function in git Bash.
As you can see, these files have dates in their name and I want to tar all dates in separate tar files. The result would be in this case that I have 2 tar files with 2 different dates.
My idea was to "find" the 6 digits in these files with find iname "jo* | \d\d\d\d\d\d | sort | tar -czvf testarchive.zip ~/test/targetfolder
and then tar them. But then, I have to put the first found dates in a list, tar them until the date changes and then put the second datefiles in a list and so on...
Since I have no experience with bash and scripting, I dont know how to solve this problem. I would be very happy for help. I was not able to find a solution in the internet yet..
PS I am not sure if git bash is Linux/Unix or a different script language or if it is just Git bash, so sorry if i didnt meet up all the requirements.
At first glance, I'd build an associative array of unique dates, then tar
each set that matched.
$: echo jo* # I made some similar local files
jo02042018ab jo02042018cd jo10112018ab jo10112018cd jo10112018ef
Filenames aren't exact, but close enough to make the point.
$: declare -A dateLst # create an *associative* array, keys are dates
An associative array uses strings as indexes (even if they are numbers).
$: for f in jo[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]?? # for each file
> do dateLst["${f//[^0-9]/}"]=1 # assert date as a key
> done
dateLst[somekey]=1
creates a unique somekey
in dateLst
.
${f//[^0-9]/}
removes all non-numerals from the filename, leaving only the date. (This will probably work fine even if they actually have .txt
filename extensions, though you'll need to edit the glob in the for
statement.)
So, dateLst["${f//[^0-9]/}"]=1
sets the date from the file as a key in the lookup table. If it already exists, it just sets it again.
We now have a table of unique dates we can use to create tarballs.
$: for t in "${!dateLst[@]}"; do echo tar -cvzf $t.tgz jo$t*; done
tar -cvzf 10112018.tgz jo10112018ab jo10112018cd jo10112018ef
tar -cvzf 02042018.tgz jo02042018ab jo02042018cd
${!dateLst[@]}
is the keys from the table, so this loops over the distinct dates and echo
s the tar
command to create each tarball. jo$t*
lists all matching files to be included in that archive. Remove the echo
to execute the commands.
Perhaps you are looking for something like this?
for pat in jo[0-9][0-9][0-9][0-9][0-9][0-9]*; do
tail=${pat#jo[0-9][0-9][0-9][0-9][0-9][0-9]}
head=${pat%"$tail"}
date=${head#jo}
# If this tarball exists, skip
if [ -e "$date.tar.gz" ]; then
continue
fi
# Else, tar this date
tar czvf "$date.tar.gz" "jo$date"*
done
It's unclear where or how you hoped ~/test/targetfolder
would be used. If that's where the files are, perhaps just cd
there before running this. If that's where you want the tar files to be created, put it before "$date.tar.gz"
everywhere in the above.
The parameter expansions ${variable#pattern}
and ${variable%pattern}
produce the value of $variable
with, repectively, any prefix or suffix match on pattern
removed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.