I have text files in a directory that I want to organize. I am trying to do so thru bash by creating a directory for each file. As I move the file to its respective new folder, I want to modify the original filename
and create an additional text file labeld info.txt
that holds the original file name(this file is also place in the new folder). The files name change goes from xxx-file_name-aa1.txt
to xxx-aa1.txt
. Anything between the first and last -
is stripped out. The folder has the same name as the file xxx-aa1
.
test.sh (my sed statement strips whitspaces and turns everything to lower case except anything after the last -
FILE_PATH="/media/sf_linux_sandbox/txt_files/"
while read -r file; do
#sed statement empty
new_name=$(echo "$file" | sed 's/^\(.*\)\(-[^-]*\)$/\L\1\E\2/; s/ *- */-/; s/^\(.*\) \+- *\([^-]*\)$/\1-\2/; s/ /_/g')
mv "$file" "$new_name"
done < <(find $FILE_PATH -maxdepth 1 -type f -name '*.txt')
Input:
|-- ./
| |-- xxxx-test_file1-aa1.txt
| |-- xxxx-test_file2-bb2.txt
Desired Output:
|-- ./
| |-- xxxx-aa1
| |--xxx-aa1.txt
| |--info.txt // contains name 'test_file1'
| |-- xxxx-bb2
|--xxx-bb2.txt
|--info.txt // contains name 'test_file2'
Current Output
|-- ./
| |-- xxxx-test_file-aa1
| |--xxxx-test_file-aa1.txt
| |-- xxxx-test_file-bb2
|--xxxx-test_file-bb2.txt