How to list files in directory using bash? [closed]
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionHow 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 '{}' +
精彩评论