Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-17-2015, 01:14 PM   #1
LQ Newbie
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 24

Rep: Reputation: Disabled
How to remove a line?

when i insert a line using command echo"" how can i remove that line? thanks advance.

[root@jakir ~]# echo "first inserting line" >file3
[root@jakir ~]# echo "first inserting line" >file3
[root@jakir ~]# cat file3
first inserting line
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# echo "first inserting line" >>file3
[root@jakir ~]# cat file3
first inserting line
first inserting line
first inserting line
first inserting line
[root@jakir ~]# rm -r "first inserting line"
rm: cannot remove `first inserting line': No such file or directory
[root@jakir ~]# ^C
[root@jakir ~]# vi file3
[root@jakir ~]# cat file3

first inserting line
first inserting line
Old 03-17-2015, 01:56 PM   #2
Registered: Sep 2012
Distribution: Slackware-14
Posts: 118

Rep: Reputation: Disabled
First thing first, if you really want to mess around please do it using a normal user not as root.
rm -r "first inserting line"
'rm -r' will delete directories recursively and that's very dangerous. rm takes filename or directory name as it's argument not contents of a text file. see 'man rm'

Now you need to know about redirection. Check out bash and sed, more specifically 'sed' for your line deletion task but it's really recommended that you get your hands dirty learning bash first.

Last edited by GNU/Linux; 03-17-2015 at 01:57 PM.
Old 03-17-2015, 04:00 PM   #3
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,822
Blog Entries: 27

Rep: Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122Reputation: 2122
Member response


I too agree that you should use 'root' carefully. A newbie can do major damage with just one slip. Especially when you are using 'rm -r' on something. 'man rm';

rm - remove files or directories SYNOPSIS


This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If a file is unwritable, the standard input is a tty, and the -f or --force option is not given, rm prompts the user for whether to remove the file. If the response does not begin with `y' or `Y', the file is skipped.


Remove (unlink) the FILE(s).
-d, --directory unlink FILE, even if it is a non-empty directory (super-user only)
-f, --force ignore nonexistent files, never prompt
-i, --interactive prompt before any removal
-r, -R, --recursive remove the contents of directories recursively
-v, --verbose explain what is being done
--help display this help and exit
--version output version information and exit
To remove a file whose name starts with a `-', for example `-foo', use one of these commands:
rm -- -foo
rm ./-foo Note that if you use rm to remove a file, it is usually possible to
recover the contents of that file. If you want more assurance that the
contents are truly unrecoverable, consider using shred.
You should get in the habit of using the 'man command'.

Just a few links to aid you to gaining some understanding;

Hope this helps.
Have fun & enjoy!

Last edited by onebuck; 03-17-2015 at 04:31 PM. Reason: reformat quote
1 members found this post helpful.
Old 03-17-2015, 04:36 PM   #4
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 824

Rep: Reputation: 237Reputation: 237Reputation: 237
Something to play with and analyse in combination with man pages:

user@localhost:~/tmp$ echo line 1 >> testfile
user@localhost:~/tmp$ echo line 2 >> testfile
user@localhost:~/tmp$ echo line 3 >> testfile
user@localhost:~/tmp$ echo line 4 >> testfile
user@localhost:~/tmp$ echo line 5 >> testfile
user@localhost:~/tmp$ head -n -1 testfile 
line 1
line 2
line 3
line 4
user@localhost:~/tmp$ head -n -1 testfile > testfile_without_last_line
user@localhost:~/tmp$ cat testfile_without_last_line 
line 1
line 2
line 3
line 4
Old 03-18-2015, 05:27 PM   #5
LQ Newbie
Registered: Dec 2014
Location: U.S.A ,NEWYORK
Posts: 24

Original Poster
Rep: Reputation: Disabled
ok thats cool idea,next time i will be work as regular user.
Old 03-19-2015, 07:02 AM   #6
Keith Hedger
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,414

Rep: Reputation: 610Reputation: 610Reputation: 610Reputation: 610Reputation: 610Reputation: 610
Use sed like so:
sed '2d' /path/to/file
Removes line 2
Or to remove a range of lines
sed '2,4d' /path/to/file
Removes lines 2-4 inclusive.


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
remove the line ust Linux - General 2 11-08-2013 09:21 PM
[SOLVED] Remove line break if next line does not start with "PX" joje47 Programming 8 05-25-2011 08:50 AM
[SOLVED] remove specified line from text ted_chou12 Linux - Newbie 7 02-12-2011 03:34 AM
script to remove line ust Linux - Server 2 07-02-2008 05:46 AM
remove the line munna_dude Programming 13 10-04-2007 03:23 AM > Forums > Linux Forums > Linux - Newbie

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

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