Rename initial string in filename
up vote
0
down vote
favorite
I need to dynamically remove first part of name and copy to other locations.
My file names are like
Voice_XYZZ_1_1801.pdf
Voice_XYZZ_1_1801.ndx
Electon_MNOPQ_1_1801.pdf
Electon_MNOPQ_1_1801.ndx
at run time either Voice can generate or Electon or both.
I need to remove the initial part from file names and they should look like
XYZZ_1_1801.pdf
XYZZ_1_1801.ndx
MNOPQ_1_1801.pdf
MNOPQ_1_1801.ndx
Which command I can use to achieve this?
shell
add a comment |
up vote
0
down vote
favorite
I need to dynamically remove first part of name and copy to other locations.
My file names are like
Voice_XYZZ_1_1801.pdf
Voice_XYZZ_1_1801.ndx
Electon_MNOPQ_1_1801.pdf
Electon_MNOPQ_1_1801.ndx
at run time either Voice can generate or Electon or both.
I need to remove the initial part from file names and they should look like
XYZZ_1_1801.pdf
XYZZ_1_1801.ndx
MNOPQ_1_1801.pdf
MNOPQ_1_1801.ndx
Which command I can use to achieve this?
shell
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to dynamically remove first part of name and copy to other locations.
My file names are like
Voice_XYZZ_1_1801.pdf
Voice_XYZZ_1_1801.ndx
Electon_MNOPQ_1_1801.pdf
Electon_MNOPQ_1_1801.ndx
at run time either Voice can generate or Electon or both.
I need to remove the initial part from file names and they should look like
XYZZ_1_1801.pdf
XYZZ_1_1801.ndx
MNOPQ_1_1801.pdf
MNOPQ_1_1801.ndx
Which command I can use to achieve this?
shell
I need to dynamically remove first part of name and copy to other locations.
My file names are like
Voice_XYZZ_1_1801.pdf
Voice_XYZZ_1_1801.ndx
Electon_MNOPQ_1_1801.pdf
Electon_MNOPQ_1_1801.ndx
at run time either Voice can generate or Electon or both.
I need to remove the initial part from file names and they should look like
XYZZ_1_1801.pdf
XYZZ_1_1801.ndx
MNOPQ_1_1801.pdf
MNOPQ_1_1801.ndx
Which command I can use to achieve this?
shell
shell
edited 2 days ago
Rui F Ribeiro
38.2k1475125
38.2k1475125
asked May 1 at 1:11
SHa
6
6
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f
using ${f#*_}
Ex.
$ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx
$ for f in Voice_* Electon_*; do echo "${f#*_}"; done
XYZZ_1_1801.ndx
XYZZ_1_1801.pdf
MNOPQ_1_1801.ndx
MNOPQ_1_1801.pdf
You can use this along with the mv
command to rename files e.g.
for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done
Copying to another location may be done similarly with the cp
command e.g.
for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something likestr=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename infind . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
add a comment |
up vote
0
down vote
This can be done very easily from the command-line by calling a Perl one-liner:
ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'
Here's an explanation for the code:
Send all the filenames that have '_' in their name to the next command
ls _ |
For each of the strings being passed in, do the command
perl -ne
Remove the newline from the filename (leftover from running 'ls' command)
chomp($_);
Remove the first part of the word, searching until you find a '_'
$_ =~ s/[^]*//;
Rename the file
rename $orig,$_;
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe addprint($_);
right beforechomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
– mdiehl13
May 2 at 15:22
which Perl are you using?perl -v
Also, try running just the perl command on a single file with no other commands:perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f
using ${f#*_}
Ex.
$ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx
$ for f in Voice_* Electon_*; do echo "${f#*_}"; done
XYZZ_1_1801.ndx
XYZZ_1_1801.pdf
MNOPQ_1_1801.ndx
MNOPQ_1_1801.pdf
You can use this along with the mv
command to rename files e.g.
for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done
Copying to another location may be done similarly with the cp
command e.g.
for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something likestr=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename infind . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
add a comment |
up vote
0
down vote
Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f
using ${f#*_}
Ex.
$ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx
$ for f in Voice_* Electon_*; do echo "${f#*_}"; done
XYZZ_1_1801.ndx
XYZZ_1_1801.pdf
MNOPQ_1_1801.ndx
MNOPQ_1_1801.pdf
You can use this along with the mv
command to rename files e.g.
for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done
Copying to another location may be done similarly with the cp
command e.g.
for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something likestr=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename infind . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
add a comment |
up vote
0
down vote
up vote
0
down vote
Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f
using ${f#*_}
Ex.
$ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx
$ for f in Voice_* Electon_*; do echo "${f#*_}"; done
XYZZ_1_1801.ndx
XYZZ_1_1801.pdf
MNOPQ_1_1801.ndx
MNOPQ_1_1801.pdf
You can use this along with the mv
command to rename files e.g.
for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done
Copying to another location may be done similarly with the cp
command e.g.
for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done
Assuming you're using a shell from the Bourne family, you can remove the shortest leading substring matching up to and including an underscore from a filename variable f
using ${f#*_}
Ex.
$ touch Voice_XYZZ_1_1801.pdf Voice_XYZZ_1_1801.ndx Electon_MNOPQ_1_1801.pdf Electon_MNOPQ_1_1801.ndx
$ for f in Voice_* Electon_*; do echo "${f#*_}"; done
XYZZ_1_1801.ndx
XYZZ_1_1801.pdf
MNOPQ_1_1801.ndx
MNOPQ_1_1801.pdf
You can use this along with the mv
command to rename files e.g.
for f in Voice_* Electon_*; do mv "$f" "${f#*_}"; done
Copying to another location may be done similarly with the cp
command e.g.
for f in Voice_* Electon_*; do cp "$f" "newdir/${f#*_}"; done
answered May 1 at 2:06
steeldriver
33.6k34982
33.6k34982
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something likestr=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename infind . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
add a comment |
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something likestr=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename infind . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?
– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
I ran command echo "{Voice_XYZZ_1_1801.pdf#*_}" ; and i am not getting expected result. All it showing me is full name of file in output.
– SHa
May 1 at 16:31
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like
str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
@SHa that's not how it works: the syntax applies to variable expansions rather than literal strings i.e. you would need to do something like
str=Voice_XYZZ_1_1801.pdf ; echo "${str#*_}"
– steeldriver
May 1 at 17:53
This is what I have done in script. for filename in
find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?– SHa
May 1 at 18:11
This is what I have done in script. for filename in
find . -iname '*.xml' -o -iname '*.ndx' -o -iname '*.pdf'
do Submission_Type = echo $filename | cut -d'' -f 1 ; FILE_NAME=$filename; FILE_NAME=echo "${FILE_NAME#*}"; mv "$FILE_NAME" "${FILE_NAME#*_}"; Is this is the right way?– SHa
May 1 at 18:11
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
The command work only for one file. if there are multiple files present then script fails while trying to change name of second file.
– SHa
May 1 at 18:17
add a comment |
up vote
0
down vote
This can be done very easily from the command-line by calling a Perl one-liner:
ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'
Here's an explanation for the code:
Send all the filenames that have '_' in their name to the next command
ls _ |
For each of the strings being passed in, do the command
perl -ne
Remove the newline from the filename (leftover from running 'ls' command)
chomp($_);
Remove the first part of the word, searching until you find a '_'
$_ =~ s/[^]*//;
Rename the file
rename $orig,$_;
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe addprint($_);
right beforechomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
– mdiehl13
May 2 at 15:22
which Perl are you using?perl -v
Also, try running just the perl command on a single file with no other commands:perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
add a comment |
up vote
0
down vote
This can be done very easily from the command-line by calling a Perl one-liner:
ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'
Here's an explanation for the code:
Send all the filenames that have '_' in their name to the next command
ls _ |
For each of the strings being passed in, do the command
perl -ne
Remove the newline from the filename (leftover from running 'ls' command)
chomp($_);
Remove the first part of the word, searching until you find a '_'
$_ =~ s/[^]*//;
Rename the file
rename $orig,$_;
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe addprint($_);
right beforechomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
– mdiehl13
May 2 at 15:22
which Perl are you using?perl -v
Also, try running just the perl command on a single file with no other commands:perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
add a comment |
up vote
0
down vote
up vote
0
down vote
This can be done very easily from the command-line by calling a Perl one-liner:
ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'
Here's an explanation for the code:
Send all the filenames that have '_' in their name to the next command
ls _ |
For each of the strings being passed in, do the command
perl -ne
Remove the newline from the filename (leftover from running 'ls' command)
chomp($_);
Remove the first part of the word, searching until you find a '_'
$_ =~ s/[^]*//;
Rename the file
rename $orig,$_;
This can be done very easily from the command-line by calling a Perl one-liner:
ls *_* | perl -ne 'chomp($_); $orig = $_; $_ =~ s/[^_]*_//; rename $orig,$_;'
Here's an explanation for the code:
Send all the filenames that have '_' in their name to the next command
ls _ |
For each of the strings being passed in, do the command
perl -ne
Remove the newline from the filename (leftover from running 'ls' command)
chomp($_);
Remove the first part of the word, searching until you find a '_'
$_ =~ s/[^]*//;
Rename the file
rename $orig,$_;
answered May 1 at 4:37
mdiehl13
1132
1132
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe addprint($_);
right beforechomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
– mdiehl13
May 2 at 15:22
which Perl are you using?perl -v
Also, try running just the perl command on a single file with no other commands:perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
add a comment |
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe addprint($_);
right beforechomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?
– mdiehl13
May 2 at 15:22
which Perl are you using?perl -v
Also, try running just the perl command on a single file with no other commands:perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
I am currently using KSH. Will it be fine to mix KSH and perl commands in same script?
– SHa
May 1 at 13:59
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
The perl command is not working in .ksh script. I tried multiple times, but name never changes.
– SHa
May 1 at 17:34
Hmm, I ran KSH and the command worked. Maybe add
print($_);
right before chomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?– mdiehl13
May 2 at 15:22
Hmm, I ran KSH and the command worked. Maybe add
print($_);
right before chomp($_)
and see if it prints out more than one file. Also, you are using single quotes, and not double quotes, around the Perl command, right?– mdiehl13
May 2 at 15:22
which Perl are you using?
perl -v
Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
which Perl are you using?
perl -v
Also, try running just the perl command on a single file with no other commands: perl -e 'rename "Voice_XYZZ.pdf","XYZZ.pdf";'
– mdiehl13
May 2 at 15:24
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441024%2frename-initial-string-in-filename%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown