cat / dev / null到多个文件以清除日志等现有文件

时间:2022-05-01 12:00:30

A good way to clear logs(syslog has a handle on file) which have frozen my linux server(out of space) I tried cat /dev/null > fileABC; cat /dev/null/ > fileXYZ

清除日志的好方法(syslog有一个文件句柄)已冻结我的linux服务器(空间不足)我试过cat / dev / null> fileABC; cat / dev / null /> fileXYZ

How can I clear multiple files by cat /dev/null to multiple files in an efficient or single command.

如何通过cat / dev / null将多个文件清除为高效或单个命令中的多个文件。

3 个解决方案

#1


13  

Hard coded solutions

硬编码解决方案

tee

Echo nothing and simply send it to multiple files using the tee command.

没有回声,只需使用tee命令将其发送到多个文件。

Like this:

$ echo -n | tee file1 file2 file3 file4 file5

All files in that list will be empty and created if they don't exist.

该列表中的所有文件都将为空,如果它们不存在则会创建。

Applied to your answer this would be:

应用于您的答案,这将是:

$ cat /dev/null | tee fileABC fileXYZ

While echo -n is considered better practice than cat /dev/null, an even better solution would be to use printf '', as noted by Charles Duffy. Resulting in following command:

虽然echo -n被认为比cat / dev / null更好,但更好的解决方案是使用printf'',正如Charles Duffy所指出的那样。导致以下命令:

$ printf '' | tee file1 file2 file3 

truncate

As answered by skrilled, truncate is probably the solution you were originally looking for. The command allows arbitrarily many file name arguments to be supplied. You can easily use it as follows:

正如skrilled所说,截断可能是您最初寻找的解决方案。该命令允许提供任意多个文件名参数。您可以按如下方式轻松使用它:

$ truncate --size 0 file1 file2 file3 file4 file5

Which allows you to achieve your goal without using any pipe and in a single command, pretty nifty answer supplied here by skrilled.

这允许您在不使用任何管道的情况下实现您的目标,并且在单个命令中,skrilled提供了非常好的答案。

Structural file names solution

结构文件名解决方案

If all files have a structure on their names (for instance files) and location you could use the find command. In the following example I will apply the erasure to all .java and .c source files in the current directory and in all directories inside the current directory.

如果所有文件的名称(例如java文件)和位置都有结构,则可以使用find命令。在下面的示例中,我将擦除应用于当前目录和当前目录内所有目录中的所有.java和.c源文件。

$ find . -maxdepth 2 -type f -name '*.java' -exec truncate --size 0 "{}" \; 

Explained:

  • find . execute find in current directory .
  • 找 。在当前目录中执行find。

  • -maxdepth 2 recursion level, descend to directories in directory but no further (level 2). Set this to 1 to not descend or n to descend n times.
  • -maxdepth 2递归级别,下降到目录中的目录但没有进一步(级别2)。将此值设置为1表示不下降或n表示下降n次。

  • -type f only apply to files, not directories
  • -type f仅适用于文件,而不适用于目录

  • -name '*.java' only apply to files ending in .java
  • -name'* .java'仅适用于以.java结尾的文件

  • -exec truncate --size 0 "{}" \; truncate each file found (file name is stored in {})
  • -exec truncate --size 0“{}”\;截断找到的每个文件(文件名存储在{}中)

See man find for more options and a more detailed explanation. Be sure to check it out because find is one of the most powerful tools for automation of file editing.

请参阅man find以获取更多选项和更详细的说明。请务必查看它,因为find是文件编辑自动化最强大的工具之一。

List of files in separate file solution

单独文件解决方案中的文件列表

The easiest thing to do might be to store the files to erase in a file, line by line. If there is no obvious structure with respect to their location and name, that is.

最简单的方法可能是将文件存储在文件中,逐行删除。如果没有关于它们的位置和名称的明显结构,那就是。

Say the files are stored in a file called erasure.

假设文件存储在名为erasure的文件中。

$ cat erasure
fileABC
fileXYZ
dir/anotherFile

In this example we will erase three files, which are listed above.

在这个例子中,我们将删除上面列出的三个文件。

$ while read file; do > "$file"; done < erasure

Explanation:

  • while read file for each line in the given file, store the line in variable file
  • 在读取给定文件中每行的文件时,将该行存储在变量文件中

  • do > "$file" empty the file and output nothing in it (i.e. erase it)
  • do>“$ file”清空文件并输出任何内容(即删除它)

  • done < erasure specify the input file using < (redirection)
  • done <擦除使用<(重定向)指定输入文件< p>

Note: while this method preserves spaces in the path, it fails to handle backslashes and trailing white space, as noted by Charles Duffy. One way to fix both issues is to modify the loop as follows:

注意:虽然这个方法保留了路径中的空格,但它无法处理反斜杠和尾随空格,正如Charles Duffy所指出的那样。解决这两个问题的一种方法是修改循环,如下所示:

while IFS= read -r file; do > "$file"; done < erasure

Yet newlines in file names will still be a problem. The only way around this issue is to separate the file names using null termination (\0). The correct loop now becomes:

然而,文件名中的换行仍然是个问题。解决此问题的唯一方法是使用空终止(\ 0)分隔文件名。现在正确的循环变为:

while IFS= read -r -d '' file; do > "$file"; done < erasure

#2


4  

