用于扫描子目录并将文件内容复制到另一个文件的 Bash 脚本

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

Bash script to scan sub-directories and copy contents of file to another file

bashshellksh

提问by AaronW

I need to do a bash command that will look through every home directory on a system, and copy the contents of the .forward file to a single file, along with copying the name of the directory it just copied from. So for example the final file would be something like forwards.txt and listeings would be

我需要执行一个 bash 命令,该命令将查看系统上的每个主目录,并将 .forward 文件的内容复制到单个文件,同时复制它刚刚从中复制的目录的名称。因此,例如最终文件将类似于 forwards.txt 和 listeings 将是

/home/user1
[email protected]
/home/user2
[email protected]

I've used this to list them to screen.

我用它来列出它们进行筛选。

find /home -name '*' | cat /home/*/.forward

and it will print out the forward in each file but I'm not getting it to prefix it with which home directory it came from. Would I need to use a loop to do this? I had this test loop,

它会打印出每个文件中的转发,但我没有得到它以它来自哪个主目录作为前缀。我需要使用循环来做到这一点吗?我有这个测试循环,

#!/bin/bash
for i in /home/*
        do
                if [ -d $i ]
                        then
                                pwd >> /tmp/forwards.txt
                                cat /home/*/.forward >> /tmp/forwards.txt
                fi
        done

But it went through the four home directories on my test setup and the forwards.txt file had the following listed four times.

但是它通过了我的测试设置中的四个主目录,并且 forwards.txt 文件四次列出了以下内容。

/tmp
[email protected]
[email protected]
[email protected]
[email protected]

Thanks.

谢谢。

采纳答案by Slava Semushin

There is corrected version of your script:

您的脚本有更正版本:

#!/bin/bash
for i in /home/*
        do
                if [ -f "$i/.forward" ]
                then
                                echo "$i" >> /tmp/forwards.txt
                                cat "$i/.forward" >> /tmp/forwards.txt
                fi
        done

Some points:

几点:

  • we checks for presents of .forwardfile inside home directory instead of existence of home directory itself

  • on each iteration $icontains name of home directory (like /home/user1). So we use its value instead of output of pwdcommand which always returns current directory (it doesn't change in our case)

  • instead of /home/*/.forwardwe use "/home/$i/.forward"because *after substitution gives to us all directories, while we need only current

  • 我们检查.forward主目录中文件的存在,而不是主目录本身的存在

  • 在每次迭代中都$i包含主目录的名称(如/home/user1)。所以我们使用它的值而不是pwd命令的输出,它总是返回当前目录(在我们的例子中它不会改变)

  • 而不是/home/*/.forward我们使用,"/home/$i/.forward"因为*替换后给了我们所有目录,而我们只需要当前



Another, shortest version of this script may looks like this:

此脚本的另一个最短版本可能如下所示:

find /home -type f -name '.forward' | while read F; do
    dirname "$F" >>/tmp/forwards.txt
    cat "$F" >>/tmp/forwards.txt
done

回答by glenn Hymanman

I would write

我会写

for fwd in /home/*/.forward; do
    dirname "$fwd"
    cat "$fwd"
done > forwards.txt

回答by Greg Burghardt

A one liner (corrected):

一个班轮(更正):

find /home -maxdepth 2 -name ".forward" -exec echo "{}" >> /tmp/forwards.txt \; -exec cat "{}" >> /tmp/forwards.txt \;

This will output:

这将输出:

/home/user1/.forward
[email protected]
[email protected]
/home/user2/.forward
[email protected]