Remove files in a directory with ls grep and rm

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I've a bash file on my project root with this line
$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.
This is what I tried but not worked to removed files on the process_logs directory.
 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
N.B: I've also tried the command with simple regex pattern like
 ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.
grep ls rm
add a comment |Â
up vote
1
down vote
favorite
I've a bash file on my project root with this line
$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.
This is what I tried but not worked to removed files on the process_logs directory.
 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
N.B: I've also tried the command with simple regex pattern like
 ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.
grep ls rm
 
 
 
 
 
 
 Why not parse- ls?
 â Cyrus
 Apr 28 at 8:51
 
 
 
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've a bash file on my project root with this line
$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.
This is what I tried but not worked to removed files on the process_logs directory.
 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
N.B: I've also tried the command with simple regex pattern like
 ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.
grep ls rm
I've a bash file on my project root with this line
$ ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
The above line removes all the .txt files from the project root but when I push all the .txt files to another folder e.g process_logs/ and try the same commands with ls, grep and rm its doesn't work.
This is what I tried but not worked to removed files on the process_logs directory.
 $ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
N.B: I've also tried the command with simple regex pattern like
 ls process_logs/ | grep -P '^*.txt$' | xargs rm -f to remove files from directory but It doesn't work though.
grep ls rm
asked Apr 28 at 5:36
Being Sunny
1084
1084
 
 
 
 
 
 
 Why not parse- ls?
 â Cyrus
 Apr 28 at 8:51
 
 
 
add a comment |Â
 
 
 
 
 
 
 Why not parse- ls?
 â Cyrus
 Apr 28 at 8:51
 
 
 
Why not parse
ls?â Cyrus
Apr 28 at 8:51
Why not parse
ls?â Cyrus
Apr 28 at 8:51
add a comment |Â
 3 Answers
 3
 
active
oldest
votes
up vote
2
down vote
accepted
Try using find instead. If you don't want find to be recursive, you can use depth options:
find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete
Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.
 
 
 
 
 
 
 - findworked for me
 â Being Sunny
 May 1 at 7:05
 
 
 
add a comment |Â
up vote
1
down vote
I agree with using find is the better option. But I want to add why your command is not working.
Your command:
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.
Try
$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
Or
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"
 
 
 
 
 
 
 Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
 â Being Sunny
 Apr 28 at 12:32
 
 
 
