I can't find a way to put an entry in read
that contains spaces? I want to put directories in the "Enter directory to be cleaned:" read
. I think it is reading the spaces as separate variables. Input would be something like /home/user/apple sauce
The "cleaning" is just removing special characters from filenames.
#!/bin/bash
read -p "Enter directory to be cleaned: " directory
echo "Current Directory Structure"
/usr/bin/ls -la $directory
read input
if [ "$input" == "y" ]
then
echo "Cleaning files..."
for file in $directory; do mv $file $(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g'); done &
else
stop
fi
Another issue I am facing is the cleanup is repeating the entire directory when it creates the new filename. If I run that for file in *; do mv "$file" $(echo "$file" | sed -e 's/[^A-Za-z0-9._-]/_/g'); done &
command in the directory itself, it just creates the new filename. If I specify the directory it writes out the whole directory:
++ sed -e 's/[^A-Za-z0-9._-]/_/g'
++ echo '/home/apples/scratch/test1/test:something?'
+ mv '/home/apples/scratch/test1/test:something?' _home_apples_scratch_test1_test_something_
I want it to just change the filename but having issues. Any help is thankful.