批量移动并重命名XML文件至指定目录的技术需求咨询
Solution to Distribute XML Files to Folders
Hey there! Let's figure out how to get those XML files into their respective folders quickly. Since you don't need to match the XML numbers to the folders, we can either pair them sequentially or randomly—both approaches work perfectly for your use case. Here's a step-by-step solution using a simple shell script:
Step 1: Navigate to the Base Directory
First, head to your /home/ss30 folder where all your XML files and ss30-* folders live:
cd /home/ss30
Step 2: Create the Shell Script
Use a text editor like nano to create a script file (we'll call it distribute_xmls.sh):
nano distribute_xmls.sh
Paste this code into the editor:
#!/bin/bash # Collect all XML files and target folders into arrays xml_files=(*.xml) target_folders=(ss30-*/) # Verify counts match (500 XMLs and 500 folders) if [ ${#xml_files[@]} -ne ${#target_folders[@]} ]; then echo "Oops: Number of XML files doesn't match the number of target folders!" exit 1 fi # Optional: Shuffle the lists for random pairing (remove if you want sequential) xml_files=($(shuf -e "${xml_files[@]}")) target_folders=($(shuf -e "${target_folders[@]}")) # Loop through each pair and move/rename the XML for index in "${!xml_files[@]}"; do current_xml="${xml_files[$index]}" current_folder="${target_folders[$index]}" template_dir="${current_folder}templates/" # Create the templates directory if it doesn't exist mkdir -p "$template_dir" # Move the XML file and rename it to templates.xml # Use 'cp' instead of 'mv' if you want to keep the original XML files mv "$current_xml" "${template_dir}templates.xml" echo "Processed: Moved $current_xml to $template_dir as templates.xml" done
Step 3: Make the Script Executable
Give the script permission to run:
chmod +x distribute_xmls.sh
Step 4: Run the Script
Execute the script to start distributing the files:
./distribute_xmls.sh
Key Notes:
- Random vs Sequential Pairing: The script includes a shuffle step (using
shuf) to randomly assign XML files to folders. If you prefer to pair the first XML with the first folder, second XML with second folder, etc., just delete the two lines that useshuf. - Preserve Original Files: If you want to keep the original XML files in
/home/ss30instead of moving them, replacemvwithcpin the script. - Safety Check: The script first checks if the number of XML files matches the number of folders, so you don't end up with unassigned files or folders.
内容的提问来源于stack exchange,提问作者Noob User
相关产品推荐
相关产品推荐

