Removing all files containing a string in their name
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
0
down vote
favorite
Is there command line tool that removes everything that whereis
returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai
it will also delete files that are named gmail
).
So recursively look through all directories and delete the contents of those that contain a string in their name.
e. g. If the directories are mail/gmail/sentmail
delete everything below gmail
.
command-line
add a comment |Â
up vote
0
down vote
favorite
Is there command line tool that removes everything that whereis
returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai
it will also delete files that are named gmail
).
So recursively look through all directories and delete the contents of those that contain a string in their name.
e. g. If the directories are mail/gmail/sentmail
delete everything below gmail
.
command-line
5
Whatwhereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
â Zanna
Feb 20 at 17:15
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
1
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is there command line tool that removes everything that whereis
returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai
it will also delete files that are named gmail
).
So recursively look through all directories and delete the contents of those that contain a string in their name.
e. g. If the directories are mail/gmail/sentmail
delete everything below gmail
.
command-line
Is there command line tool that removes everything that whereis
returns (even if they are directories) but works even with partial names (e. g. if I give the string gmai
it will also delete files that are named gmail
).
So recursively look through all directories and delete the contents of those that contain a string in their name.
e. g. If the directories are mail/gmail/sentmail
delete everything below gmail
.
command-line
command-line
edited Feb 22 at 6:50
![](https://i.stack.imgur.com/8CW8e.png?s=32&g=1)
![](https://i.stack.imgur.com/8CW8e.png?s=32&g=1)
Zanna
48.2k13120228
48.2k13120228
asked Feb 20 at 17:03
Nesa
1053
1053
5
Whatwhereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
â Zanna
Feb 20 at 17:15
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
1
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59
add a comment |Â
5
Whatwhereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at it
â Zanna
Feb 20 at 17:15
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
1
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59
5
5
What
whereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at itâ Zanna
Feb 20 at 17:15
What
whereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at itâ Zanna
Feb 20 at 17:15
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
1
1
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
If you really want to do this, you can use this find command, but you should test before without the switch -delete
to test if it's what's you expect :
find ./mail -depth -path '*gmai*' -delete
It's recursive in all sub-dirs
With the help of comment from @David Foerster and my original command
add a comment |Â
up vote
2
down vote
whereis(1)
doesn't sound suitable to your purpose since it's no general purpose path name search tool.
Without a search index
As Gilles noted in his answer you can use find(1)
for this job but I want to make a small yet significant improvement1:
find [PATH...] -depth -path '*gmai*' -print # -delete
Explanation:
find [PATH...]
goes through all files and subdirectories below eachPATH
or the current working directories, if no path was given, and prints all matches (by default).-path PATTERN
matches full paths againstPATTERN
using globbing if any, so*gmai*
matches all path names with an infixgmai
.-delete
deletes all matched paths (and overrides the default action to print them).-depth
instructsfind
to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.-print
prints all matched paths. Use this to check the result before the actual deletion. Comment in the-delete
command (by removing the#
in front of it) to actually delete them.
With a search index
If all the locations that you intend to delete appear in the mlocate.db(5)
search index you can use it for faster searches:
locate '/your/parent/path/*gmai*'
searches all (indexed) paths that start with /your/parent/path/
and have an infix gmai
in their remainder. This command only lists search results.
To delete the results use:
locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --
Explanation:
|
redirects the output of the left-side command to the input of the right-side command.xargs
collects "items" from its input, appends them to a given command and runs that command.-r
makesxargs
not run the command if no input item occurs.rm -rf
removes files and directories recursively-0
tellslocate
to delimit matches by null-bytes andxargs
to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.
1 that he incorporated in his answer before the question was reopened and allowed me to answer.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
If you really want to do this, you can use this find command, but you should test before without the switch -delete
to test if it's what's you expect :
find ./mail -depth -path '*gmai*' -delete
It's recursive in all sub-dirs
With the help of comment from @David Foerster and my original command
add a comment |Â
up vote
4
down vote
accepted
If you really want to do this, you can use this find command, but you should test before without the switch -delete
to test if it's what's you expect :
find ./mail -depth -path '*gmai*' -delete
It's recursive in all sub-dirs
With the help of comment from @David Foerster and my original command
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
If you really want to do this, you can use this find command, but you should test before without the switch -delete
to test if it's what's you expect :
find ./mail -depth -path '*gmai*' -delete
It's recursive in all sub-dirs
With the help of comment from @David Foerster and my original command
If you really want to do this, you can use this find command, but you should test before without the switch -delete
to test if it's what's you expect :
find ./mail -depth -path '*gmai*' -delete
It's recursive in all sub-dirs
With the help of comment from @David Foerster and my original command
edited Feb 21 at 22:22
answered Feb 20 at 23:47
Gilles Quenot
1,712188
1,712188
add a comment |Â
add a comment |Â
up vote
2
down vote
whereis(1)
doesn't sound suitable to your purpose since it's no general purpose path name search tool.
Without a search index
As Gilles noted in his answer you can use find(1)
for this job but I want to make a small yet significant improvement1:
find [PATH...] -depth -path '*gmai*' -print # -delete
Explanation:
find [PATH...]
goes through all files and subdirectories below eachPATH
or the current working directories, if no path was given, and prints all matches (by default).-path PATTERN
matches full paths againstPATTERN
using globbing if any, so*gmai*
matches all path names with an infixgmai
.-delete
deletes all matched paths (and overrides the default action to print them).-depth
instructsfind
to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.-print
prints all matched paths. Use this to check the result before the actual deletion. Comment in the-delete
command (by removing the#
in front of it) to actually delete them.
With a search index
If all the locations that you intend to delete appear in the mlocate.db(5)
search index you can use it for faster searches:
locate '/your/parent/path/*gmai*'
searches all (indexed) paths that start with /your/parent/path/
and have an infix gmai
in their remainder. This command only lists search results.
To delete the results use:
locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --
Explanation:
|
redirects the output of the left-side command to the input of the right-side command.xargs
collects "items" from its input, appends them to a given command and runs that command.-r
makesxargs
not run the command if no input item occurs.rm -rf
removes files and directories recursively-0
tellslocate
to delimit matches by null-bytes andxargs
to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.
1 that he incorporated in his answer before the question was reopened and allowed me to answer.
add a comment |Â
up vote
2
down vote
whereis(1)
doesn't sound suitable to your purpose since it's no general purpose path name search tool.
Without a search index
As Gilles noted in his answer you can use find(1)
for this job but I want to make a small yet significant improvement1:
find [PATH...] -depth -path '*gmai*' -print # -delete
Explanation:
find [PATH...]
goes through all files and subdirectories below eachPATH
or the current working directories, if no path was given, and prints all matches (by default).-path PATTERN
matches full paths againstPATTERN
using globbing if any, so*gmai*
matches all path names with an infixgmai
.-delete
deletes all matched paths (and overrides the default action to print them).-depth
instructsfind
to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.-print
prints all matched paths. Use this to check the result before the actual deletion. Comment in the-delete
command (by removing the#
in front of it) to actually delete them.
With a search index
If all the locations that you intend to delete appear in the mlocate.db(5)
search index you can use it for faster searches:
locate '/your/parent/path/*gmai*'
searches all (indexed) paths that start with /your/parent/path/
and have an infix gmai
in their remainder. This command only lists search results.
To delete the results use:
locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --
Explanation:
|
redirects the output of the left-side command to the input of the right-side command.xargs
collects "items" from its input, appends them to a given command and runs that command.-r
makesxargs
not run the command if no input item occurs.rm -rf
removes files and directories recursively-0
tellslocate
to delimit matches by null-bytes andxargs
to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.
1 that he incorporated in his answer before the question was reopened and allowed me to answer.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
whereis(1)
doesn't sound suitable to your purpose since it's no general purpose path name search tool.
Without a search index
As Gilles noted in his answer you can use find(1)
for this job but I want to make a small yet significant improvement1:
find [PATH...] -depth -path '*gmai*' -print # -delete
Explanation:
find [PATH...]
goes through all files and subdirectories below eachPATH
or the current working directories, if no path was given, and prints all matches (by default).-path PATTERN
matches full paths againstPATTERN
using globbing if any, so*gmai*
matches all path names with an infixgmai
.-delete
deletes all matched paths (and overrides the default action to print them).-depth
instructsfind
to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.-print
prints all matched paths. Use this to check the result before the actual deletion. Comment in the-delete
command (by removing the#
in front of it) to actually delete them.
With a search index
If all the locations that you intend to delete appear in the mlocate.db(5)
search index you can use it for faster searches:
locate '/your/parent/path/*gmai*'
searches all (indexed) paths that start with /your/parent/path/
and have an infix gmai
in their remainder. This command only lists search results.
To delete the results use:
locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --
Explanation:
|
redirects the output of the left-side command to the input of the right-side command.xargs
collects "items" from its input, appends them to a given command and runs that command.-r
makesxargs
not run the command if no input item occurs.rm -rf
removes files and directories recursively-0
tellslocate
to delimit matches by null-bytes andxargs
to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.
1 that he incorporated in his answer before the question was reopened and allowed me to answer.
whereis(1)
doesn't sound suitable to your purpose since it's no general purpose path name search tool.
Without a search index
As Gilles noted in his answer you can use find(1)
for this job but I want to make a small yet significant improvement1:
find [PATH...] -depth -path '*gmai*' -print # -delete
Explanation:
find [PATH...]
goes through all files and subdirectories below eachPATH
or the current working directories, if no path was given, and prints all matches (by default).-path PATTERN
matches full paths againstPATTERN
using globbing if any, so*gmai*
matches all path names with an infixgmai
.-delete
deletes all matched paths (and overrides the default action to print them).-depth
instructsfind
to match children before parents, i. e. directory entries before the parent directories themselves. It's necessary to delete children before their parents because in Unix' file system semantics only empty directories can be deleted.-print
prints all matched paths. Use this to check the result before the actual deletion. Comment in the-delete
command (by removing the#
in front of it) to actually delete them.
With a search index
If all the locations that you intend to delete appear in the mlocate.db(5)
search index you can use it for faster searches:
locate '/your/parent/path/*gmai*'
searches all (indexed) paths that start with /your/parent/path/
and have an infix gmai
in their remainder. This command only lists search results.
To delete the results use:
locate -0 '/your/parent/path/*gmai*' | xargs -r -0 -- rm -rf --
Explanation:
|
redirects the output of the left-side command to the input of the right-side command.xargs
collects "items" from its input, appends them to a given command and runs that command.-r
makesxargs
not run the command if no input item occurs.rm -rf
removes files and directories recursively-0
tellslocate
to delimit matches by null-bytes andxargs
to accept null-separated input items. This avoids issues with path names that contain white-space and, more specifically, line break characters.
1 that he incorporated in his answer before the question was reopened and allowed me to answer.
edited Feb 22 at 20:35
answered Feb 22 at 13:22
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
![](https://i.stack.imgur.com/E0SEH.png?s=32&g=1)
David Foerster
26.5k1362106
26.5k1362106
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2faskubuntu.com%2fquestions%2f1008103%2fremoving-all-files-containing-a-string-in-their-name%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
5
What
whereis
returns is very specific, and normally you wouldn't want to delete the files it lists. It's certainly possible to delete files based on all or part of their names, but I suggest you tell us exactly what you want to do rather than hint at itâ Zanna
Feb 20 at 17:15
@DavidFoerster I wanted what the answer is doing, except if I am doing this to delete, it might be faster not to look further inside directories that will be deleted
â Nesa
Feb 21 at 21:53
1
Unix file systems semantics don't allow the deletion of non-empty directories. Any tool that wants do successfully delete a directory needs to delete all of its children first.
â David Foerster
Feb 21 at 21:59