bash 如何使用bash列出目录中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7265272/
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
How to list files in directory using bash?
提问by Arthur
How to copy only the regular files in a directory (ignoring sub-directories and links) to the same destination? (bash on Linux) A very large number of files
如何仅将目录中的常规文件(忽略子目录和链接)复制到同一目的地?(Linux 上的 bash) 大量文件
回答by Mu Qiao
for file in /source/directory/*
do
if [[ -f $file ]]; then
#copy stuff ....
fi
done
回答by poplitea
To list regular files in /my/sourcedir/
, not looking recursively in subdirs:
要在 中列出常规文件/my/sourcedir/
,而不是在子目录中递归查找:
find /my/sourcedir/ -type f -maxdepth 1
To copy these files to /my/destination/
:
要将这些文件复制到/my/destination/
:
find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \;
回答by glenn Hymanman
To expand on poplitea's answer, you don't have to exec cp for each file: use xargs
to copy multiple files at a time:
要扩展poplitea 的答案,您不必为每个文件执行 cp:用于一次xargs
复制多个文件:
find /my/sourcedir -maxdepth 1 -type f -print0 | xargs -0 cp -t /my/destination
or
或者
find /my/sourcedir -maxdepth 1 -type f -exec cp -t /my/destination '{}' +