按首字母筛选顶层目录复制至服务器的脚本问题求助
Got it, let's fix this issue for you. The root problem with your current script is that it isn't restricting its match to only top-level directories—it's recursively picking up any subdirectory that starts with "u" too, which is causing those duplicate copies. Here are two straightforward, reliable approaches to fix this:
Method 1: Use rsync (Recommended)
rsync is perfect for this kind of selective directory copy because it lets you explicitly include/exclude items with precision. Run this command:
rsync -av --include='u*/' --exclude='*' /your/workstation/source/path/ /your/server/destination/path/
Let's break down what each part does:
-av: Enables archive mode (preserves permissions, timestamps, etc.) and verbose output so you can see what's being copied.--include='u*/': Tellsrsyncto only include top-level directories that start with "u" (the trailing/ensures we only match directories, not files).--exclude='*': Excludes everything else, which prevents any recursive matching of subdirectories.
Method 2: Use find + cp
If you prefer using basic Unix tools, find can target only top-level directories and pass them to cp:
find /your/workstation/source/path -maxdepth 1 -type d -name 'u*' -exec cp -r {} /your/server/destination/path/ \;
Here's the breakdown:
-maxdepth 1: Restrictsfindto only look at the top level of the source directory (no subdirectories).-type d: Ensures we only match directories, not files.-name 'u*': Filters for directories starting with "u".-exec cp -r {} ...: Copies each matching directory recursively to your server destination.
Just make sure to replace /your/workstation/source/path and /your/server/destination/path with your actual directory paths. Both methods will avoid copying those unwanted nested "u" subdirectories and only grab the top-level ones you need.
内容的提问来源于stack exchange,提问作者Jonathan Becker

