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
You can actually do all these manipulations in Bash itself.
Since you have
-maxdepth 1
it looks like you can just use a wildcard loop instead offind
so I refactored that, too.Briefly,
${var#prefix}
expands to the value ofvar
withprefix
removed, and${var%suffix}
correspondingly performs the same substitution with a suffix. Finally,${var,,}
produces the lowercase version of the value. Then we simply assemble the file name structure you want from those parts.change the
sed
aseg:
-[^-]*-
selects the string between two-
, seds
replace it with-
Also you can use
grep
command to get the name of the directory as^[^.]*
selects the portion in filename, that doesnt contain.
that is ommits the extension.EDIT
To get the name of the file
(?<=-)
lookbehind assertion checks if the pattern is preseced by-
(?=-)
lookahead assertion checks if the pattern is followed by-
[^.]*
pattern matches anything other than.
, selects the filename