Skip to content

Instantly share code, notes, and snippets.

@egalink
Created December 11, 2023 19:11
Show Gist options
  • Select an option

  • Save egalink/8e5acf32b6f3adb487cfb7d2caa594c0 to your computer and use it in GitHub Desktop.

Select an option

Save egalink/8e5acf32b6f3adb487cfb7d2caa594c0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script can copy or move files from one folder to another,
# removing special characters in file names that can cause file system
# inconsistencies
# HOW TO USE:
# 1. Open a terminal and navigate to the directory containing the script file.
# 2. Make the script executable by running the following command:
# -> chmod +x bkpfiles.sh
# 3. Run the script with the source directory and destination directory as arguments:
# -> ./bkpfiles.sh /path/to/source/directory /path/to/destination/directory
src_dir=$1
dest_dir=$2
# Create destination directory if it doesn't exist
mkdir -p "$dest_dir"
# Iterate over each file in the source directory
for file in "$src_dir"/*; do
# Remove special characters from the filename
filename=$(basename "$file" | tr -cd '[:alnum:] ._-')
# Copy the file to the destination directory with the sanitized filename
cp "$file" "$dest_dir/$filename"
# Or you can move all files to the destination directory with this command:
# mv "$file" "$dest_dir/$filename"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment