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
Use the ls Command to List Directories in Bash. We use the ls command to list items in the current directory in Bash. However, we can use */ to print directories only since all directories finish in a / with the -d option to assure that only the directories' names are displayed rather than their contents.
Open the command-line shell and write the 'ls” command to list only directories. The output will show only the directories but not the files. To show the list of all files and folders in a Linux system, try the “ls” command along with the flag '-a” as shown below.
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/ \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With