You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux批量清理文件名:解决USB复制时特殊字符导致文件无法复制问题

Handling Special Characters When Copying USB Files on Linux

I’ve run into this exact headache before—those tricky filenames with weird symbols, non-ASCII characters, or control codes always trip up the basic cp command. Here are a few robust, batch-friendly solutions to fix this without tedious manual renaming:

Solution 1: Use rsync (The Simplest Fix)

rsync is way more resilient to special characters than plain cp, and it automatically preserves permissions, timestamps, and directory structure. Just run this command:

rsync -av /path/to/source/ /path/to/destination/
  • -a: Enables archive mode (keeps all file attributes intact)
  • -v: Gives verbose output so you can track what’s being copied
    This should handle most problematic filenames out of the box without needing to rename anything first.

Solution 2: Batch Rename Problematic Files First

If rsync still hits snags (like super obscure control characters), you can batch-clean filenames to strip out invalid characters.

Option A: Use the rename Command

Most Linux distros come with the Perl-based rename tool. Run this dry-run command first to preview changes:

find /path/to/source -type f -exec rename -n 's/[^a-zA-Z0-9\.\-_]/_/g' {} \;
  • Remove the -n flag once you’re happy with the proposed changes (the -n does a test run without modifying files).
  • The regex [^a-zA-Z0-9\.\-_] targets any character that’s not a letter, number, dot, hyphen, or underscore, replacing it with an underscore.

Option B: Use tr if rename Isn’t Available

If you don’t have rename installed, use a shell loop with tr to clean filenames:

find /path/to/source -type f | while read -r file; do
    # Keep only safe characters, delete others automatically
    new_name=$(echo "$file" | tr -cd 'a-zA-Z0-9\.\-_/')
    if [ "$file" != "$new_name" ]; then
        mv "$file" "$new_name"
        echo "Renamed: $file → $new_name"
    fi
done
  • -cd tells tr to delete any character not in the allowed set.
  • The / is included to preserve the directory structure in the file path.

Solution 3: Use tar to Archive and Extract

Tar excels at handling complex filenames because it stores metadata properly. You can either create an archive first or pipe directly between source and destination:

Create an Archive (Ideal for Large Drives)

# Create archive from source
cd /path/to/source
tar -cf ../usb_backup.tar .

# Extract to destination
cd /path/to/destination
tar -xf ../usb_backup.tar

Direct Pipe (No Intermediate File)

tar -cf - /path/to/source | tar -xf - -C /path/to/destination

This sends the archive directly to stdout and pipes it to the destination’s extract command, saving disk space.

Pro Tips

  • Always test these commands on a small subset of files first (e.g., copy a few problematic files to a test folder and run the rename/copy command there).
  • If your source USB uses a FAT filesystem, some characters (like :, *, ?) are invalid on FAT but might exist on other filesystems—renaming is your best bet here.

内容的提问来源于stack exchange,提问作者user1980828

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:17:22