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

醉酒当歌 提交于 2019-12-18 01:23:46

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!