Reputation: 1190
I am running a script called filename_to_dir.sh
that takes an argument -s
inside a while loop. That argument is a .txt
file name. I am using the while loop to execute the script on each file it finds in a certain directory and then create subdirectories with the formatted file names. But before creating any directories, the file name is formatted and stripped of whitespace and couple of other things to avoid errors when feeding it to filename_to_dir.sh
. The only issue is directories are being crated with formatted names but the text file names remain intact. How could create a directory based on a formatted file name?
Current Results:
|-- text_files
| |-- this_is_new-1
| |-- test_11-today
| |-- hi_hi-Bye
| |-- this is new - 1.txt
| |-- test 11 - today.txt
| |-- hi HI - Bye.txt
Desired Outcome:
|-- text_files
| |-- this_is_new-1
| |-- test_11-today
| |-- hi_hi-Bye
| |-- this_is_new-1.txt
| |-- test_11-today.txt
| |-- hi_hi-Bye.txt
Two ways I have tried it:
while read -r file; do
new_file=$(echo "$file" | sed -re 's/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/' -e 's/ /_/g' -e 's/_-/-/g')
if [ "$file" != "$new_file" ]; then
echo mv "$file" "$new_file"
file_split.sh -s $file
fi
done < <(find $FILE_PATH -type f -name '*.txt')
AND
//Using find but i loose a way to check if file has been renamed already
while read -r file; do
file_split.sh -s $file
done < <(find $FILE_PATH -type f -exec bash -c 'echo "mv \"$1\" \"$(echo "$1" | sed -re '\''s/^([^-]*)-\s*([^\.]*)/\L\1\E-\2/'\'' -e '\''s/ /_/g'\'' -e '\''s/_-/-/g'\'')\""' - {} \;)
Upvotes: 0
Views: 81
Reputation: 7255
I think you should change echo mv "$file" "$new_file"
to mv "$file" "$new_file"
.
Upvotes: 1