我想删除目录中每个文件的第一行,并通过在每个文件名的末尾附加一个“.tmp”来保存相应的输出。例如,如果有一个文件名input.txt,内容如下:
line 1
line 2
Run Code Online (Sandbox Code Playgroud)
我想在同一个目录中创建一个名称input.txt.tmp为以下内容的文件
line 2
Run Code Online (Sandbox Code Playgroud)
我正在尝试这个命令:
find . -type f | xargs -I '{}' tail -n +2 '{}' > '{}'.tmp
Run Code Online (Sandbox Code Playgroud)
问题是,它不是将输出写入带有.tmp后缀的单独文件,而是仅创建一个名为{}.tmp. 我知道这是因为输出重定向是在xargs完全完成后完成的。但是有什么方法可以告诉xargs输出重定向是它的参数的一部分吗?
find请注意,您可以与 一起使用-exec,而不需要通过管道传输到xargs:
find . -type f -exec sh -c 'f={}; tail -n+2 $f > $f.tmp' \;
^^^^ ^^^^^^^^^^^^^^^^^^^^^
| perform the tail and redirection
store the name of the file
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4910 次 |
| 最近记录: |