Using linux command line, what is the easiest way to delete matching files in all subdirectories of other...
My mom helped me cosign a car and now she wants to take it
Is it beneficial to use a crop sensor camera with a full frame telezoom?
What are the children of two Muggle-borns called?
Does "boire un jus" tend to mean "coffee" or "juice of fruit"?
Why should I allow multiple IP addresses on a website for a single session?
Could you fall off a planet if it was being accelerated by engines?
What is my external HDD doing?
Processes in a session in an interactive shell vs in a script
Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?
How to count the number of bytes in a file, grouping the same bytes?
Russian equivalents of 能骗就骗 (if you can cheat, then cheat)
Is it theoretically possible to hack printer using scanner tray?
Is it advisable to inform the CEO about his brother accessing his office?
Why do movie directors use brown tint on Mexico cities?
Where to connect the fuse and why?
How do I present a future free of gender stereotypes without being jarring or overpowering the narrative?
How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?
Is it OK to say "The situation is pregnant with a crisis"?
Finding MacCready
How can this fractal shape perfectly cover a certain platonic solid?
Understanding the as-if rule, "the program was executed as written"
Checkmate in 1 on a Tangled Board
Have any large aeroplanes been landed — safely and without damage — in locations that they could not be flown away from?
iMac 2019: Can I mix the old modules with the new ones when upgrading RAM?
Using linux command line, what is the easiest way to delete matching files in all subdirectories of other extensions?
How to remove all the files in a directory?Delete all but largest file in multiple directoriesHow do you get the first file in each subdirectory matching a list of file extensions?count lines matching string in each subdirectory and their subdirectoriesHow to find out two files and remove the common entries from second fileHow to Delete All the Files in the Current Directory Except the Last TwoHow can I run script for all directories?How to join text files based on a column and removing the first line in linux?how to substitute strings in a set of files with different strings?compare a certain number of lines between columns of two files
I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.
So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2
I want to delete all the ex1 files, but only if ex2 version exists first.
So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.
I want to use standard linux fu as I'm doing this on unraid.
I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.
Thanks!
Marshalleq
linux bash shell-script
New contributor
add a comment |
I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.
So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2
I want to delete all the ex1 files, but only if ex2 version exists first.
So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.
I want to use standard linux fu as I'm doing this on unraid.
I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.
Thanks!
Marshalleq
linux bash shell-script
New contributor
add a comment |
I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.
So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2
I want to delete all the ex1 files, but only if ex2 version exists first.
So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.
I want to use standard linux fu as I'm doing this on unraid.
I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.
Thanks!
Marshalleq
linux bash shell-script
New contributor
I've been browsing around and trying to find the answer - bonus points for doing this in the shortest number of characters.
So I have file1.ex1 file2.ex1 file1.ex2 but no file2.ex2
I want to delete all the ex1 files, but only if ex2 version exists first.
So I assume this means I need to get a directory listing of all files ending in the extension I want to keep, piping that through and searching for which ones match the first part of the filename, then piping that into a delete command.
I want to use standard linux fu as I'm doing this on unraid.
I know this is easy for most of you, which is why I added to do it in least number of characters for a challenge! Oh, and this should work in all subdirectories matching to whatever is in that subdirectory only.
Thanks!
Marshalleq
linux bash shell-script
linux bash shell-script
New contributor
New contributor
New contributor
asked 1 hour ago
MarshalleqMarshalleq
11 bronze badge
11 bronze badge
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;
This finds all regular files with suffix .ex1
recursively in the current directory and executes the shell script inside
of the single quotes for each file ($1
) found. It only prints the rm
command it would execute.
[ -f "${1%1}2" ] &&
tests if argument$1
exists with2
as last character.
${1%1}
removes the last1
and we replace it with2
. The[ -f ... ] &&
tests if the file exists and executes the following command if the condition is true.
You can remove the echo
and run the command again to really delete the files.
If you want to delete the files only in the current directory and not its subdirectories:
for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done
Again, you have to remove the echo
to execute rm
.
add a comment |
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
});
}
});
Marshalleq 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%2f527688%2fusing-linux-command-line-what-is-the-easiest-way-to-delete-matching-files-in-al%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;
This finds all regular files with suffix .ex1
recursively in the current directory and executes the shell script inside
of the single quotes for each file ($1
) found. It only prints the rm
command it would execute.
[ -f "${1%1}2" ] &&
tests if argument$1
exists with2
as last character.
${1%1}
removes the last1
and we replace it with2
. The[ -f ... ] &&
tests if the file exists and executes the following command if the condition is true.
You can remove the echo
and run the command again to really delete the files.
If you want to delete the files only in the current directory and not its subdirectories:
for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done
Again, you have to remove the echo
to execute rm
.
add a comment |
find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;
This finds all regular files with suffix .ex1
recursively in the current directory and executes the shell script inside
of the single quotes for each file ($1
) found. It only prints the rm
command it would execute.
[ -f "${1%1}2" ] &&
tests if argument$1
exists with2
as last character.
${1%1}
removes the last1
and we replace it with2
. The[ -f ... ] &&
tests if the file exists and executes the following command if the condition is true.
You can remove the echo
and run the command again to really delete the files.
If you want to delete the files only in the current directory and not its subdirectories:
for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done
Again, you have to remove the echo
to execute rm
.
add a comment |
find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;
This finds all regular files with suffix .ex1
recursively in the current directory and executes the shell script inside
of the single quotes for each file ($1
) found. It only prints the rm
command it would execute.
[ -f "${1%1}2" ] &&
tests if argument$1
exists with2
as last character.
${1%1}
removes the last1
and we replace it with2
. The[ -f ... ] &&
tests if the file exists and executes the following command if the condition is true.
You can remove the echo
and run the command again to really delete the files.
If you want to delete the files only in the current directory and not its subdirectories:
for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done
Again, you have to remove the echo
to execute rm
.
find . -name "*.ex1" -type f -exec sh -c '[ -f "${1%1}2" ] && echo rm "$1"' sh {} ;
This finds all regular files with suffix .ex1
recursively in the current directory and executes the shell script inside
of the single quotes for each file ($1
) found. It only prints the rm
command it would execute.
[ -f "${1%1}2" ] &&
tests if argument$1
exists with2
as last character.
${1%1}
removes the last1
and we replace it with2
. The[ -f ... ] &&
tests if the file exists and executes the following command if the condition is true.
You can remove the echo
and run the command again to really delete the files.
If you want to delete the files only in the current directory and not its subdirectories:
for i in *.ex1; do [ -f "${i%1}2" ] && echo rm "$i"; done
Again, you have to remove the echo
to execute rm
.
answered 20 mins ago
FreddyFreddy
5,4511 gold badge6 silver badges23 bronze badges
5,4511 gold badge6 silver badges23 bronze badges
add a comment |
add a comment |
Marshalleq is a new contributor. Be nice, and check out our Code of Conduct.
Marshalleq is a new contributor. Be nice, and check out our Code of Conduct.
Marshalleq is a new contributor. Be nice, and check out our Code of Conduct.
Marshalleq 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.
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%2f527688%2fusing-linux-command-line-what-is-the-easiest-way-to-delete-matching-files-in-al%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