add a comment |Â
up vote
0
down vote
If you have a simple shell filename globbing pattern, just use that.
E.g. (the * may be replaced by some more precise pattern),
rm -f process_logs/*.txt
or, if there are hundred of thousands of files, use xargs:
printf '%s' process_logs/*.txt | xargs -0 rm -f
If you absolutely need to use a regular expression, GNU find may do that with
find process_logs -maxdepth 1 -type f -regex '.txt$' -delete
Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.
add a comment |Â
 3 Answers
 3
 
active
oldest
votes
 3 Answers
 3
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Try using find instead. If you don't want find to be recursive, you can use depth options:
find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete
Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.
 
 
 
 
 
 
 - findworked for me
 â Being Sunny
 May 1 at 7:05
 
 
 
add a comment |Â
up vote
2
down vote
accepted
Try using find instead. If you don't want find to be recursive, you can use depth options:
find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete
Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.
 
 
 
 
 
 
 - findworked for me
 â Being Sunny
 May 1 at 7:05
 
 
 
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Try using find instead. If you don't want find to be recursive, you can use depth options:
find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete
Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.
Try using find instead. If you don't want find to be recursive, you can use depth options:
find /process_logs -maxdepth 1 -mindepth 1 -type f -name 'some_shell_glob_pattern_here' -delete
Parsing the output of ls is not recommended because ls is not always accurate because ls prints a human-readable version of the filename, which may not match the actual filename. For more info see the parsing ls wiki article.
answered Apr 28 at 5:49
jordanm
28.9k27690
28.9k27690
 
 
 
 
 
 
 - findworked for me
 â Being Sunny
 May 1 at 7:05
 
 
 
add a comment |Â
 
 
 
 
 
 
 - findworked for me
 â Being Sunny
 May 1 at 7:05
 
 
 
find worked for meâ Being Sunny
May 1 at 7:05
find worked for meâ Being Sunny
May 1 at 7:05
add a comment |Â
up vote
1
down vote
I agree with using find is the better option. But I want to add why your command is not working.
Your command:
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.
Try
$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
Or
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"
 
 
 
 
 
 
 Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
 â Being Sunny
 Apr 28 at 12:32
 
 
 
add a comment |Â
up vote
1
down vote
I agree with using find is the better option. But I want to add why your command is not working.
Your command:
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.
Try
$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
Or
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"
 
 
 
 
 
 
 Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
 â Being Sunny
 Apr 28 at 12:32
 
 
 
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I agree with using find is the better option. But I want to add why your command is not working.
Your command:
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.
Try
$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
Or
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"
I agree with using find is the better option. But I want to add why your command is not working.
Your command:
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
ls outputs filenames without the path. But you don't add the path to rm. So it will try to delete file names from your subdirectory in your current/ root directory.
Try
$ cd process_logs/; ls | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs rm -f
Or
$ ls process_logs/ | grep -P '^some_pattern_matching_regex_goeshere.txt$' | xargs -i rm -f "process_logs/"
answered Apr 28 at 6:18
RoVo
84219
84219
 
 
 
 
 
 
 Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
 â Being Sunny
 Apr 28 at 12:32
 
 
 
add a comment |Â
 
 
 
 
 
 
 Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
 â Being Sunny
 Apr 28 at 12:32
 
 
 
Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
â Being Sunny
Apr 28 at 12:32
Though I'll use find but your answer is very clear to me why my approach with ls doesn't work, thanks :)
â Being Sunny
Apr 28 at 12:32
add a comment |Â
up vote
0
down vote
If you have a simple shell filename globbing pattern, just use that.
E.g. (the * may be replaced by some more precise pattern),
rm -f process_logs/*.txt
or, if there are hundred of thousands of files, use xargs:
printf '%s' process_logs/*.txt | xargs -0 rm -f
If you absolutely need to use a regular expression, GNU find may do that with
find process_logs -maxdepth 1 -type f -regex '.txt$' -delete
Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.
add a comment |Â
up vote
0
down vote
If you have a simple shell filename globbing pattern, just use that.
E.g. (the * may be replaced by some more precise pattern),
rm -f process_logs/*.txt
or, if there are hundred of thousands of files, use xargs:
printf '%s' process_logs/*.txt | xargs -0 rm -f
If you absolutely need to use a regular expression, GNU find may do that with
find process_logs -maxdepth 1 -type f -regex '.txt$' -delete
Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you have a simple shell filename globbing pattern, just use that.
E.g. (the * may be replaced by some more precise pattern),
rm -f process_logs/*.txt
or, if there are hundred of thousands of files, use xargs:
printf '%s' process_logs/*.txt | xargs -0 rm -f
If you absolutely need to use a regular expression, GNU find may do that with
find process_logs -maxdepth 1 -type f -regex '.txt$' -delete
Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.
If you have a simple shell filename globbing pattern, just use that.
E.g. (the * may be replaced by some more precise pattern),
rm -f process_logs/*.txt
or, if there are hundred of thousands of files, use xargs:
printf '%s' process_logs/*.txt | xargs -0 rm -f
If you absolutely need to use a regular expression, GNU find may do that with
find process_logs -maxdepth 1 -type f -regex '.txt$' -delete
Note that the -regex predicate matches against the complete pathname, not just the filename portion at the end (which is why I anchored the expression to the end of the pathname with $). See also the -regextype option in the GNU find manual.
edited May 20 at 7:41
answered May 7 at 18:29


Kusalananda
102k13199315
102k13199315
add a comment |Â
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%2f440534%2fremove-files-in-a-directory-with-ls-grep-and-rm%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
Why not parse
ls?â Cyrus
Apr 28 at 8:51