使用Bash查找和复制文件[重复]

时间:2022-04-11 19:27:09

This question already has an answer here:

这个问题在这里已有答案:

Anybody has an alternate way of finding and copying files in bash than:

任何人都有另一种在bash中查找和复制文件的方法:

find . -ctime -15 | awk '{print "cp " $1 " ../otherfolder/"}' | sh

I like this way because it's flexible, as I'm building my command (can by any command) and executing it after.

我喜欢这种方式,因为它很灵活,因为我正在构建我的命令(可以通过任何命令)并在之后执行它。

Are there other ways of streamlining commands to a list of files?

是否有其他方法可以将命令简化为文件列表?

Thanks

谢谢

6 个解决方案

#1


65  

I would recommend using find's -exec option:

我建议使用find的-exec选项:

find . -ctime 15 -exec cp {} ../otherfolder \;

找 。 -ctime 15 -exec cp {} ../otherfolder \;

As always, consult the manpage for best results.

与往常一样,请参阅联机帮助页以获得最佳结果。

#2


8  

I usually use this one:

我通常使用这个:

find . -ctime -15 -exec cp {} ../otherfolder/ \;

#3


8  

If your cp is GNU's:

如果你的cp是GNU的:

find . -ctime 15 -print0 | xargs -0 cp --target-directory=../otherfolder

#4


4  

You can do it with xargs:

你可以用xargs做到这一点:

$ find . -ctime 15 -print0 | xargs -0 -I{} cp {} ../otherfolder

See also grep utility in shell script.

另请参见shell脚本中的grep实用程序。

#5


1  

Use this for copy and many other things:

用于复制和许多其他事情:

for f in (find /apps -type f -name 'foo'); do cp ${f} ${f}.bak; cmd2; cmd3; done;

#6


-1  

-exec is likely the way to go, unless you have far too many files. Then use xargs.

-exec可能是要走的路,除非你有太多的文件。然后使用xargs。

#1


65  

I would recommend using find's -exec option:

我建议使用find的-exec选项:

find . -ctime 15 -exec cp {} ../otherfolder \;

找 。 -ctime 15 -exec cp {} ../otherfolder \;

As always, consult the manpage for best results.

与往常一样,请参阅联机帮助页以获得最佳结果。

#2


8  

I usually use this one:

我通常使用这个:

find . -ctime -15 -exec cp {} ../otherfolder/ \;

#3


8  

If your cp is GNU's:

如果你的cp是GNU的:

find . -ctime 15 -print0 | xargs -0 cp --target-directory=../otherfolder

#4


4  

You can do it with xargs:

你可以用xargs做到这一点:

$ find . -ctime 15 -print0 | xargs -0 -I{} cp {} ../otherfolder

See also grep utility in shell script.

另请参见shell脚本中的grep实用程序。

#5


1  

Use this for copy and many other things:

用于复制和许多其他事情:

for f in (find /apps -type f -name 'foo'); do cp ${f} ${f}.bak; cmd2; cmd3; done;

#6


-1  

-exec is likely the way to go, unless you have far too many files. Then use xargs.

-exec可能是要走的路,除非你有太多的文件。然后使用xargs。