Please place your code snippets inside [CODE]...[/CODE]
tags for better readability. You may type those yourself or click the "
#" button in the edit controls.
I would probably do it with
find, but as is your code is very close to working for the examples as given.
Code:
#!/bin/bash
for file in ./*T* ; do mv -v $file ${file%_*}; done
The red part does not make sense, but this should work:
Code:
#!/bin/bash
for file in ./*T* ; do mv -v $file ${file}_*; done
If the examples are truly representative of the actual files and directories, and the files are all in the parent directory of the targets where the script is run, that will be sufficient.
*** EDIT
pan64's post made me do a double-think and realise the above is not right...
So, falling back to
find which I should have done first, and cleaning up after myself:
Code:
.
├── 1996-08-30T02.11.41.940
├── 1996-08-30T02.11.41.940_5.1
├── 1997-03-04T17.24.17.650
├── 1997-03-04T17.24.17.650_5.0
├── 1997-04-01T15.43.26.390
└── 1997-04-01T15.43.26.390_4.5
while read file; do
mv $file ${file}_*
done< <(find ./ -type f -name '*T*')
.
├── 1996-08-30T02.11.41.940_5.1
│ └── 1996-08-30T02.11.41.940
├── 1997-03-04T17.24.17.650_5.0
│ └── 1997-03-04T17.24.17.650
└── 1997-04-01T15.43.26.390_4.5
└── 1997-04-01T15.43.26.390
That will still be fragile if you have filenames with spaces or special characters, but I leave that detail for an exercise!