0

in base directory i have folders like this:

1
2
3
4
5
10
110

so in each of them i have files like

  0010011.mp3  0010031.mp3  0010051.mp3  0010071.mp3  0010021.mp3  0010041.mp3  0010061.mp3

so i want to remove first 3 characters and last 1 character so files will look like this

 001.mp3   003.mp3   005.mp3   007.mp3   002.mp3   004.mp3  006.mp3

i tried this

for file in ??????*; do echo mv $file `echo $file | cut -c4-`; done

also this is not working in subdirectories it is just if files are in base directory

0

I would not advise just collecting all files that match *.mp3 and then trimming the file names. You MAY want to run this same script AGAIN later (like next week) and it should NOT try to rename the same files a second time, making them even shorter and have naming conflicts.

Your example file names seem to be 001xyz1.mp3 and you want xyz.mp3

     KEEP_DIR=$PWD 
     cd /your/music/base_dir        # sub-dirs holding .mp3

     RCOUNT=0 ; SKIP=0 ; FCC=0


 for FFF in */001???1.mp3    
     do
     LOC="$(dirname $FFF)"   
     BAS="$(basename $FFF)"
     TRIM1="${BAS#1.mp3}"     #could use ${BAS:4:3}
     NEWF="$LOC/${TRIM1%001}.mp3"
     if [ -e "$NEWF" ] ; then
        echo "#-- File $NEWF already exists, not renaming $FFF"
        SKIP=$((SKIP+1))
     else
        mv $FFF $NEWF
        MCOUNT=$((MCOUNT+1))
     fi
     FCC=$((FCC+1))
 done

 echo "Renamed $MCOUNT mp3 files, of $FCC found. (Skipped $SKIP)"
 cd $KEEP_DIR

 #--[eof]
| improve this answer | |
  • Don't forget to double quote "$variables" when you use them – roaima May 3 '18 at 6:53
0

If you're got rename (sometimes known as prename) you can do this in one command:

rename -n 's!(.*/)...(.*).(\.mp3)!$1$2$3!' */?????*.mp3

Alternatively you can loop through the files:

for f in */?????*.mp3
do
    echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(\.mp3)!\1\2\3!')"
done

In the first instance remove -n (or replace it with -v) to make it perform the work. In the second instance remove the first echo to allow it to apply the changes.

| improve this answer | |
  • output of second command is mv -v *.mp3 p3.mp3 – arlind Apr 27 '18 at 17:10
  • i need to run command in parent directory and it should work for all subdirectories – arlind Apr 27 '18 at 17:25
  • @arpa answer updated to run from the root of your folders. – roaima Apr 27 '18 at 17:58
  • for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(\.mp3)!\1\2\3!')"; done i get -bash: !: event not found – arlind Apr 27 '18 at 20:32
0

You may use sed to create one mv command line for each mp3 with a 7 characters long basename:

$ find . -name "???????.mp3" | sed 's/\(.*\)\(...\)\(...\)\(.\)\(\.mp3\)/mv \1\2\3\4\5 \1\3\5/'

After reviewing the output you can redirect it into a file and execute.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.