trash-cli icon indicating copy to clipboard operation
trash-cli copied to clipboard

Please consider trash-undo

Open stephenjjohnson opened this issue 8 years ago • 1 comments
trafficstars

I guess this would require that the trash-put command cache the list of files removed at its last invocation (or a diff of trash-list before and after), but it would be great to be able to undo (i.e. trash-restore) everything I just trashed without thinking when I don't know the name of the file I need to get back.

stephenjjohnson avatar Oct 13 '17 17:10 stephenjjohnson

For people in the future, here's a one liner that does the equivalent procedure using only coreutils:

echo '' | trash-restore 2>/dev/null | sed '$d' | sort -k2,3 -k1,1n | awk 'END {print $1}' | trash-restore >/dev/null 2>&1

trash-restore's ordering is consistent, but not chronological.

  1. We generate the numbered list by feeding an empty option to trash-restore
  2. Remove the extra line at the end
  3. To find the most recent element, we first sort by date (2), then time (3), then the index (1) in ascending order
  4. Take the last one
  5. We then feed that into trash-restore and get a restore.

The only edge case is if trash-restore is empty, but in that case, we end up feeding an empty line into trash-restore again, so nothing happens.

Also, you can drop in an fzf or dmenu in the middle there and make it an interactive restoration:

echo '' | trash-restore 2>/dev/null \
	| sed '$d' \
	| sort -k2,3 -k1,1n \
	| dmenu \
	| awk 'END {print $1}' \
	| trash-restore >/dev/null 2>&1

norcalli avatar Apr 02 '19 22:04 norcalli