Linux remove file content

How to Empty or Delete a Large File Content in Linux?

Usually a continuously growing files, needs to be emptied from time to time to accept the latest data from the next operation. There are various mechanisms to empty the file. We will see them one by one below. The common approach is the overwrite the target file by using > sign which should come from a source which is empty.

/dev/null

This is a common method where we output empty result and then redirect that result to the target file.

# Original file size $ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt # Redirect the output from /dev/null $ cat /dev/null > ref_file.txt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:35 ref_file.txt

echo

The echo command with null out put can also make the target file empty.

$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $echo > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:36 ref_file.txt

Using >

Just putting only a > just before the file name will make it empty. We can also use just a colon(:) to get the same result.

$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $ > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:39 ref_file.txt

Using truncate

Using truncate command with the size option as 0 will make the target file empty.

$ truncate s -0 ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:41 ref_file.txt

Using touch

$ touch ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:45 ref_file.txt

Источник

5 Ways to Empty or Delete a Large File Content in Linux

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

Caution: Before we proceed to looking at the various ways, note that because in Linux everything is a file, you must always make sure that the file(s) you are emptying are not important user or system files. Clearing the content of a critical system or configuration file could lead to a fatal application/system error or failure.

Читайте также:  Unable to locate package linux headers next sunxi

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 way to empty or blank a file content using shell redirect null (non-existent object) to the file as below:

Empty Large File Using Null 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 it can be used as a no-op (no operation).

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

# : > access.log OR # true > access.log

Empty Large File Using Linux Commands

3. 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 process, or else as a suitable empty file for input streams. This is normally done by redirection mechanism.

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

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

Empty File Using cat Command

Next, we will use cp command to blank a file content as shown.

Empty File Content Using cp Command

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

Empty File Content Using dd Command

4. Empty File Using echo Command

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

# echo "" > access.log OR # echo > access.log

Empty File Using echo Command

Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.

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

To send a null output to the file, use the flag -n which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.

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 defined size.

You can employ it with the -s option that specifies the file size. To empty a file content, use a size of 0 (zero) as in the next command:

Читайте также:  Linux tar exit status

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 mechanism.

These are not probably the only 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.

Источник

How To Use “Truncate” Command In Linux?

Sometimes we need to remove the content of a file without deleting the file; for that Linux operating system offers a command called “truncate”. It is used to extend or reduce the file size. Truncating a file is much quicker and simpler without modifying the permissions and ownership of the file.

The truncated size depends on the original size of the file; the extra data will be lost if the file size is greater than the specified size.

Let’s start with different examples to see how we can truncate the file size.

Installing Coreutils Packages

The “truncate” command comes with most Linux distribution. It can also be installed, if not present, using the command given below:

Use the “grep” command to list the detail of packages:

How to Use the “truncate” Command?

The “>” shell redirection operator is the most popular and simplest way to truncate files.

The syntax for truncating files with redirection is:

The “:” colon denotes true and has no output and the redirection operator “>” redirect the output to a specific file.

The file I am truncating is “test.sh”:

Another way to truncate file is:

It is removing the content of “test.sh” file.

Clear the Content of File

Use the “-s” option to remove the content of the files. This is a preferable way to manually delete a file. The truncate command effectively eliminates all the contents of a file. It does not delete the file itelf, but leaves it as a zero-byte file on the disk.

Let’s use truncate to clear file.txt to 0 bytes:

The file permissions and ownership will be preserved if you use the truncate command.

Use the “ls -lh” command to confirm the size:

Truncating a File to a Specific Size

To confirm the file permission and size of the file, use:

Let’s truncate the file to 100 bytes size:

To truncate a file size to 300K:

Type below mentioned command to check the size:

Extending the File Size

You can increase the file size by using the “+” with “-s” option. The file is currently 300k in size, as shown in the image below:

Читайте также:  Hyperbola gnu linux libre bsd

I’d like to increase the size of the file from 300k to 600k bytes:

The file size has been extended from 300k to 600k. Check the size:

Reducing the File Size

Let’s assume you have a 600k file and want to reduce its size to 270k, use “-s” option and “” with the size figured:

The current size of the file is 330k.

Getting Help

To get a help message, use:

Checking Version

To check the version of the truncate command, use:

Conclusion:

Truncate is a very useful command for removing the content of a file while not deleting the file. You can also change the size of the file to the size you want it to be. We have learned how to truncate the content of a file, as well as how to shrink or extend the files in this article.

About the author

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related
articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.

Источник

Linux command to delete file content

I want to delete content of the file main_copy.json . I have a confusion if I used below command, will it remove files in all these directories?

 [user@yyy ~]$ > /AAA/BBB/CCC/DDD/main_copy.json 

1 Answer 1

The ‘>’ character is the shell (often Bash) redirection character. You are at a command prompt of the shell, probably Bash but you don’t specify. The command as you have written basically says «redirect to the file /AAA/BBB/CCC/DDD/main_copy.json». The net result is to truncate the file to zero length, effectively deleting its contents.

Since there are no spaces in the argument to ‘>’, bash treats it as a single argument, thus there is no possibility that your command will delete contents of any files in any of the directories in your path. In any case, the ‘>’ redirect operator does not work with multiple arguments. So if you were to say:

[user@yyy ~]$ > /AAA /BBB/CCC/DDD/main_copy.json 

The net effect would issue an error because you can’t redirect to a directory, only to a file. If you were to say:

[user@yyy ~]$ > /AAA/myfile.txt BBB/CCC/DDD/main_copy.json 

the shell would truncate the file myfile.txt to zero length, (or create a zero-length file if it did not exist) and then treat the second argument as an executable command, which of course would fail with something like «Permission Denied» because it’s not an executable.

Hope that helps. Bash (and other shells) is a complicated beast and takes years to really learn well.

Источник

Оцените статью
Adblock
detector