bash - remove first 3 characters and last 1 character from all mp3 files in all subdirectory

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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
linux bash
migrated from serverfault.com May 3 at 5:54
This question came from our site for system and network administrators.
add a comment |Â
up vote
0
down vote
favorite
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
linux bash
migrated from serverfault.com May 3 at 5:54
This question came from our site for system and network administrators.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
linux bash
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
linux bash
asked Apr 27 at 15:06
arlind
82
82
migrated from serverfault.com May 3 at 5:54
This question came from our site for system and network administrators.
migrated from serverfault.com May 3 at 5:54
This question came from our site for system and network administrators.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
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]
Don't forget to double quote"$variables"when you use them
â roaima
May 3 at 6:53
add a comment |Â
up vote
0
down vote
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)!123!')"
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.
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
add a comment |Â
up vote
0
down vote
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 12345 135/'
After reviewing the output you can redirect it into a file and execute.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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]
Don't forget to double quote"$variables"when you use them
â roaima
May 3 at 6:53
add a comment |Â
up vote
0
down vote
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]
Don't forget to double quote"$variables"when you use them
â roaima
May 3 at 6:53
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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]
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]
answered May 3 at 2:25
Joseph in Atlanta
Don't forget to double quote"$variables"when you use them
â roaima
May 3 at 6:53
add a comment |Â
Don't forget to double quote"$variables"when you use them
â roaima
May 3 at 6:53
Don't forget to double quote
"$variables" when you use themâ roaima
May 3 at 6:53
Don't forget to double quote
"$variables" when you use themâ roaima
May 3 at 6:53
add a comment |Â
up vote
0
down vote
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)!123!')"
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.
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
add a comment |Â
up vote
0
down vote
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)!123!')"
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.
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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)!123!')"
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.
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)!123!')"
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.
edited May 3 at 6:57
answered Apr 27 at 16:01
roaima
39.4k545106
39.4k545106
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
add a comment |Â
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
output of second command is mv -v *.mp3 p3.mp3
â arlind
Apr 27 at 17:10
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
i need to run command in parent directory and it should work for all subdirectories
â arlind
Apr 27 at 17:25
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
@arpa answer updated to run from the root of your folders.
â roaima
Apr 27 at 17:58
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
for f in /.mp3; do echo mv -v "$f" "$(echo "$f" | sed -r 's!(.*/)...(.*).(.mp3)!123!')"; done i get -bash: !: event not found
â arlind
Apr 27 at 20:32
add a comment |Â
up vote
0
down vote
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 12345 135/'
After reviewing the output you can redirect it into a file and execute.
add a comment |Â
up vote
0
down vote
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 12345 135/'
After reviewing the output you can redirect it into a file and execute.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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 12345 135/'
After reviewing the output you can redirect it into a file and execute.
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 12345 135/'
After reviewing the output you can redirect it into a file and execute.
answered May 3 at 7:56
rudimeier
5,1111331
5,1111331
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441458%2fbash-remove-first-3-characters-and-last-1-character-from-all-mp3-files-in-all%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password