Truncate can be used to empty a file as well.

截断也可用于清空文件。

truncate --size 0 /path/to/file/here

#3


0  

You can do it quite nicely with GNU Parallel like this:

你可以用这样的GNU Parallel很好地完成它:

parallel '>' ::: TooLong.log BigBoy.dat NonExistant.file

#1


13  

Hard coded solutions

硬编码解决方案

tee

Echo nothing and simply send it to multiple files using the tee command.

没有回声,只需使用tee命令将其发送到多个文件。

Like this:

$ echo -n | tee file1 file2 file3 file4 file5

All files in that list will be empty and created if they don't exist.

该列表中的所有文件都将为空,如果它们不存在则会创建。

Applied to your answer this would be:

应用于您的答案,这将是:

$ cat /dev/null | tee fileABC fileXYZ

While echo -n is considered better practice than cat /dev/null, an even better solution would be to use printf '', as noted by Charles Duffy. Resulting in following command:

虽然echo -n被认为比cat / dev / null更好,但更好的解决方案是使用printf'',正如Charles Duffy所指出的那样。导致以下命令:

$ printf '' | tee file1 file2 file3 

truncate

As answered by skrilled, truncate is probably the solution you were originally looking for. The command allows arbitrarily many file name arguments to be supplied. You can easily use it as follows:

正如skrilled所说,截断可能是您最初寻找的解决方案。该命令允许提供任意多个文件名参数。您可以按如下方式轻松使用它:

$ truncate --size 0 file1 file2 file3 file4 file5

Which allows you to achieve your goal without using any pipe and in a single command, pretty nifty answer supplied here by skrilled.

这允许您在不使用任何管道的情况下实现您的目标,并且在单个命令中,skrilled提供了非常好的答案。

Structural file names solution

结构文件名解决方案

If all files have a structure on their names (for instance files) and location you could use the find command. In the following example I will apply the erasure to all .java and .c source files in the current directory and in all directories inside the current directory.

如果所有文件的名称(例如java文件)和位置都有结构,则可以使用find命令。在下面的示例中,我将擦除应用于当前目录和当前目录内所有目录中的所有.java和.c源文件。

$ find . -maxdepth 2 -type f -name '*.java' -exec truncate --size 0 "{}" \; 

Explained:

  • find . execute find in current directory .
  • 找 。在当前目录中执行find。

  • -maxdepth 2 recursion level, descend to directories in directory but no further (level 2). Set this to 1 to not descend or n to descend n times.
  • -maxdepth 2递归级别,下降到目录中的目录但没有进一步(级别2)。将此值设置为1表示不下降或n表示下降n次。

  • -type f only apply to files, not directories
  • -type f仅适用于文件,而不适用于目录

  • -name '*.java' only apply to files ending in .java
  • -name'* .java'仅适用于以.java结尾的文件

  • -exec truncate --size 0 "{}" \; truncate each file found (file name is stored in {})
  • -exec truncate --size 0“{}”\;截断找到的每个文件(文件名存储在{}中)

See man find for more options and a more detailed explanation. Be sure to check it out because find is one of the most powerful tools for automation of file editing.

请参阅man find以获取更多选项和更详细的说明。请务必查看它,因为find是文件编辑自动化最强大的工具之一。

List of files in separate file solution

单独文件解决方案中的文件列表

The easiest thing to do might be to store the files to erase in a file, line by line. If there is no obvious structure with respect to their location and name, that is.

最简单的方法可能是将文件存储在文件中,逐行删除。如果没有关于它们的位置和名称的明显结构,那就是。

Say the files are stored in a file called erasure.

假设文件存储在名为erasure的文件中。

$ cat erasure
fileABC
fileXYZ
dir/anotherFile

In this example we will erase three files, which are listed above.

在这个例子中,我们将删除上面列出的三个文件。

$ while read file; do > "$file"; done < erasure

Explanation:

  • while read file for each line in the given file, store the line in variable file
  • 在读取给定文件中每行的文件时,将该行存储在变量文件中

  • do > "$file" empty the file and output nothing in it (i.e. erase it)
  • do>“$ file”清空文件并输出任何内容(即删除它)

  • done < erasure specify the input file using < (redirection)
  • done <擦除使用<(重定向)指定输入文件< p>

Note: while this method preserves spaces in the path, it fails to handle backslashes and trailing white space, as noted by Charles Duffy. One way to fix both issues is to modify the loop as follows:

注意:虽然这个方法保留了路径中的空格,但它无法处理反斜杠和尾随空格,正如Charles Duffy所指出的那样。解决这两个问题的一种方法是修改循环,如下所示:

while IFS= read -r file; do > "$file"; done < erasure

Yet newlines in file names will still be a problem. The only way around this issue is to separate the file names using null termination (\0). The correct loop now becomes:

然而,文件名中的换行仍然是个问题。解决此问题的唯一方法是使用空终止(\ 0)分隔文件名。现在正确的循环变为:

while IFS= read -r -d '' file; do > "$file"; done < erasure

#2


4  

Truncate can be used to empty a file as well.

截断也可用于清空文件。

truncate --size 0 /path/to/file/here

#3


0  

You can do it quite nicely with GNU Parallel like this:

你可以用这样的GNU Parallel很好地完成它:

parallel '>' ::: TooLong.log BigBoy.dat NonExistant.file