exit cat command without saving
hello everyone,
In past days, sometimes by mistake i run the command, "cat > /etc/passwd" and it get clear when i press CTRL+Z, CTRL+C or CTRL+D. Can I exit this command without changing my file. |
Why would you want to do that to clear the /etc/passwd file?
If you want to remove user accounts use the userdel to deluser commands. This ensures the entries are also removed from the /etc/shadow file and other stuff related to the deleted user are processed as defined in your /etc/login.def file. IF you simply want to view the passwd file, simply cat it: Code:
cat /etc/passwd Code:
cat > /etc/passwd |
before you press Ctrl+c your file content is cleared.
so exiting it in any kind of way will not return your file content. |
Quote:
Code:
sudo killall -9 cat It certainly shouldn't make anything 'worse'. I'd also check for a /etc/passwd~ file before doing anything else. |
Quote:
command. |
Quote:
Code:
echo "This is a test" > test.txt Code:
cat test.txt |
Quote:
|
If your file is opened in text editor(gedit) and you have run
cat > filename it asks in editor "The file /path/of/the/file has changed on disk Do you want to reload the file" Reload or cancel This is your last chance to save your contents. Click on cancel & save it in another file because once you close gedit, contents are disappeared. |
Code:
~$ cat > /etc/passwd It's a critical file, and don't do any experiments with it. And even better take backup of passwd file before any modification, so in case of need, it can be restored. Code:
~$ cp -p /etc/passwd /etc/passwd.$(date +%Y%m%d) |
Quote:
|
Yes don't do this with passwd
but unfortunately if you have done this with your /etc/passwd. There is a backup file of /etc/passwd in same directory with name passwd-. To recover this. Code:
$ cp /etc/passwd- /etc/passwd |
Quote:
As a workaround, create some manual entries in it at least for root, so root can login into the system (leave 2nd field empty) Code:
~$ vi /etc/passwd |
Quote:
With that said, pavi's suggestion is about the most practical if you don't do backup's. Of course, you should certainly not be messing around with /etc/passwd if you make these simple mistakes. |
I would have thought the first question to ask would be why would you issue that command in the first place?
My first ever linux / unix course instructor had the following to say: If you need to make a change as root (which would be the case here), after typing the command, sit on your hands and re-read the command out loud. This may seem silly, but I have to say, at any time I issue a command for the first few times that I have not used before, I still follow this method. If looking at the example provided and I have typed: Code:
# cat > /etc/passwd "Cat redirection to the file /etc/passwd" After thinking for around a second you should realise that this will immediately blow away all contents of the named file. So ask yourself before pressing Enter ... is this what I want to do? |
It was asked first.
Quote:
|
All times are GMT -5. The time now is 01:22 PM. |