问题
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
回答1:
for file in /source/directory/*
do
if [[ -f $file ]]; then
#copy stuff ....
fi
done
回答2:
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/ \;
回答3:
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 '{}' +
来源:https://stackoverflow.com/questions/7265272/how-to-list-files-in-directory-using-bash