[Usage of the truncate command in Linux]

truncate related commands

Usage: truncate option... file...

Shrink or expand the file to the specified size .

 

Creates the specified file if it does not exist.

 

If the specified file exceeds the specified size, the excess data will be lost.

If the specified file is smaller than the specified size, it will be padded with 0.

 

Parameters that are required for long options are also required for short options.

  -c, --no-create do not create file

  -o, --io-blocks treat SIZE as IO blocks instead of bytes

  -r, --reference=file use the size of this file

  -s, --size=size use this size

      --help display this help message and exit

      --version display version information and exit

 

SIZE can be an optional integer followed by one of the following units:

KB 1000, K 1024, MB 1000*1000, M 1024*1024, and G, T, P, E, Z, Y.

 

 

The specified size can also be decorated with the following prefixes:

"+" increases, "-" decreases, "<" at most, ">" at least,

"/" is less than or equal to the minimum multiple of the specified number of the original size number, "%" is greater than or equal to the maximum multiple of the specified number of the original size number.

Translator's Note: When the input value is m and the reference value is n,

The mathematical formula for the "/" operation is m / n * n;

The mathematical formula for the "%" operation is ( m + n - 1 ) / n * n

 

 

Note that -r and -s are mutually exclusive options.

 

Summary of Linux file emptying methods

1. Use the redirect method

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# > test.txt 

[root@centos7 ~]# du -h test.txt 

0 test.txt

 

2. Use the true command to redirect the empty file

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# true > test.txt 

[root@centos7 ~]# du -h test.txt 

0 test.txt

 

3. Use the cat/cp/dd command and the /dev/null device to clear the file

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# cat /dev/null > test.txt 

[root@centos7 ~]# du -h test.txt 

 test.txt

###################################################

[root@centos7 ~]# echo "Hello World" > test.txt 

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# cp /dev/null test.txt 

cp: Overwrite "test.txt"? y

[root@centos7 ~]# du -h test.txt 

 test.txt

##################################################

[root@centos7 ~]# echo "Hello World" > test.txt 

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# dd if=/dev/null of=test.txt 

0+0 reads are recorded

Recorded the write out of 0+0

0 bytes (0 B) copied, 0.000266781 sec, 0.0 kB/sec

[root@centos7 ~]# du -h test.txt 

 test.txt

 

4. Use the echo command to empty the file

[root@centos7 ~]# echo "Hello World" > test.txt 

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# echo -n "" > test.txt ==> To add the "-n" parameter, by default it will be "\n", which is the carriage return

[root@centos7 ~]# du -h test.txt 

0 test.txt

 

5. Use the truncate command to empty the file

[root@centos7 ~]# du -h test.txt 

4.0K test.txt

[root@centos7 ~]# truncate -s 0 test.txt The -s parameter is used to set the size of the file, if the file is cleared, it is set to 0;

[root@centos7 ~]# du -h test.txt 

0 test.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326036540&siteId=291194637