Bash Clear Out File Before Running Again

Occasionally, while dealing with files in Linux terminal, you may want to clear the content of a file without necessarily opening information technology using any Linux control line editors. How can this be achieved? In this article, we will go through several dissimilar ways of emptying file content with the help of some useful commands.

Caution: Earlier we go along to looking at the diverse ways, annotation that because in Linux everything is a file, yous must always brand sure that the file(s) you are emptying are not important user or organisation files. Clearing the content of a disquisitional system or configuration file could atomic number 82 to a fatal awarding/system error or failure.

With that said, below are means of clearing file content from the command line.

Important: For the purpose of this article, we've used file access.log in the following examples.

1. Empty File Content by Redirecting to Null

A easiest fashion to empty or blank a file content using trounce redirect null (not-real object) to the file as beneath:

# > access.log        
Empty Large File Using Null Redirect in Linux
Empty Big File Using Aught Redirect in Linux

2. Empty File Using 'true' Command Redirection

Here we will use a symbol : is a shell built-in command that is essence equivalent to the true command and information technology can be used as a no-op (no operation).

Another method is to redirect the output of : or true congenital-in command to the file like so:

# : > admission.log OR  # true > access.log        
Empty Large File Using Linux Commands
Empty Large File Using Linux Commands

iii. Empty File Using cat/cp/dd utilities with /dev/null

In Linux, the null device is basically utilized for discarding of unwanted output streams of a procedure, or else every bit a suitable empty file for input streams. This is usually washed past redirection machinery.

And the /dev/nada device file is therefore a special file that writes-off (removes) whatsoever input sent to it or its output is same as that of an empty file.

Additionally, you lot tin empty contents of a file by redirecting output of /dev/null to it (file) as input using cat control:

# true cat /dev/zippo > access.log        
Empty File Using cat Command
Empty File Using cat Command

Adjacent, we volition use cp command to bare a file content as shown.

# cp /dev/nix access.log        
Empty File Content Using cp Command
Empty File Content Using cp Control

In the following command, if means the input file and of refers to the output file.

# dd if=/dev/zilch of=access.log        
Empty File Content Using dd Command
Empty File Content Using dd Control

4. Empty File Using echo Command

Here, you can use an echo command with an empty string and redirect information technology to the file as follows:

# echo "" > access.log OR # repeat > access.log        
Empty File Using echo Command
Empty File Using echo Command

Annotation: You lot should keep in listen that an empty string is not the same as nil. A string is already an object much as it may be empty while null merely means not-beingness of an object.

For this reason, when you redirect the out of the echo control above into the file, and view the file contents using the cat control, is prints an empty line (empty string).

To ship a null output to the file, use the flag -northward which tells repeat to not output the trailing newline that leads to the empty line produced in the previous control.

# echo -n "" > admission.log        
Empty File Using Null Redirect
Empty File Using Null Redirect

5. Empty File Using truncate Command

The truncate command helps to shrink or extend the size of a file to a divers size.

You can employ it with the -s choice that specifies the file size. To empty a file content, use a size of 0 (zero) every bit in the adjacent control:

# truncate -south 0 access.log        
Truncate File Content in Linux
Truncate File Content in Linux

That's it for now, in this article we have covered multiple methods of clearing or emptying file content using simple command line utilities and shell redirection machinery.

These are non probably the but available practical ways of doing this, so you can also tell us about any other methods not mentioned in this guide via the feedback section below.

If Y'all Capeesh What Nosotros Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and nigh trusted community site for whatsoever kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or scan the thousands of published articles bachelor FREELY to all.

If yous similar what y'all are reading, please consider ownership the states a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never catastrophe support.

yungstoud1951.blogspot.com

Source: https://www.tecmint.com/empty-delete-file-content-linux/

Belum ada Komentar untuk "Bash Clear Out File Before Running Again"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel