How do I delete files except some specific files passed as parameters?Remove all Vim undo files in all but...

What is the strongest case that can be made in favour of the UK regaining some control over fishing policy after Brexit?

Were there two appearances of Stan Lee?

Why is current rating for multicore cable lower than single core with the same cross section?

How to figure out whether the data is sample data or population data apart from the client's information?

Illegal assignment from SObject to Contact

Does a creature that is immune to a condition still make a saving throw?

Where did the extra Pym particles come from in Endgame?

Phrase for the opposite of "foolproof"

Modify locally tikzset

Python "triplet" dictionary?

Was it really necessary for the Lunar Module to have 2 stages?

Binary Numbers Magic Trick

What does "rf" mean in "rfkill"?

How to stop co-workers from teasing me because I know Russian?

Given what happens in Endgame, why doesn't Dormammu come back to attack the universe?

Airbnb - host wants to reduce rooms, can we get refund?

Pawn Sacrifice Justification

What is the range of this combined function?

Sci-fi novel series with instant travel between planets through gates. A river runs through the gates

Reverse the word in a string with the same order in javascript

Did Henry V’s archers at Agincourt fight with no pants / breeches on because of dysentery?

Asahi Dry Black beer can

You look catfish vs You look like a catfish

Why does Bran Stark feel that Jon Snow "needs to know" about his lineage?



How do I delete files except some specific files passed as parameters?


Remove all Vim undo files in all but one directoryHow to delete the files in one folder which are more than 60 days old in UNIX?find files in linux with some exceptionsDelete files older than X daysDelete multiple patterns of files using one command (find)Stop all subscripts if test failsRemove all log files except current log fileFind and delete files depending on the date the files were copiedBash : Shell script delete files matching all the passed arguments?Search for files whose pathnames contain several words without specific order between them






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'd tried but got stuck for hours with this assignment question. The task is to write a shell script with n+1 arguments where the first one is the directory and the rest is the specified files and will delete all but specified files.



E.g. calling rmexcept . '.jpg' '.png'



cd $1
for i in “${@:2}”
do
find . -type f -not -name $i -delete
done


Here is my attempt. However, it just only works with 1 specified files (e.g. rmexcept . '.jpg'). If there are more than 1 file (e.g. rmexcept . '.jpg' '.png'), all the files are deleted. I can't figure out what went wrong since I believe I have created a for loop.










share|improve this question









New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

    – Jeff Schaller
    1 hour ago











  • Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

    – bunnieatsnocarrot
    1 hour ago


















0















I'd tried but got stuck for hours with this assignment question. The task is to write a shell script with n+1 arguments where the first one is the directory and the rest is the specified files and will delete all but specified files.



E.g. calling rmexcept . '.jpg' '.png'



cd $1
for i in “${@:2}”
do
find . -type f -not -name $i -delete
done


Here is my attempt. However, it just only works with 1 specified files (e.g. rmexcept . '.jpg'). If there are more than 1 file (e.g. rmexcept . '.jpg' '.png'), all the files are deleted. I can't figure out what went wrong since I believe I have created a for loop.










share|improve this question









New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

    – Jeff Schaller
    1 hour ago











  • Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

    – bunnieatsnocarrot
    1 hour ago














0












0








0








I'd tried but got stuck for hours with this assignment question. The task is to write a shell script with n+1 arguments where the first one is the directory and the rest is the specified files and will delete all but specified files.



E.g. calling rmexcept . '.jpg' '.png'



cd $1
for i in “${@:2}”
do
find . -type f -not -name $i -delete
done


Here is my attempt. However, it just only works with 1 specified files (e.g. rmexcept . '.jpg'). If there are more than 1 file (e.g. rmexcept . '.jpg' '.png'), all the files are deleted. I can't figure out what went wrong since I believe I have created a for loop.










share|improve this question









New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'd tried but got stuck for hours with this assignment question. The task is to write a shell script with n+1 arguments where the first one is the directory and the rest is the specified files and will delete all but specified files.



E.g. calling rmexcept . '.jpg' '.png'



cd $1
for i in “${@:2}”
do
find . -type f -not -name $i -delete
done


Here is my attempt. However, it just only works with 1 specified files (e.g. rmexcept . '.jpg'). If there are more than 1 file (e.g. rmexcept . '.jpg' '.png'), all the files are deleted. I can't figure out what went wrong since I believe I have created a for loop.







shell-script find rm arguments parameter






share|improve this question









New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Jeff Schaller

45.3k1165148




45.3k1165148






New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









bunnieatsnocarrotbunnieatsnocarrot

1




1




New contributor




bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






bunnieatsnocarrot is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

    – Jeff Schaller
    1 hour ago











  • Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

    – bunnieatsnocarrot
    1 hour ago














  • 1





    Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

    – Jeff Schaller
    1 hour ago











  • Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

    – bunnieatsnocarrot
    1 hour ago








1




1





Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

– Jeff Schaller
1 hour ago





Of course, every png file is not a jpg file, so those disappear, then down the line. Since it's an assignment, what was taught recently, so that we might guide you in a productive direction?

– Jeff Schaller
1 hour ago













Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

– bunnieatsnocarrot
1 hour ago





Well, my recent lessons revolve around control structures (for, while, if, case, etc.), printf, expr, dirname and basename

– bunnieatsnocarrot
1 hour ago










0






active

oldest

votes












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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






bunnieatsnocarrot is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f516077%2fhow-do-i-delete-files-except-some-specific-files-passed-as-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








bunnieatsnocarrot is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















bunnieatsnocarrot is a new contributor. Be nice, and check out our Code of Conduct.













bunnieatsnocarrot is a new contributor. Be nice, and check out our Code of Conduct.












bunnieatsnocarrot is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f516077%2fhow-do-i-delete-files-except-some-specific-files-passed-as-parameters%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...