[SOLVED] bash script to safely and recursively delete files with no extension
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
bash script to safely and recursively delete files with no extension
I am slowly working my way through KN King's 'C Programming - a modern approach'. I use geany as an IDE, which creates an 'executable' file with no extension when I compile each source file e.g. heapsort.c would create a file called heapsort. I create many such files and it is a pain to track them down and remove them. I wish to keep the source files.
I have the following directory structure
Code:
/home/mik/C/knking/ch_[1-9]
So, seems like a job for a bash script. So, I have created clean.sh:
Code:
#!/bin/sh
find . -type f ! -name "*.*" -delete
which I have placed in the /home/mik/C/knking directory. The idea is it recursively deletes files with no extension in the knking directory and the chapter directories below... and nowhere else.
I am scared to run it 'as is' - I have visions of deleting crucial system files.
I have tested it without the -delete and it finds the correct files.
My questions are: is it safe to use from the knking directory? Is there a better/safer solution?
The . is a shortcut for current working directory. Use the absolute path and regardless of how the script is run only those files in the desired directory will be deleted. Keep a backup of the source code just in case. A regular user only has read execute permissions to system files so it is unlikely to delete them but it would be possible for other files in your home directory.
I use geany as an IDE, which creates an 'executable' file with no extension when I compile each source file e.g. heapsort.c would create a file called heapsort.
Have you checked whether that's configurable? (It is if you call the C compiler directly.)
It would be far safer to set a specific extension so you can target those specific files.
Quote:
I am scared to run it 'as is' - I have visions of deleting crucial system files.
Consider something like this:
Code:
#!/bin/bash
find /specific/path INSERT_FILTER_HERE -print
read -r -p 'Enter "YES" to delete the specified files'
if [ "$REPLY" == "YES" ]
then
find /specific/path INSERT_FILTER_HERE -delete
else
echo '(no action taken)'
fi
Obviously, the two find commands should be identical except for the final print/delete action.
If you're super paranoid, you could write a self-referencing check that grep the current file for "find" and if the commands differ, error and exit - but it would be simpler to configure your IDE or create a build script that names the files differently.
Have you checked whether that's configurable? (It is if you call the C compiler directly.)
It would be far safer to set a specific extension so you can target those specific files.
Consider something like this:
Code:
#!/bin/bash
find /specific/path INSERT_FILTER_HERE -print
read -r -p 'Enter "YES" to delete the specified files'
if [ "$REPLY" == "YES" ]
then
find /specific/path INSERT_FILTER_HERE -delete
else
echo '(no action taken)'
fi
Obviously, the two find commands should be identical except for the final print/delete action.
If you're super paranoid, you could write a self-referencing check that grep the current file for "find" and if the commands differ, error and exit - but it would be simpler to configure your IDE or create a build script that names the files differently.
There's no need to quote an entire post that's directly above your reply.
Arrays are certainly an option to reduce maintenance. I didn't use them previously because I was keeping it /bin/sh compatible (as amikoyan's original script was) - then when checking the code before posting, I discovered read's -p is Bash specific and since they had mentioned Bash already, I made the simplest change.
I mean not to delete anything outside of knking directory and its subdirectories. michaelk's suggestion of changing . to an absolute path should fix that possibility.
I mean not to delete anything outside of knking directory and its subdirectories. michaelk's suggestion of changing . to an absolute path should fix that possibility.
See #6 above. Run find first with -print and then once you are sure of the pattern with -delete or -delete -print.
echo > remove_script.sh
while read file; do
echo rm \"$file\" >> remove_script.sh;
done < <(find /home/mik/C/knking/ch_[1-9] -type f ! -name "*.*" -print)
chmod +x remove_script.sh
$file should be in quotes immediately and in the generated script,
the whole loop should be redirected to the output file, and
a pipe looks simpler here:
Code:
find /home/mik/C/knking/ch_[1-9] -type f ! -name "*.*" -print |
while IFS= read -r file; do
echo "rm '$file'"
done > remove_script.sh
chmod +x remove_script.sh
And the loop can be replaced by sed
Code:
find /home/mik/C/knking/ch_[1-9] -type f ! -name "*.*" -print |
sed "s/.*/rm '&'/" > remove_script.sh
chmod +x remove_script.sh
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.