Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
WARNING: I'd advise testing this for yourself before using this, there is some inherent danger whenever using rm, I won't take any responsibility if you use it wrongly and deleted unintentional things.
Last edited by r3sistance; 02-09-2017 at 10:15 AM.
Reason: warning and better example
WARNING: I'd advise testing this for yourself before using this, there is some inherent danger whenever using rm, I won't take any responsibility if you use it wrongly and deleted unintentional things.
Possibly. I only understand the first part of the command. What does the awk and xargs do?
Cheers
You can also use find with -exec. The following find files containing the string "bash" anywhere and then prints the first line of said file. You can modify it to delete the file, if that is your goal.
Code:
find . -maxdepth 1 -type f -exec grep -q bash {} \; -exec head -n 1 {} \;
The option -exec passes the exit code from the executed program(s) back to find where it is used within the expression.
awk is an extremely powerful tool, as the man page says: gawk - pattern scanning and processing language
in this example I am using awk to split the text using a delimiter of a colon and printing the first output parameter which in this case would be test/test1 for test, in other words formatting it ready to pass to xargs
xargs is used for running a command repeatedly against the passed paramters or as the man page states: xargs - build and execute command lines from standard input
the -r here is to stop it running if it receives no input, since we have an rm it is very prudent we don't let it run randomly. The -L 1 specifies that the parameters are split into single lines (rather than delimited by space, tabs, etc). following this is the command to run, in this case it is "rm -f". So it will delete any hits, since it lacks the recursive flag then we aren't in danger of deleting entire directories but rm is still dangerous.
iirc recursive greps don't work on some *nix distros, so the find example given by Turbocapitalist is going to be more portable.
Last edited by r3sistance; 02-09-2017 at 10:25 AM.
You can also use find with -exec. The following find files containing the string "bash" anywhere and then prints the first line of said file. You can modify it to delete the file, if that is your goal.
Code:
find . -maxdepth 1 -type f -exec grep -q bash {} \; -exec head -n 1 {} \;
The option -exec passes the exit code from the executed program(s) back to find where it is used within the expression.
Yes I do want to delete the file that contains that text. So would the code below be valid?
for i in /path/to/folder/*; do
if grep pattern "$i"; then
rm "$i";
fi
done
In real life i would make sure "$i" is a file and do something in case rm fails (rm "$i" || do_something_bout_the_failure ).
Ups: and probably make path/to/folder and pattern a variable.
Grep has the -l option for for listing filenames containing matches, -Z for outputting zero byte delimited filenames suitable for xargs and -r for recursive searching.
I tend to take a more batch processing POV. Because you can audit your work. And start or stop the process at any stage. But it's certainly not a method for unsupervised automation.
$ egrep -r -i entrop12 /home/time/Documents/p5e/status/ | tee pre_delete_log_of_files.txt
(find the suspected files targeted for deletion)
$ nano pre_delete_log_of_files.txt
(manually edit the file in case there's things you should keep like .bash_history since you're searching for it)
$ cat pre_delete_log_of_files.txt | while read $FILE; do echo $FILE; rm $FILE; done
(do the deletes)
$ cat pre_delete_log_of_files.txt | while read FILE; do if [ -r $FILE ]; then echo $FILE" --- not deleted "; fi; done
(and verify that what you told it to do happened)
Optionally delete the temp file, depending on your needs. Or move the files to another location, instead of delete if you might want to "UNDO" the process someday. Which is when the temp file is useful, to know what to "UNDO".
$ cat pre_delete_log_of_files.txt | while read $FILE; do echo $FILE; rm $FILE; done
(do the deletes)
$ cat pre_delete_log_of_files.txt | while read FILE; do if [ -r $FILE ]; then echo $FILE" --- not deleted "; fi; done
(and verify that what you told it to do happened)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.