LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   exit cat command without saving (https://www.linuxquestions.org/questions/linux-newbie-8/exit-cat-command-without-saving-4175453249/)

sunil80 03-08-2013 06:24 AM

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.

towheedm 03-08-2013 06:36 AM

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
And no, I don't think you can do what you asked. Once you run:
Code:

cat > /etc/passwd
it will overwrite the /etc/passwd when you hit the enter key. The CTRL-C|D|Z keys simply sends the SIGINT, SIGHUP etc signals to the last executed foreground process.

ali.abry 03-08-2013 06:41 AM

before you press Ctrl+c your file content is cleared.
so exiting it in any kind of way will not return your file content.

Habitual 03-08-2013 06:51 AM

Quote:

Originally Posted by sunil80 (Post 4907351)
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.

In a new terminal, or new console, issue a
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.

sunil80 03-08-2013 06:52 AM

Quote:

Originally Posted by towheedm (Post 4907358)
Why would you want to do that to clear the /etc/passwd file?

I don't want to delete a user. I just say sometimes by mistake it had done. Is there any solution after hitting "cat > /etc/passwd"
command.

towheedm 03-08-2013 06:56 AM

Quote:

Originally Posted by Habitual (Post 4907364)
In a new terminal, or new console, issue a
Code:

sudo killall -9 cat
?

That will not work.
Code:

echo "This is a test" > test.txt
cat > test.txt

Open a new terminal window and:
Code:

cat test.txt
returns empty file.

sunil80 03-08-2013 06:57 AM

Quote:

Originally Posted by Habitual (Post 4907364)
In a new terminal, or new console, issue a
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.

I have tried this, but of no use sir.

fortran 03-08-2013 06:58 AM

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.

shivaa 03-08-2013 06:59 AM

Code:

~$ cat > /etc/passwd
It will simply override the /etc/passwd file, and no user will be able to login into the system.

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)

sunil80 03-08-2013 07:01 AM

Quote:

Originally Posted by ali.abry (Post 4907362)
before you press Ctrl+c your file content is cleared.
so exiting it in any kind of way will not return your file content.

But is there any solution for it.............

fortran 03-08-2013 07:04 AM

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
$ chmod 644 /etc/passwd
$ pwck -q


shivaa 03-08-2013 07:19 AM

Quote:

Originally Posted by sunil80 (Post 4907374)
But is there any solution for it.............

No real time solution. Maximun you can restore it from most recent backup, if you have.

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
root::0:0:Super-User:/root:/sbin/sh

Rest you will need to take backup of all users /home directory data and create them again.

towheedm 03-08-2013 07:23 AM

Quote:

Originally Posted by sunil80 (Post 4907374)
But is there any solution for it.............

Yes, there are three I can think of:
  • As pavi suggested
  • Restore it from your backup. You do backup, important data, right?
  • A bit of explanation first. When you issue the cat > file command, BASH loads the file in memory and then processes it as you have commanded it. In this case, overwrite it with the NUL char. When the changes are made, BASH tells the OS to write the file the disk. But those changes are not immediately committed to the physical disk. It goes to the writeback cache. Dirty pages in the cache are flushed to disk on the next writeback cycle. So between the time that you hit enter and the next writeback cycle, the data IS still actually on the disk. If you are extremely fast and can stop the cache commit to disk, you can prevent the contents of the file from being overwritten. This IS next to impossible of course.


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.

grail 03-08-2013 08:51 AM

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
And I read this aloud:

"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?

towheedm 03-08-2013 12:44 PM

It was asked first.

Quote:

Originally Posted by towheedm (Post 4907358)
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
And no, I don't think you can do what you asked. Once you run:
Code:

cat > /etc/passwd
it will overwrite the /etc/passwd when you hit the enter key. The CTRL-C|D|Z keys simply sends the SIGINT, SIGHUP etc signals to the last executed foreground process.



All times are GMT -5. The time now is 01:22 PM.