Remove files with smallest filename suffixes

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question






















  • 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










  • @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














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.







share|improve this question






















  • 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










  • @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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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? 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










  • @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











  • 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










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





share|improve this answer




















  • Worked beautifully. Thanks very much.
    – user300457
    Nov 29 '17 at 20:59










  • @user300457, you're welcome
    – RomanPerekhrest
    Nov 29 '17 at 20:59

















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





share|improve this answer




















  • That was very quick.
    – user300457
    Nov 29 '17 at 20:57










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer




















  • Worked beautifully. Thanks very much.
    – user300457
    Nov 29 '17 at 20:59










  • @user300457, you're welcome
    – RomanPerekhrest
    Nov 29 '17 at 20:59














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





share|improve this answer




















  • Worked beautifully. Thanks very much.
    – user300457
    Nov 29 '17 at 20:59










  • @user300457, you're welcome
    – RomanPerekhrest
    Nov 29 '17 at 20:59












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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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












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





share|improve this answer




















  • That was very quick.
    – user300457
    Nov 29 '17 at 20:57














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





share|improve this answer




















  • That was very quick.
    – user300457
    Nov 29 '17 at 20:57












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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 29 '17 at 20:17









Stéphane Chazelas

282k53520854




282k53520854











  • 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




That was very quick.
– user300457
Nov 29 '17 at 20:57

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)