cutting a directory name with multiple instances of the same delimiter (Shell)
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have directories I want to rename,
for example: 10-00017_S9_L001_plasmidspades_careful
I want to rename them to just the 10-00017_S9_L001
part
I've tried to use the cut command echo "$filename" | cut -d'_' -f3
, however that leaves me with just the L001 part of the filename.
Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.
Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?
cut
New contributor
add a comment |
up vote
0
down vote
favorite
I have directories I want to rename,
for example: 10-00017_S9_L001_plasmidspades_careful
I want to rename them to just the 10-00017_S9_L001
part
I've tried to use the cut command echo "$filename" | cut -d'_' -f3
, however that leaves me with just the L001 part of the filename.
Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.
Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?
cut
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have directories I want to rename,
for example: 10-00017_S9_L001_plasmidspades_careful
I want to rename them to just the 10-00017_S9_L001
part
I've tried to use the cut command echo "$filename" | cut -d'_' -f3
, however that leaves me with just the L001 part of the filename.
Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.
Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?
cut
New contributor
I have directories I want to rename,
for example: 10-00017_S9_L001_plasmidspades_careful
I want to rename them to just the 10-00017_S9_L001
part
I've tried to use the cut command echo "$filename" | cut -d'_' -f3
, however that leaves me with just the L001 part of the filename.
Obviously I could just rename them all manually, but there are hundreds of directories and I have not got the time.
Is there a way to use the cut command to get everything before the specific instance of the delimiter, or indeed anything else I could use?
cut
cut
New contributor
New contributor
edited 2 days ago
Rui F Ribeiro
38.2k1475123
38.2k1475123
New contributor
asked 2 days ago
A. Murray
61
61
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
The best solution is to use rename
(Debian) or prename
(RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:
rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *
or a more lenient:
rename -n 's/(.+_.+_.+_.+)_.+/$1/' *
With -n
it just shows what it would do, remove (or better, replace by -v
) for actual execution.
Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.
In is Larry Wall'srename
. Some systems have a differentrename
.rename
issed
for file-names.
– ctrl-alt-delor
2 days ago
add a comment |
up vote
0
down vote
A rather low tech solution:
- Use
ls
orfind
to create a list of directory in a file - Use an editor you are familiar with to replace each line (that just contains
oldname
bymv oldname newname
) Execute the script:
. list_of_moves
(note leading dot-space)
add a comment |
up vote
0
down vote
Put all directories in one file and execute below command to get it renamed
awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh
Tested for above mentioned dirtectoy and it worked fine
echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The best solution is to use rename
(Debian) or prename
(RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:
rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *
or a more lenient:
rename -n 's/(.+_.+_.+_.+)_.+/$1/' *
With -n
it just shows what it would do, remove (or better, replace by -v
) for actual execution.
Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.
In is Larry Wall'srename
. Some systems have a differentrename
.rename
issed
for file-names.
– ctrl-alt-delor
2 days ago
add a comment |
up vote
1
down vote
The best solution is to use rename
(Debian) or prename
(RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:
rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *
or a more lenient:
rename -n 's/(.+_.+_.+_.+)_.+/$1/' *
With -n
it just shows what it would do, remove (or better, replace by -v
) for actual execution.
Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.
In is Larry Wall'srename
. Some systems have a differentrename
.rename
issed
for file-names.
– ctrl-alt-delor
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
The best solution is to use rename
(Debian) or prename
(RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:
rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *
or a more lenient:
rename -n 's/(.+_.+_.+_.+)_.+/$1/' *
With -n
it just shows what it would do, remove (or better, replace by -v
) for actual execution.
Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.
The best solution is to use rename
(Debian) or prename
(RedHat) (same command, different name)(these packages could have to be added from the standard distro repos). It uses regular expressions (Perl-style). For instance, trying to be a bit strict on the match:
rename -n 's/(d+_d+_Sd_Ld+)_.+/$1/' *
or a more lenient:
rename -n 's/(.+_.+_.+_.+)_.+/$1/' *
With -n
it just shows what it would do, remove (or better, replace by -v
) for actual execution.
Note the single quotes around the pattern to avoid bash substitution on backslashes and dollars.
edited 2 days ago
answered 2 days ago
xenoid
2,4491723
2,4491723
In is Larry Wall'srename
. Some systems have a differentrename
.rename
issed
for file-names.
– ctrl-alt-delor
2 days ago
add a comment |
In is Larry Wall'srename
. Some systems have a differentrename
.rename
issed
for file-names.
– ctrl-alt-delor
2 days ago
In is Larry Wall's
rename
. Some systems have a different rename
. rename
is sed
for file-names.– ctrl-alt-delor
2 days ago
In is Larry Wall's
rename
. Some systems have a different rename
. rename
is sed
for file-names.– ctrl-alt-delor
2 days ago
add a comment |
up vote
0
down vote
A rather low tech solution:
- Use
ls
orfind
to create a list of directory in a file - Use an editor you are familiar with to replace each line (that just contains
oldname
bymv oldname newname
) Execute the script:
. list_of_moves
(note leading dot-space)
add a comment |
up vote
0
down vote
A rather low tech solution:
- Use
ls
orfind
to create a list of directory in a file - Use an editor you are familiar with to replace each line (that just contains
oldname
bymv oldname newname
) Execute the script:
. list_of_moves
(note leading dot-space)
add a comment |
up vote
0
down vote
up vote
0
down vote
A rather low tech solution:
- Use
ls
orfind
to create a list of directory in a file - Use an editor you are familiar with to replace each line (that just contains
oldname
bymv oldname newname
) Execute the script:
. list_of_moves
(note leading dot-space)
A rather low tech solution:
- Use
ls
orfind
to create a list of directory in a file - Use an editor you are familiar with to replace each line (that just contains
oldname
bymv oldname newname
) Execute the script:
. list_of_moves
(note leading dot-space)
answered 2 days ago
xenoid
2,4491723
2,4491723
add a comment |
add a comment |
up vote
0
down vote
Put all directories in one file and execute below command to get it renamed
awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh
Tested for above mentioned dirtectoy and it worked fine
echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh
add a comment |
up vote
0
down vote
Put all directories in one file and execute below command to get it renamed
awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh
Tested for above mentioned dirtectoy and it worked fine
echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh
add a comment |
up vote
0
down vote
up vote
0
down vote
Put all directories in one file and execute below command to get it renamed
awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh
Tested for above mentioned dirtectoy and it worked fine
echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh
Put all directories in one file and execute below command to get it renamed
awk 'print "mv" " " $1 " " substr($1,1,16)' filename | sh
Tested for above mentioned dirtectoy and it worked fine
echo "10-00017_S9_L001_plasmidspades_careful" | awk 'print "mv" " " $1 " " substr($1,1,16)' | sh
answered 2 days ago
Praveen Kumar BS
1,055138
1,055138
add a comment |
add a comment |
A. Murray is a new contributor. Be nice, and check out our Code of Conduct.
A. Murray is a new contributor. Be nice, and check out our Code of Conduct.
A. Murray is a new contributor. Be nice, and check out our Code of Conduct.
A. Murray is a new contributor. Be nice, and check out our Code of Conduct.
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%2f482153%2fcutting-a-directory-name-with-multiple-instances-of-the-same-delimiter-shell%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