Reputation: 765
I have a folder containing 2Gb of images, with sub-folders several levels deep.
I'd like to archive only N
files of each (sub) folder in a tar file. I tried to use find
then tail
then tar
but couldn't manage to get it to work. Here is what I tried (assuming N = 10
):
find . | tail -n 10 | tar -czvf backup.tar.gz
… which outputs this error:
Cannot stat: File name too long
What's wrong here? thinking of it - even if it works I think it will tar only the first 10 files of all folders, not the first 10 files of each folder.
How can I get the first N
files of each folder?
Upvotes: 4
Views: 797
Reputation: 2384
A proposal with some quirks: order is only determined by the order out of find
, so "first" isn't well-defined.
find . -type f | awk -v N=10 -F / 'match($0, /.*\//, m) && a[m[0]]++ < N' | xargs -r -d '\n' tar -rvf /tmp/backup.tar gzip /tmp/backup.tar
Comments:
find . -type f
to ensure that files have a leading directory-name prefix, so the next step can workawk
command tracks such leading directory names, and emits full path names until N (10, here) files with the same leading directory have been emittedxargs
to invoke tar
- we're gathering regular file names, and they need to be arguments to that archiving commandxargs
may invoke tar
more than once, so we'll append (-r option) to a plain archive, then compress it after it's all writtenAlso, you may not want to write a backup file into the current directory, since you're scanning that - that's why this suggestion writes into /tmp.
Upvotes: 1