Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

linux - Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?

I have a list of file names in a directory (/path/to/local). I would like to remove a certain number of characters from all of those filenames.

Example filenames:

iso1111_plane001_00321.moc1
iso1111_plane002_00321.moc1
iso2222_plane001_00123.moc1

In every filename I wish to remove the last 5 characters before the file extension.

For example:

iso1111_plane001_.moc1
iso1111_plane002_.moc1
iso2222_plane001_.moc1

I believe this can be done using sed, but I cannot determine the exact coding. Something like...

for filename in /path/to/local/*.moc1; do
    mv $filname $(echo $filename | sed -e 's/.....^//');
done

...but that does not work. Sorry if I butchered the sed options, I do not have much experience with it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 mv $filname $(echo $filename | sed -e 's/......moc1$//');

or

 echo ${filename%%?????.moc1}.moc1

%% is a bash internal operator...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...