Remove files with smallest filename suffixes

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a directory of files with similar names, but with incrementing digits as a suffix. I want to remove the lower suffixed files and only keep the files with the highest suffix. Below is an example file listing:
1k_02.txt
1k_03.txt
1l_02.txt
1l_03.txt
1l_04.txt
2a_05.txt
2a_06.txt
4c_03.txt
4c_04.txt
The above list needs to be reduced to the files below:
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
I don't even know where to start with this, but if possible I would like a single bash command.
command-line files rm duplicate
add a comment |Â
up vote
0
down vote
favorite
I have a directory of files with similar names, but with incrementing digits as a suffix. I want to remove the lower suffixed files and only keep the files with the highest suffix. Below is an example file listing:
1k_02.txt
1k_03.txt
1l_02.txt
1l_03.txt
1l_04.txt
2a_05.txt
2a_06.txt
4c_03.txt
4c_04.txt
The above list needs to be reduced to the files below:
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
I don't even know where to start with this, but if possible I would like a single bash command.
command-line files rm duplicate
explain your removing logic, how files with duplicate suffixes should be processed? Why1k_03.txtshould be kept but4c_03.txtshould be removed?
â RomanPerekhrest
Nov 29 '17 at 20:07
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.zsh -c âÂÂrm ....âÂÂ
â Jeff Schaller
Nov 29 '17 at 20:09
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a directory of files with similar names, but with incrementing digits as a suffix. I want to remove the lower suffixed files and only keep the files with the highest suffix. Below is an example file listing:
1k_02.txt
1k_03.txt
1l_02.txt
1l_03.txt
1l_04.txt
2a_05.txt
2a_06.txt
4c_03.txt
4c_04.txt
The above list needs to be reduced to the files below:
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
I don't even know where to start with this, but if possible I would like a single bash command.
command-line files rm duplicate
I have a directory of files with similar names, but with incrementing digits as a suffix. I want to remove the lower suffixed files and only keep the files with the highest suffix. Below is an example file listing:
1k_02.txt
1k_03.txt
1l_02.txt
1l_03.txt
1l_04.txt
2a_05.txt
2a_06.txt
4c_03.txt
4c_04.txt
The above list needs to be reduced to the files below:
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
I don't even know where to start with this, but if possible I would like a single bash command.
command-line files rm duplicate
edited Nov 29 '17 at 20:44
Jeff Schaller
32.1k849109
32.1k849109
asked Nov 29 '17 at 19:59
user300457
31
31
explain your removing logic, how files with duplicate suffixes should be processed? Why1k_03.txtshould be kept but4c_03.txtshould be removed?
â RomanPerekhrest
Nov 29 '17 at 20:07
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.zsh -c âÂÂrm ....âÂÂ
â Jeff Schaller
Nov 29 '17 at 20:09
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43
add a comment |Â
explain your removing logic, how files with duplicate suffixes should be processed? Why1k_03.txtshould be kept but4c_03.txtshould be removed?
â RomanPerekhrest
Nov 29 '17 at 20:07
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.zsh -c âÂÂrm ....âÂÂ
â Jeff Schaller
Nov 29 '17 at 20:09
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43
explain your removing logic, how files with duplicate suffixes should be processed? Why
1k_03.txt should be kept but 4c_03.txt should be removed?â RomanPerekhrest
Nov 29 '17 at 20:07
explain your removing logic, how files with duplicate suffixes should be processed? Why
1k_03.txt should be kept but 4c_03.txt should be removed?â RomanPerekhrest
Nov 29 '17 at 20:07
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.
zsh -c âÂÂrm ....âÂÂâ Jeff Schaller
Nov 29 '17 at 20:09
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.
zsh -c âÂÂrm ....âÂÂâ Jeff Schaller
Nov 29 '17 at 20:09
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Complex pipeline:
Files list:
$ ls
1l_04.txt 2a_05.txt 4c_03.txt 1k_03.txt 1l_02.txt 4c_04.txt 2a_06.txt 1l_03.txt 1k_02.txt
printf "%sn" * | sort -t'_' -k1,1 -k2nr | awk -F'_' 'a[$1]++' | xargs rm
Results:
$ printf "%sn" *
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
add a comment |Â
up vote
1
down vote
With zsh:
$ ls
1k_02.txt 1l_02.txt 1l_04.txt 2a_06.txt 4c_04.txt
1k_03.txt 1l_03.txt 2a_05.txt 4c_03.txt
$ (typeset -A seen; for f (*_*(nOn)) ((seen[$f%_*]++)) && rm -- $f)
$ ls
1k_03.txt 1l_04.txt 2a_06.txt 4c_04.txt
That was very quick.
â user300457
Nov 29 '17 at 20:57
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
accepted
Complex pipeline:
Files list:
$ ls
1l_04.txt 2a_05.txt 4c_03.txt 1k_03.txt 1l_02.txt 4c_04.txt 2a_06.txt 1l_03.txt 1k_02.txt
printf "%sn" * | sort -t'_' -k1,1 -k2nr | awk -F'_' 'a[$1]++' | xargs rm
Results:
$ printf "%sn" *
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
add a comment |Â
up vote
0
down vote
accepted
Complex pipeline:
Files list:
$ ls
1l_04.txt 2a_05.txt 4c_03.txt 1k_03.txt 1l_02.txt 4c_04.txt 2a_06.txt 1l_03.txt 1k_02.txt
printf "%sn" * | sort -t'_' -k1,1 -k2nr | awk -F'_' 'a[$1]++' | xargs rm
Results:
$ printf "%sn" *
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Complex pipeline:
Files list:
$ ls
1l_04.txt 2a_05.txt 4c_03.txt 1k_03.txt 1l_02.txt 4c_04.txt 2a_06.txt 1l_03.txt 1k_02.txt
printf "%sn" * | sort -t'_' -k1,1 -k2nr | awk -F'_' 'a[$1]++' | xargs rm
Results:
$ printf "%sn" *
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
Complex pipeline:
Files list:
$ ls
1l_04.txt 2a_05.txt 4c_03.txt 1k_03.txt 1l_02.txt 4c_04.txt 2a_06.txt 1l_03.txt 1k_02.txt
printf "%sn" * | sort -t'_' -k1,1 -k2nr | awk -F'_' 'a[$1]++' | xargs rm
Results:
$ printf "%sn" *
1k_03.txt
1l_04.txt
2a_06.txt
4c_04.txt
answered Nov 29 '17 at 20:47
RomanPerekhrest
22.4k12145
22.4k12145
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
add a comment |Â
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
Worked beautifully. Thanks very much.
â user300457
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
@user300457, you're welcome
â RomanPerekhrest
Nov 29 '17 at 20:59
add a comment |Â
up vote
1
down vote
With zsh:
$ ls
1k_02.txt 1l_02.txt 1l_04.txt 2a_06.txt 4c_04.txt
1k_03.txt 1l_03.txt 2a_05.txt 4c_03.txt
$ (typeset -A seen; for f (*_*(nOn)) ((seen[$f%_*]++)) && rm -- $f)
$ ls
1k_03.txt 1l_04.txt 2a_06.txt 4c_04.txt
That was very quick.
â user300457
Nov 29 '17 at 20:57
add a comment |Â
up vote
1
down vote
With zsh:
$ ls
1k_02.txt 1l_02.txt 1l_04.txt 2a_06.txt 4c_04.txt
1k_03.txt 1l_03.txt 2a_05.txt 4c_03.txt
$ (typeset -A seen; for f (*_*(nOn)) ((seen[$f%_*]++)) && rm -- $f)
$ ls
1k_03.txt 1l_04.txt 2a_06.txt 4c_04.txt
That was very quick.
â user300457
Nov 29 '17 at 20:57
add a comment |Â
up vote
1
down vote
up vote
1
down vote
With zsh:
$ ls
1k_02.txt 1l_02.txt 1l_04.txt 2a_06.txt 4c_04.txt
1k_03.txt 1l_03.txt 2a_05.txt 4c_03.txt
$ (typeset -A seen; for f (*_*(nOn)) ((seen[$f%_*]++)) && rm -- $f)
$ ls
1k_03.txt 1l_04.txt 2a_06.txt 4c_04.txt
With zsh:
$ ls
1k_02.txt 1l_02.txt 1l_04.txt 2a_06.txt 4c_04.txt
1k_03.txt 1l_03.txt 2a_05.txt 4c_03.txt
$ (typeset -A seen; for f (*_*(nOn)) ((seen[$f%_*]++)) && rm -- $f)
$ ls
1k_03.txt 1l_04.txt 2a_06.txt 4c_04.txt
answered Nov 29 '17 at 20:17
Stéphane Chazelas
282k53520854
282k53520854
That was very quick.
â user300457
Nov 29 '17 at 20:57
add a comment |Â
That was very quick.
â user300457
Nov 29 '17 at 20:57
That was very quick.
â user300457
Nov 29 '17 at 20:57
That was very quick.
â user300457
Nov 29 '17 at 20:57
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%2f407812%2fremove-files-with-smallest-filename-suffixes%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
explain your removing logic, how files with duplicate suffixes should be processed? Why
1k_03.txtshould be kept but4c_03.txtshould be removed?â RomanPerekhrest
Nov 29 '17 at 20:07
ThereâÂÂs different prefixes for each.
â Jeff Schaller
Nov 29 '17 at 20:08
@user - are you opposed to a zsh-based command that would accomplish the goal? I donâÂÂt know one, but suspect there may be. e.g.
zsh -c âÂÂrm ....âÂÂâ Jeff Schaller
Nov 29 '17 at 20:09
@RomanPerekhrest - The suffix is a version number for the file; I want to keep the highest version of each file and remove the remainder, but not all files were incremented uniformly. Unfortunately the date stamps were all reset so all I have to go by are the file names.
â user300457
Nov 29 '17 at 20:42
@JeffSchaller - Anything goes, although I've never used zsh.
â user300457
Nov 29 '17 at 20:43