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.
I wrote up a little script that I use to remove large amounts of files with. It's not much now but I was hoping to get some feedback on this thing. Maybe some ways to make it better?
#!/bin/bash
rm -rf \
<path to file you want removed> \
<path to file you want removed? \
One thing that is really a pain is that you actually have to paste in a list of the files you want removed and then append a "\" to the end of each one of them. I would really like to find a way to just paste in the files and run the script to remove the files, without all the work! Another thing that I thought about was adding "updatedb" to update slocates database after it clears all the files.
you don't need the backslash after every line, write the script the same as you would write it on the command line, you put a different command on a new line, if you want more than one command on a line then separate those commands with a semi-colon
Search google for tutorials to write bash scripts, there are millions lying around
I made a little script here that you can use if you want:
Here is from when I tried it out:
Code:
$ touch /tmp/file1
$ touch "/tmp/file with space"
$ mkdir "/tmp/directory1"
$ ./removal.sh
Usage: ./removal.sh [option] <removelist>
Try ./removal.sh -h for more info.
$ ./removal.sh -v
Removal Tool v0.1
$ ./removal.sh -h
Removal Tool usage:
-h, --help Show this
-v, --version Show version
-t, --trash Use trashcan, do not remove
-e, --empty Empty trashcan
$ cat removelist
/tmp/file1
/tmp/directory1
/tmp/file with space
$ ./removal.sh --trash removelist
Trashcan set to: /home/mezzymeat/trashcan
Moving "/tmp/file1" to trashcan ...
Moving "/tmp/directory1" to trashcan ...
Moving "/tmp/file with space" to trashcan ...
$ ls ~/trashcan/
directory1/ file with space file1
$ ./removal.sh -e
Cleaning up in /home/mezzymeat/trashcan ...
Done!
$ ls -l ~/trashcan/
total 0
And here is the code. It is simple and not optimized, so you can make a lot of improvements to it:
Code:
#!/bin/sh
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo "Usage: $0 [option] <removelist>"
echo "Try $0 -h for more info."
exit
fi;
if [ $1 == "-h" ] || [ $1 == "--help" ]; then
echo "Removal Tool usage:"
echo -e "\t-h, --help\t\tShow this"
echo -e "\t-v, --version\t\tShow version"
echo -e "\t-t, --trash\t\tUse trashcan, do not remove"
echo -e "\t-e, --empty\t\tEmpty trashcan"
echo ""
exit
fi;
if [ $1 == "-v" ] || [ $1 == "--version" ]; then
echo "Removal Tool v0.1"
exit
fi;
if [ $1 == "-e" ] || [ $1 == "--empty" ]; then
if [ -z $TRASHCAN ]; then
TRASHCAN="$HOME/trashcan"
fi;
if [ -d $TRASHCAN ]; then
if [ -w $TRASHCAN ]; then
echo "Cleaning up in $TRASHCAN ..."
rm -r $TRASHCAN/*
echo "Done!"
exit
else
echo "! You do not write permission for $TRASHCAN"
exit
fi;
else
echo "! $TRASHCAN does not exist."
exit
fi;
echo "Cleaning up in $TRASHCAN ..."
exit
fi;
trash=0
if [ $1 == "-t" ] || [ $1 == "--trash" ]; then
if [ -z $TRASHCAN ]; then
TRASHCAN="$HOME/trashcan"
fi;
if [ ! -d $TRASHCAN ]; then
echo "! $TRASHCAN does not exist."
exit
fi;
echo "Trashcan set to: $TRASHCAN"
trash=1
fi;
cat removelist | while read entry; do
if [ -w "$entry" ]; then
if [ $trash == 1 ]; then
echo -e "Moving \"$entry\" to trashcan ..."
mv "$entry" $TRASHCAN
else
echo -e "Removing \"$entry\" ..."
rm -r "$entry"
fi;
else
if [ -e "$entry" ]; then
echo "Cannot access: $entry"
else
echo "No such file or directory: $entry"
fi;
fi;
done;
It is very simple. I added the trashcan functionality and put the remove-list you wanted in a seperate file. Now you just need to have a file with a list of all files/dirs you want to remove and then run something like "removal -t <file>" and it should remove the files listed.
You can improve some things:
The switches (-h, -t, -e) works now but if you add some more and want to have two switches (eg you add -V for verbose and want to run "-t -V" or "-Vt" the script should be changed a little bit.
You may add so if the trashcan-dir does not exist the script asks the user if he wants it to be created.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.