How to Use Linux Commands Effectively to Handle Files

I’m Damn sure, everyone might have googled many times to get an answer for the below questions.

  • How to empty a file in Linux?
  • How do I delete “N” lines from a file in Linux?
  • How to remove a line based on a matching string from a file in Linux?
  • How to remove blank lines from a file in Linux

Are you thinking, these all are very difficult? I would say yes for newbies but no for experienced guys since it is used sed command, and most of the Linux guys was already familiar about that.

Whoever are not familiar with sed command, I would advise you to spend valuable time on that which will be very helpful in future.These all are falls under file manipulation tools such as sed, grep, etc,.

We are going to add at least two examples on each section to demonstrate this.

1) How to Empty a File in Linux

Everyone knows about rm command and its purpose but in this article we have covered possible methods that will empty your file rather than deleting it. This will help you when you want to empty a file instead of creating new one.

We assume that we had a filename called “2g.txt” in the present working directory. Hence, we are going to use the filename in each example.

1a) How to Use Shell Redirection to Empty a File in Linux

Use shell redirection to empty a file.

# > 2g.txt
or
# :> 2g.txt

1b) How to Use the echo Command to Empty a File in Linux

You can easily empty a file by writing null values.

# echo -n > 2g.txt
  • n: Do not output the trailing newline.

1c) How to Use the truncate command to Empty a File in Linux

The truncate command is used to shrink or extend the size of a file to the specified size. To empty a file, use the s option followed by “zero” in the truncate command.

# truncate -s 0 2g.txt
or
# truncate 2g.txt --size 0

1d) How to Use the /dev/null Device to Empty a File in Linux

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams.

This is usually done by redirection. The “/dev/null” device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.

Using the cat command

# cat /dev/null > 2g.txt

Using the cp command

# cp /dev/null 2g.txt
cp: overwrite `2g.txt'? y

Using the dd command

# dd if=/dev/null of=2g.txt
0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000219195 s, 0.0 kB/s
  • dd : It’s a command
  • if : It’s an input file
  • of : It’s an output file
  • /dev/null : It’s a special 0 byte file

1e) How to Use vim Editor to Empty a File in Linux

Vim editor is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as “vi” in most of UNIX systems.

# ex -sc ':%d|x' 2g.txt
or
# ex -sc ':1,$d|x' 2g.txt
  • ex : Enter into Ex mode
  • s : Silent; do not display prompts
  • c : Run the given ex command upon startup
  • : : Invoke an ex command
  • % : Choose all the lines
  • d : Delete selected lines
  • x : save and close
  • 2g.txt : input filename

2) How Do I Remove the Number of “N” Lines From a File in Linux?

Alternatively, you can delete the number of “N” lines from a file using the wim editor.

For demonstration purposes, we include the numbers 1 to 10 in the file 2g.txt.

# more 2g.txt

1
2
3
4
5
6
7
8
9
10

If you need to remove the first 4 lines (1 to 4) from a file, how do you do that? The format should be as follows.

# ex -sc '1d4|x' 2g.txt
  • ex : Enter into Ex mode
  • s : Silent; do not display prompts
  • c : Run the given ex command upon startup
  • 1 : Move to first line
  • 4 : Select 4 lines
  • d : Delete selected lines
  • x : save and close
  • 2g.txt : input filename

The output is follow.

# more 2g.txt

5
6
7
8
9
10

If you want to remove 6 lines in the center starting from 4, the format should look like the one below.

# ex -sc '4d6|x' 2g.txt

The output is as follow. The above command completely removed 6 lines starting from 4th line to 9th line.

# more 2g.txt

1
2
3
10

3) How to Remove a Line Based on a Matching String From a File in Linux?

sed command is provides an effective and a versatile way of deleting one or more lines from a designated file. The same has been achieved through vi as well.

To demonstrate this example, we have added some names in the “2g.txt” file.

# more 2g.txt

Fedora
Debian
Ubuntu
Windows
Linux
Windows
RHEL
CentOS
Windows
openSUSE

The above file contains three matching strings called Windows, which we are going to remove in the example below.

Using the sed command.

# sed -i '/Windows/d' 2g.txt
  • sed : is a command
  • i : edit files in place
  • Windows : Matching string
  • d : Delete the matching string
  • 2g.txt : input filename

Using vi editor.

# ex +g/Windows/d -cwq 2g.txt

The output is follow.

# more 2g.txt
Fedora
Debian
Ubuntu
Linux
RHEL
CentOS
openSUSE

4) How to Remove Empty/Blank Lines From a File in Linux

You can remove blank lines from a file using the sed command.

To prove this example we have manually added some blank lines to the output above.

# more 2g.txt
Fedora
Debian

Ubuntu


Linux
RHEL
CentOS

openSUSE

As you can see the above output file contains empty lines, hence we are going to use sed command to remove them.

# sed -i '/^$/d' 2g.txt
  • sed : is a command
  • i : edit files in place
  • ^$ : is a regular expression to match blank lines
  • d : Delete the matching string
  • 2g.txt : input filename

See the output now.

# more 2g.txt
Fedora
Debian
Ubuntu
Linux
RHEL
CentOS
openSUSE

References

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *