开发者

How to list files in directory using bash? [closed]

Closed. This question 开发者_JAVA技巧is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 years ago.

Improve this question

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


for file in /source/directory/*
do
    if [[ -f $file ]]; then
        #copy stuff ....
    fi
done


To list regular files in /my/sourcedir/, not looking recursively in subdirs:

find /my/sourcedir/ -type f -maxdepth 1

To copy these files to /my/destination/:

find /my/sourcedir/ -type f -maxdepth 1 -exec cp {} /my/destination/ \;


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:

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 '{}' +
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