在 Linux 中将文件内容复制到新文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11262350/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:09:27  来源:igfitidea点击:

copy file content into new file in Linux

linux

提问by Ahmet Karakaya

I would like to know how to copy all content of a file (server.log) to a new file and removing the content from original file by using Linux commands. Actually it is easy to to that. But I actually want to be sure there will not be content update within that operations. Following Linux commands do what i want, but I have to be sure there is no change in server.log between command1-command2 execution.

我想知道如何使用 Linux 命令将文件 (server.log) 的所有内容复制到新文件并从原始文件中删除内容。其实很容易做到这一点。但我实际上想确保在该操作中不会有内容更新。按照 Linux 命令执行我想要的操作,但我必须确保在 command1-command2 执行之间 server.log 没有变化。

command1: #cp server.log serverNew.log 
command2: #truncate -l 0 server.log

采纳答案by Eric des Courtis

I would use a tool especially built for this purpose instead of using some ad hoc solution.

我会使用专门为此目的构建的工具,而不是使用一些临时解决方案。

Take a look at logrotate. You can use the command directly or set it up in a cron job.

看看logrotate。您可以直接使用该命令或在 cron 作业中设置它。

It's supports compression, running command after each rotation, rotating based on size or time etc...

它支持压缩,每次旋转后运行命令,根据大小或时间旋转等......

Based on your comment below I assume you are interested in these options:

根据您在下面的评论,我假设您对这些选项感兴趣:

postrotate/endscript

postrotate/endscript

The lines between postrotateand endscript(both of which must appear on lines by themselves) are executed (using /bin/sh) after the log file is rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script.If sharedscriptsis specified, whole pattern is passed to the script. See also prerotate. See sharedscriptsand nosharedscriptsfor error handling.

postrotate和之间的行endscript(两者都必须单独出现在行上/bin/sh)在日志文件轮换后执行(使用)。这些指令可能只出现在日志文件定义中。通常,日志文件的绝对路径作为第一个参数传递给脚本。如果sharedscripts指定,则整个模式将传递给脚本。另见prerotate。有关错误处理,请参阅sharedscriptsnosharedscripts

prerotate/endscript

prerotate/endscript

The lines between prerotateand endscript(both of which must appear on lines by themselves) are executed (using /bin/sh) before the log file is rotated and only if the log will actually be rotated. These directives may only appear inside a log file definition. Normally, the absolute path to the log file is passed as first argument to the script.If sharedscriptsis specified, whole pattern is passed to the script. See also postrotate. See sharedscriptsand nosharedscriptsfor error handling.

prerotate和之间的行endscript(两者都必须/bin/sh单独出现在行上)在日志文件轮换之前执行(使用),并且仅当日志实际轮换时才执行。这些指令可能只出现在日志文件定义中。通常,日志文件的绝对路径作为第一个参数传递给脚本。如果sharedscripts指定,则整个模式将传递给脚本。另见postrotate。有关错误处理,请参阅sharedscriptsnosharedscripts

回答by unwind

Don't copy, do a rename (with mv). The rename is atomic at the file system level, so any application writing a file with the old name will not collide.

不要复制,请重命名(使用mv)。重命名在文件系统级别是原子的,因此任何使用旧名称写入文件的应用程序都不会发生冲突。

回答by rad

After that you can use touchcommand to make sure

之后,您可以使用touch命令来确保

command3: #touch server.log

回答by DonCallisto

You can use those command in tandem: cp oldFile newFile; cat '' >> oldFile
cpcopy your file. The second command, that is executed afert the first, is use for overwriting the original file

您可以同时使用这些命令:cp oldFile newFile; cat '' >> oldFile
cp复制您的文件。在第一个命令之后执行的第二个命令用于覆盖原始文件

Obviously if your "program" (or script) that will run once you copy the content of old file into new one, open the file in write (and not in write and append) the second command isn't necessary.
Moreover for >>redirection, you have to verify that the noclobberoption is setted to "off" (1)

显然,如果您的“程序”(或脚本)将在您将旧文件的内容复制到新文件后运行,则以 write (而不是 write和 append)打开文件,则不需要第二个命令。
此外,对于>>重定向,您必须验证该noclobber选项是否设置为“关闭”(1)

回答by Cobra_Fast

Here is a simple C application that will (probably) do what you want:

这是一个简单的 C 应用程序,它将(可能)执行您想要的操作:

#include <stdlib.h>
#include <stdio.h>
#include <sys/file.h>

void main (int argc, char** argv)
{
    if (argc != 2)
        exit(1);

    FILE* fi = fopen(argv[1], "rb+");
    FILE* fo = fopen(argv[2], "wb");

    if (fi != NULL && fo != NULL && flock(fi, LOCK_EX) == 0)
    {
        while (feof(fi) == 0)
        {
            char* buf = malloc(4096);
            int bRead = 0;
            bRead = fread(buf, 1, 4096, fi);
            fwrite(buf, 1, bRead, fo);
        }

        frewind(fi);
        fputc(10, fi);

        flock(fi, LOCK_UN);
        fclose(fi);
        fclose(fo);
    }
else exit(1);

exit(0);
}

Call like: ./a.out oldfile newfile

像这样调用: ./a.out oldfile newfile

Warning: I have not actually tested this code, be sure to do some testingbefore you use this for any kind of important work.

警告:我还没有实际测试过这段代码,在将它用于任何类型的重要工作之前,请务必进行一些测试

Alternatively, you could also try something with the shell-tool flock: http://linux.die.net/man/1/flock

或者,您也可以尝试使用 shell-tool 进行一些操作flockhttp: //linux.die.net/man/1/flock