6 ms·
i've filled up an zfs array to the point where i could not delete files. the trick is to truncate a large enough files, or enough small files, to zero. not su
by werid 3y ago
i've filled up an zfs array to the point where i could not delete files.
the trick is to truncate a large enough files, or enough small files, to zero.
not sure if this is a universal shell trick, but worked on those i tried: "> filename"
- pdimitar 3y agoFor reasons I am completely unwilling to research, just doing `> filename` has not worked for me in a while. Since then I memorized this: `cat /dev/null >! filename`, and it has worked on systems with zsh and bash.
- alias_neo 3y ago"truncate -s0 filename" I believe "> filename" only works correctly if you're root (at least in my experience, if I remember correctly). EDIT: To remove <> from filename placeholder which might be confusing, and to put commands in quotes.
- pdimitar 3y agoOh yes, that one also worked everywhere I tried, thanks for reminding me.
- alias_neo 3y agoPleasure. It saved me just yesterday when I needed to truncate hundreds of gigabytes of Docker logs on a system that had been having some issues for a while but I didn't want to recreate containers. "truncate -s 0 /var/lib/docker/containers/**/*-json.log" Will truncate all of the json logs for all of the containers on the host to 0 bytes. Of course the system should have had logging configured better (rotation, limits, remote log) in the first place, but it isn't my system. EDIT: Missing double-star.*
- matja 3y agoSimple to verify with strace -f bash -c "> file": openat(AT_FDCWD, "file", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3 man 2 openat: O_TRUNC If the file already exists and is a regular file and the access mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0. ...
- pdimitar 3y agoSure, but I just get an interactive prompt when I type `> file` and I honestly don't care to troubleshoot. ¯\_(ツ)_/¯
- adrianmonk 3y agoThat seems to be zsh-specific syntax that is like ">" except that overrides a CLOBBER setting[1]. However, it won't work in bash. It will create file named "!" with the same contents as "filename". It is equivalent to "cat /dev/null filename > !". (Bash lets you put the redirection almost anywhere, including between one argument and another.) --- [1] See https://zsh.sourceforge.io/Doc/Release/Redirection.html https://zsh.sourceforge.io/Doc/Release/Redirection.html
- pdimitar 3y agoYikes, then I have remembered wrong about bash, thank you. In that case I'll just always use `truncate -s0` then. Safest option to remember without having to carry around context about which shell is running the script, it seems.
- ralferoo 3y agoIt'd be better to do ": >filename" : is a shell built-in for most shells that does nothing.
- markandrewj 3y agoDepending on the environment you can also use the truncate command. This will work if the file is open as well. https://man7.org/linux/man-pages/man1/truncate.1.html https://man7.org/linux/man-pages/man1/truncate.1.html