如何使用bash列出目录中的文件?

Art*_*hur 38 bash

如何只将目录中的常规文件(忽略子目录和链接)复制到同一目的地?(bash on Linux) 非常多的文件

Mu *_*iao 59

for file in /source/directory/*
do
    if [[ -f $file ]]; then
        #copy stuff ....
    fi
done
Run Code Online (Sandbox Code Playgroud)


pop*_*tea 25

要列出常规文件/my/sourcedir/,而不是在子目录中递归查找:

find /my/sourcedir/ -type f -maxdepth 1
Run Code Online (Sandbox Code Playgroud)

要将这些文件复制到/my/destination/:

find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \;
Run Code Online (Sandbox Code Playgroud)


gle*_*man 8

要扩展poplitea的答案,您不必为每个文件执行cp:用于一次xargs复制多个文件:

find /my/sourcedir -maxdepth 1 -type f -print0 | xargs -0 cp -t /my/destination
Run Code Online (Sandbox Code Playgroud)

要么

find /my/sourcedir -maxdepth 1 -type f -exec cp -t /my/destination '{}' +
Run Code Online (Sandbox Code Playgroud)