I keep a text file of useful commands and info I collect as I'm learning linux. I had been opening it in vim whenever I came across something good to add, but that was getting tedious. So today I was using cat >>
to append things to it, figuring I'd go back later to organize it. (I'm working without a GUI so every little thing helps.) I'd either use cat or just do a command >> file.
I tried to view the file and accidentally did a cat > textfile
and blanked the file.
Looking at my .zsh_history as I write this, more precisely what I did was cat> textfile | tail
. Apparently I took out the space instead of one of the redirect "greater than" symbols.
What I've Tried
I realized right away what I had done and I didn't do anything more in that terminal; cat is still sitting there waiting with an empty line. Based on this question, I tried this answer in another tty terminal:
Suggested Answer:
sudo grep -i -a -B100 -A100 'string' /dev/sda1 > file.txt
My Attempt:
grep -F -a -B3000 -A3000 'string' /dev/sdb2 > /media/EXTERNAL_HDD_MOUNT/grepResults
I thought I'd leave it overnight and see what it came up with in the morning, but was surprised to see it finished at almost exactly an hour. The OS is installed on USB/C flash drive with the main partition 186 GB (not persistent live, just normal installed). It's running on a machine that currently doesn't have any internal hard drives. It's come up with 11 MB of something so I'll have to extract the readable strings and see what I get (another thing I have to learn how to do).
One of my questions is does it make any difference if and how I kill/stop/terminate the cat process? I know it doesn't work without a newline, but it does its thing right away regardless and the file already appears empty when I peek with other viewers.
Another question (or x y problem) is can I potentially recover it from /proc? That was another suggestion on that 2010 question that I thought looked promising. I found the PID and it has a directory in /proc but I don't know what I'm looking at when I go in there, and I don't know how to go about it.
Oh, one other thing I did was fc -p
. I was afraid any command I did could cause something to be written to the drive. ZSH history is one of the few things I'm aware of at this point, so I googled and attempted to stop it.
Any grep advice is also appreciated. I realized my "MyCommands" file is just full of special characters, so I tried to avoid those in selecting my unique string to search for, just to not be adding extra variables into the mix and complicate things more for myself. I save it as a .bat just because I like the syntax highlighting in that format.
strings -t d /dev/disk | grep
orstrings -w …
instead of using grep directly. If you are using special characters (non-ASCII UTF-8) try on a sample file first ifstrings
prints them at all. On SSD you also have to umount / disablefstrim
first, otherwise with TRIM it's just gone… – frostschutz Oct 28 '22 at 10:07