Reputation: 5762
I have a directory with files coming for every day. Now I want to zip those files group by dates. Is there anyway to group/list the files which landed in same date.
Suppose there are below files in a directory
-rw-r--r--. 1 anirban anirban 1598 Oct 14 07:19 hello.txt
-rw-r--r--. 1 anirban anirban 1248 Oct 14 07:21 world.txt
-rw-rw-r--. 1 anirban anirban 659758 Oct 14 11:55 a
-rw-rw-r--. 1 anirban anirban 9121 Oct 18 07:37 b.csv
-rw-r--r--. 1 anirban anirban 196 Oct 20 08:46 go.xls
-rw-r--r--. 1 anirban anirban 1698 Oct 20 08:52 purge.sh
-rw-r--r--. 1 anirban anirban 47838 Oct 21 08:05 code.java
-rw-rw-r--. 1 anirban anirban 9446406 Oct 24 05:51 cron
-rw-rw-r--. 1 anirban anirban 532570 Oct 24 05:57 my.txt
drwxrwsr-x. 2 anirban anirban 67 Oct 25 05:05 look_around.py
-rw-rw-r--. 1 anirban anirban 44525 Oct 26 17:23 failed.log
So there are no way to group the files with any suffix/prefix, since all are unique. Now when I will run the command I am seeking I will get a set of lines like below based on group by dates.
[ [hello.txt world.txt a] [b.csv] [go.xls purge.sh] [code.java] ... ] and so on.
With that list I will loop through and make archive
tar -zvcf Oct_14.tar.gz hello.txt world.txt a
Upvotes: 3
Views: 884
Reputation: 19717
Create zero-seperated list of (Month_Day.tar FILENAME) pairs and use xargs to add each file to the corresponding archive:
find . -maxdepth 1 -mindepth 1 -type f -printf "%Tb%Td.tar\0%f\0"|xargs -n 2 -0 tar uf
Upvotes: 0
Reputation: 124648
If you have the GNU version of the date
command, you can get the date of modification of a file with the -r
flag, which can be very useful.
For example, given the file list in your question, date +%b_%d -r hello.txt
will output Oct_14
.
Using this, you could loop over the files, and build up tar files:
Like this:
#!/usr/bin/env bash
tarfiles=()
for file; do
tarfile=$(date +%b_%d.tar -r "$file")
if ! [ -f "$tarfile" ]; then
tar cf "$tarfile" "$file"
tarfiles+=("$tarfile")
else
tar uf "$tarfile" "$file"
fi
done
for tarfile in "${tarfiles[@]}"; do
gzip "$tarfile"
done
Pass the list of files you want to archive as command line parameters, for example if /path/to/files
is the directory where you want to archive files (listed in your question), and you save this script in ~/bin/tar-by-dates.sh
, then you can use like this:
cd /path/to/files
~/bin/tar-by-dates.sh *
Upvotes: 2