How can I strip out a numeric prefix from multiple filenames?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am new to Unix. I have >2000 files which are named like
123-FILENAME_TEXT_M101K_20150929.CSV
where 123
can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123-
should be removed from the example name)
I have tried mv **-FILENAME* FILENAME*
.rename
is not an available function
Any advice would be greatly appreciated.
rename mv
add a comment |Â
up vote
1
down vote
favorite
I am new to Unix. I have >2000 files which are named like
123-FILENAME_TEXT_M101K_20150929.CSV
where 123
can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123-
should be removed from the example name)
I have tried mv **-FILENAME* FILENAME*
.rename
is not an available function
Any advice would be greatly appreciated.
rename mv
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am new to Unix. I have >2000 files which are named like
123-FILENAME_TEXT_M101K_20150929.CSV
where 123
can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123-
should be removed from the example name)
I have tried mv **-FILENAME* FILENAME*
.rename
is not an available function
Any advice would be greatly appreciated.
rename mv
I am new to Unix. I have >2000 files which are named like
123-FILENAME_TEXT_M101K_20150929.CSV
where 123
can be any three or four-digit number. The files are all within the same directory. I would like a script that removes the prefixing number and the dash. (i.e. the leading 123-
should be removed from the example name)
I have tried mv **-FILENAME* FILENAME*
.rename
is not an available function
Any advice would be greatly appreciated.
rename mv
edited Jan 31 at 14:35
ilkkachu
49.8k674137
49.8k674137
asked Jan 31 at 14:22
Kim
84
84
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You could try something like this, it will remove everything before the first -
for file in *-*.CSV
do
newName="$file#*-"
mv -- "$file" "$newName"
done
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted$*
there in Bash; It's had a bunch of bugs with that)
â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash'svar=$*
, it's the absence of quotes that cause problems.
â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
add a comment |Â
up vote
0
down vote
I have tried by using combination of sed,awk and find . Tested and working fine
find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You could try something like this, it will remove everything before the first -
for file in *-*.CSV
do
newName="$file#*-"
mv -- "$file" "$newName"
done
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted$*
there in Bash; It's had a bunch of bugs with that)
â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash'svar=$*
, it's the absence of quotes that cause problems.
â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
add a comment |Â
up vote
4
down vote
accepted
You could try something like this, it will remove everything before the first -
for file in *-*.CSV
do
newName="$file#*-"
mv -- "$file" "$newName"
done
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted$*
there in Bash; It's had a bunch of bugs with that)
â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash'svar=$*
, it's the absence of quotes that cause problems.
â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You could try something like this, it will remove everything before the first -
for file in *-*.CSV
do
newName="$file#*-"
mv -- "$file" "$newName"
done
You could try something like this, it will remove everything before the first -
for file in *-*.CSV
do
newName="$file#*-"
mv -- "$file" "$newName"
done
edited Jan 31 at 14:51
Jeff Schaller
31.4k846105
31.4k846105
answered Jan 31 at 14:32
mmoisse
584
584
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted$*
there in Bash; It's had a bunch of bugs with that)
â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash'svar=$*
, it's the absence of quotes that cause problems.
â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
add a comment |Â
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted$*
there in Bash; It's had a bunch of bugs with that)
â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash'svar=$*
, it's the absence of quotes that cause problems.
â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
1
1
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted
$*
there in Bash; It's had a bunch of bugs with that)â ilkkachu
Jan 31 at 14:42
The assignment is actually one of the few places that don't need the quotes (unless you use an unquoted
$*
there in Bash; It's had a bunch of bugs with that)â ilkkachu
Jan 31 at 14:42
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
Quotes are indeed a source of bug, but I didn't know that it was not needed on assignment. Tx
â mmoisse
Jan 31 at 14:45
1
1
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's
var=$*
, it's the absence of quotes that cause problems.â Stéphane Chazelas
Jan 31 at 15:12
Quotes won't harm anyway. To clarify @ilkkachu's comment, in the case of bash's
var=$*
, it's the absence of quotes that cause problems.â Stéphane Chazelas
Jan 31 at 15:12
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Sorry I am a bit stuck, I have tried running this as a shell script but getting bad substitution - any ideas where I am going wrong?
â Kim
Jan 31 at 15:41
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
Solved, I was running as a .sh rather than .bash, even thou in my script I had #!/bin/bash. Any all work now tysm
â Kim
Jan 31 at 15:56
add a comment |Â
up vote
0
down vote
I have tried by using combination of sed,awk and find . Tested and working fine
find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh
add a comment |Â
up vote
0
down vote
I have tried by using combination of sed,awk and find . Tested and working fine
find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I have tried by using combination of sed,awk and find . Tested and working fine
find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh
I have tried by using combination of sed,awk and find . Tested and working fine
find . -type f -iname "*.CSV"| sed "s/^.///g"| sed -n '/^[0-9]4/p' | awk -F "-" 'print "mv" " " $0 " " $2'| sh
answered Jan 31 at 19:18
Praveen Kumar BS
1,010128
1,010128
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%2f420972%2fhow-can-i-strip-out-a-numeric-prefix-from-multiple-filenames%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