bash cat /dev/null 到多个文件以清除日志等现有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30895546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cat /dev/null to multiple files to clear existing files like logs
提问by Srujan Kumar Gulla
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
一种清除已冻结我的 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 将多个文件清除为多个文件。
回答by ShellFish
Hard coded solutions
硬编码解决方案
tee
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:
正如查尔斯·达菲( Charles Duffy)所指出的,虽然echo -n
被认为是更好的实践cat /dev/null
,但使用更好的解决方案是。导致以下命令:printf ''
$ printf '' | tee file1 file2 file3
truncate
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
可能是您最初寻找的解决方案。该命令允许提供任意多个文件名参数。您可以轻松地使用它,如下所示:
$ 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 javafiles) 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 .
executefind
in current directory.
-maxdepth 2
recursion level, descend to directories in directory but no further (level 2). Set this to 1 to not descend orn
to descendn
times.-type f
only apply to files, not directories-name '*.java'
only apply to files ending in.java
-exec truncate --size 0 "{}" \;
truncate each file found (file name is stored in{}
)
find .
find
在当前目录执行.
-maxdepth 2
递归级别,下降到目录中的目录,但不再进一步(级别 2)。将此设置为 1 以不下降或n
下降n
时间。-type f
仅适用于文件,不适用于目录-name '*.java'
仅适用于以.java
-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 variablefile
do > "$file"
empty the file and output nothing in it (i.e. erase it)done < erasure
specify the input file using<
(redirection)
while read file
对于给定文件中的每一行,将该行存储在变量中file
do > "$file"
清空文件并在其中输出任何内容(即擦除它)done < erasure
使用<
(重定向)指定输入文件
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
回答by skrilled
Truncate can be used to empty a file as well.
截断也可用于清空文件。
truncate --size 0 /path/to/file/here
回答by Mark Setchell
You can do it quite nicely with GNU Parallellike this:
您可以像这样使用GNU Parallel很好地做到这一点:
parallel '>' ::: TooLong.log BigBoy.dat NonExistant.file