I have setup the docker container to run the model evaluation.
Write a bash script to rename all files of the form "merged_*" to "finished_*". I want to call it by running bash rename.sh /path/to/files.
Write a bash script to rename all files of the form "merged_*" to "finished_*". I want to call it by running bash rename.sh /path/to/files.
Okay, here is a Bash script named `rename.sh` that accomplishes this. **rename.sh:**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/bin/bash # Script to rename files matching "merged_*" to "finished_*" # in a specified directory. # --- Argument Handling --- if [ $# -ne 1 ]; then echo "Usage: $0 /path/to/directory" >&2 echo "Example: $0 ./my_processed_data" >&2 exit 1 fi TARGET_DIR="$1" # --- Directory Validation --- # Check if the provided argument is actually a directory if [ ! -d "$TARGET_DIR" ]; then echo "Error: Target '$TARGET_DIR' is not a valid directory." >&2 exit 1 fi # --- Change to target directory (simplifies file paths) --- # Using pushd/popd is slightly safer than cd if the script needs to return # pushd "$TARGET_DIR" > /dev/null || { echo "Error: Could not change to directory '$TARGET_DIR'." >&2; exit 1; } # Alternatively, using cd and checking its success: if ! cd "$TARGET_DIR"; then echo "Error: Could not change to directory '$TARGET_DIR'." >&2 exit 1 fi echo "Processing files in directory: '$(pwd)'" # Show the actual directory # --- File Processing --- # Enable nullglob option: if no files match the pattern, the loop won't run at all. shopt -s nullglob files_processed=0 files_skipped=0 rename_errors=0 # Loop through all items starting with "merged_" in the current directory for filename in merged_*; do # Ensure it's a regular file (not a directory, symlink etc.) if [ ! -f "$filename" ]; then # Silently skip non-files, or uncomment below to be verbose # echo "Skipping non-file item: '$filename'" continue fi # Construct the new filename using Bash parameter expansion # ${filename#merged_} removes the prefix "merged_" from the start new_filename="finished_${filename#merged_}" # --- Safety Check: Prevent Overwriting Existing 'finished_*' File --- # Check if a file with the new name already exists if [ -e "$new_filename" ]; then echo "Skipping rename: '$new_filename' already exists." >&2 files_skipped=$((files_skipped + 1)) continue # Skip to the next file in the loop fi # --- Perform Rename --- echo "Renaming '$filename' -> '$new_filename'" # Use 'mv' to rename the file. Add error checking. if mv "$filename" "$new_filename"; then files_processed=$((files_processed + 1)) else # This might happen due to permissions issues, etc. echo "Error renaming '$filename'. Check permissions or other issues." >&2 rename_errors=$((rename_errors + 1)) fi done # Disable nullglob again (good practice) shopt -u nullglob # --- Return to original directory (if using pushd/popd) --- # popd > /dev/null # If using cd, you might want 'cd - > /dev/null' to go back, # but it's often not necessary if the script ends here. # --- Summary --- echo "--------------------" echo "Finished processing." echo "Files successfully renamed: $files_processed" echo "Files skipped (target existed): $files_skipped" echo "Rename errors encountered: $rename_errors" # Exit with a non-zero status code if any errors occurred if [ "$rename_errors" -gt 0 ]; then exit 1 else exit 0 fi |
1 2 3 4 5 6 7 8 9 | # Example: If your files are in a subdirectory named 'data_files' bash rename.sh ./data_files # Example: If your files are in /home/user/project/output bash rename.sh /home/user/project/output # You can also run it directly if it's in your PATH or current dir # ./rename.sh /path/to/files |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/bin/bash # Script to rename files matching "merged_*" to "finished_*" # in a specified directory. # --- Argument Handling --- if [ $# -ne 1 ]; then echo "Usage: $0 /path/to/directory" >&2 echo "Example: $0 ./my_processed_data" >&2 exit 1 fi TARGET_DIR="$1" # --- Directory Validation --- # Check if the provided argument is actually a directory if [ ! -d "$TARGET_DIR" ]; then echo "Error: Target '$TARGET_DIR' is not a valid directory." >&2 exit 1 fi # --- Change to target directory (simplifies file paths) --- # Using pushd/popd is slightly safer than cd if the script needs to return # pushd "$TARGET_DIR" > /dev/null || { echo "Error: Could not change to directory '$TARGET_DIR'." >&2; exit 1; } # Alternatively, using cd and checking its success: if ! cd "$TARGET_DIR"; then echo "Error: Could not change to directory '$TARGET_DIR'." >&2 exit 1 fi echo "Processing files in directory: '$(pwd)'" # Show the actual directory # --- File Processing --- # Enable nullglob option: if no files match the pattern, the loop won't run at all. shopt -s nullglob files_processed=0 files_skipped=0 rename_errors=0 # Loop through all items starting with "merged_" in the current directory for filename in merged_*; do # Ensure it's a regular file (not a directory, symlink etc.) if [ ! -f "$filename" ]; then # Silently skip non-files, or uncomment below to be verbose # echo "Skipping non-file item: '$filename'" continue fi # Construct the new filename using Bash parameter expansion # ${filename#merged_} removes the prefix "merged_" from the start new_filename="finished_${filename#merged_}" # --- Safety Check: Prevent Overwriting Existing 'finished_*' File --- # Check if a file with the new name already exists if [ -e "$new_filename" ]; then echo "Skipping rename: '$new_filename' already exists." >&2 files_skipped=$((files_skipped + 1)) continue # Skip to the next file in the loop fi # --- Perform Rename --- echo "Renaming '$filename' -> '$new_filename'" # Use 'mv' to rename the file. Add error checking. if mv "$filename" "$new_filename"; then files_processed=$((files_processed + 1)) else # This might happen due to permissions issues, etc. echo "Error renaming '$filename'. Check permissions or other issues." >&2 rename_errors=$((rename_errors + 1)) fi done # Disable nullglob again (good practice) shopt -u nullglob # --- Return to original directory (if using pushd/popd) --- # popd > /dev/null # If using cd, you might want 'cd - > /dev/null' to go back, # but it's often not necessary if the script ends here. # --- Summary --- echo "--------------------" echo "Finished processing." echo "Files successfully renamed: $files_processed" echo "Files skipped (target existed): $files_skipped" echo "Rename errors encountered: $rename_errors" # Exit with a non-zero status code if any errors occurred if [ "$rename_errors" -gt 0 ]; then exit 1 else exit 0 fi |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #!/bin/bash # Script to rename files matching "merged_*" to "finished_*" # in a specified directory. # --- Argument Handling --- if [ $# -ne 1 ]; then echo "Usage: $0 /path/to/directory" >&2 echo "Example: $0 ./my_processed_data" >&2 exit 1 fi TARGET_DIR="$1" # --- Directory Validation --- # Check if the provided argument is actually a directory if [ ! -d "$TARGET_DIR" ]; then echo "Error: Target '$TARGET_DIR' is not a valid directory." >&2 exit 1 fi # --- Change to target directory (simplifies file paths) --- # Using pushd/popd is slightly safer than cd if the script needs to return # pushd "$TARGET_DIR" > /dev/null || { echo "Error: Could not change to directory '$TARGET_DIR'." >&2; exit 1; } # Alternatively, using cd and checking its success: if ! cd "$TARGET_DIR"; then echo "Error: Could not change to directory '$TARGET_DIR'." >&2 exit 1 fi echo "Processing files in directory: '$(pwd)'" # Show the actual directory # --- File Processing --- # Enable nullglob option: if no files match the pattern, the loop won't run at all. shopt -s nullglob files_processed=0 files_skipped=0 rename_errors=0 # Loop through all items starting with "merged_" in the current directory for filename in merged_*; do # Ensure it's a regular file (not a directory, symlink etc.) if [ ! -f "$filename" ]; then # Silently skip non-files, or uncomment below to be verbose # echo "Skipping non-file item: '$filename'" continue fi # Construct the new filename using Bash parameter expansion # ${filename#merged_} removes the prefix "merged_" from the start new_filename="finished_${filename#merged_}" # --- Safety Check: Prevent Overwriting Existing 'finished_*' File --- # Check if a file with the new name already exists if [ -e "$new_filename" ]; then echo "Skipping rename: '$new_filename' already exists." >&2 files_skipped=$((files_skipped + 1)) continue # Skip to the next file in the loop fi # --- Perform Rename --- echo "Renaming '$filename' -> '$new_filename'" # Use 'mv' to rename the file. Add error checking. if mv "$filename" "$new_filename"; then files_processed=$((files_processed + 1)) else # This might happen due to permissions issues, etc. echo "Error renaming '$filename'. Check permissions or other issues." >&2 rename_errors=$((rename_errors + 1)) fi done # Disable nullglob again (good practice) shopt -u nullglob # --- Return to original directory (if using pushd/popd) --- # popd > /dev/null # If using cd, you might want 'cd - > /dev/null' to go back, # but it's often not necessary if the script ends here. # --- Summary --- echo "--------------------" echo "Finished processing." echo "Files successfully renamed: $files_processed" echo "Files skipped (target existed): $files_skipped" echo "Rename errors encountered: $rename_errors" # Exit with a non-zero status code if any errors occurred if [ "$rename_errors" -gt 0 ]; then exit 1 else exit 0 fi |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Processing files in directory: '/usr/src/app/foo' Renaming 'merged_0' -> 'finished_0' Renaming 'merged_1' -> 'finished_1' Renaming 'merged_2' -> 'finished_2' Renaming 'merged_3' -> 'finished_3' Renaming 'merged_4' -> 'finished_4' Renaming 'merged_5' -> 'finished_5' Renaming 'merged_6' -> 'finished_6' Renaming 'merged_7' -> 'finished_7' Renaming 'merged_8' -> 'finished_8' Renaming 'merged_9' -> 'finished_9'</p> <hr /> <p>Finished processing. Files successfully renamed: 10 Files skipped (target existed): 0 Rename errors encountered: 0 |
final: True Resulting in output: True