LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-08-2013, 06:24 AM   #1
sunil80
Member
 
Registered: Jan 2013
Posts: 44

Rep: Reputation: Disabled
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.
 
Old 03-08-2013, 06:36 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
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.

Last edited by towheedm; 03-08-2013 at 06:39 AM.
 
Old 03-08-2013, 06:41 AM   #3
ali.abry
Member
 
Registered: Feb 2013
Posts: 74

Rep: Reputation: Disabled
before you press Ctrl+c your file content is cleared.
so exiting it in any kind of way will not return your file content.
 
1 members found this post helpful.
Old 03-08-2013, 06:51 AM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,249
Blog Entries: 11

Rep: Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289Reputation: 2289
Quote:
Originally Posted by sunil80 View Post
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.

Last edited by Habitual; 03-08-2013 at 06:55 AM.
 
Old 03-08-2013, 06:52 AM   #5
sunil80
Member
 
Registered: Jan 2013
Posts: 44

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by towheedm View Post
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.
 
Old 03-08-2013, 06:56 AM   #6
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by Habitual View Post
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.
 
Old 03-08-2013, 06:57 AM   #7
sunil80
Member
 
Registered: Jan 2013
Posts: 44

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
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.
 
Old 03-08-2013, 06:58 AM   #8
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
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.
 
Old 03-08-2013, 06:59 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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)
 
1 members found this post helpful.
Old 03-08-2013, 07:01 AM   #10
sunil80
Member
 
Registered: Jan 2013
Posts: 44

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by ali.abry View Post
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.............
 
Old 03-08-2013, 07:04 AM   #11
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
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
 
4 members found this post helpful.
Old 03-08-2013, 07:19 AM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by sunil80 View Post
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.
 
Old 03-08-2013, 07:23 AM   #13
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
Quote:
Originally Posted by sunil80 View Post
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.
 
1 members found this post helpful.
Old 03-08-2013, 08:51 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,493

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
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?
 
1 members found this post helpful.
Old 03-08-2013, 12:44 PM   #15
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
It was asked first.

Quote:
Originally Posted by towheedm View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE4: saving programs, positions and window settings on exit snoopy1 Linux - Software 2 05-28-2009 12:37 PM
How to exit and save (or just to exit) inside a "make config" running command? scrat75 Linux - Kernel 6 09-05-2008 04:02 PM
Saving open webpages on exit Maverick1182 Linux - Software 3 10-08-2006 02:29 PM
Kde 3 not saving configuration on exit Raduprv Linux - Software 2 12-06-2003 10:52 PM
exit without saving KaktusKnight Linux - Newbie 1 02-10-2003 02:21 AM


All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration