`rm -f` asks for confirmation, when aliased as `rm -i` - bash

I'm using this command to try and delete all Thumbs.db files in a very large folder. I thought -f should force deletion without asking for confirmation, but i'm still being prompted for "y" or "n" on every file.
find "megapacks" -name Thumbs.db -ok rm -f {} \;
I tried type rm to see if there was an alias and it responded with
rm is aliased to `rm -i'
I tried using /bin/rm instead but i'm still being prompted
find "megapacks" -name Thumbs.db -ok /bin/rm -f {} \;
Does anyone have another idea for how to avoid the confirmation?

Problem is with -ok option that is as per man find:
Like -exec but ask the user first. If the user agrees, run the command. Otherwise just return false.
This should work for you with -exec:
find "megapacks" -name Thumbs.db -exec /bin/rm -f {} \;
or faster:
find "megapacks" -name Thumbs.db -exec /bin/rm -f {} +

But I think the problem is that you pass -ok to find, which is
Like -exec but ask the user first.
If the alias were the problem, simply unset the alias:
unalias rm
Note that this only affect the current shell session.
You can also use the -delete option for find:
find "megapacks" -name Thumbs.db -delete

Related

Why does "find . -name somedir -exec rm -r {} \;" exit nonzero whenever it successfully deletes a directory?

I want to find a folder in a known folder but with unknown exact path, thus I must use find. When I find it, I use -exec to remove it, but I cannot evaluate if this has succeeded. Behavior somewhat confuses me; if I don't find the folder, I get return code 0:
>find . -name "testuuu" -exec rm -r {} \;
find: ‘./.docker’: Permission denied
>echo $?
0
But when I do find the folder and manage to delete it, it returns error code 1:
> find . -name "test" -exec rm -r {} \;
find: ‘./.docker’: Permission denied
find: ‘./test’: No such file or directory
> echo $?
1
Is this expected behavior? Why?
Further, how can I get return code of "rm" and not "find" and thus evaluate if the folder was deleted?
Use the -depth option to order a depth-first traversal -- that way find doesn't try to find things under your directory after the directory has already been deleted (an operation which, by nature, will always fail).
find . -depth -name "test" -exec rm -r -- '{}' \;
This option is also turned on by default when you use the -delete action in GNU find to delete content.
By the way -- if there are lots of test directories, you could get better performance by making that -exec rm -r -- {} + (which passes each copy of rm as many filenames as will fit on its command line) instead of -exec rm -r -- {} \; (which starts a new copy of rm for each test that is found), at the expense of no longer collecting individual error codes.

How to print the deleted file names along with path in shell script

I am deleting the files in all the directories and subdirectories using the command below:
find . -type f -name "*.txt" -exec rm -f {} \;
But I want to know which are the files deleted along with their paths. How can I do this?
Simply add a -print argument to your find.
$ find . -type f -name "*.txt" -print -exec rm -f {} \;
As noted by #JonathanRoss below, you can achieve an equivalent result with the -v option to rm.
It's not the scope of your question, but more generally it gets more interesting if you want to delete directories recursively. Then:
a simple -exec rm -r argument keeps it silent
a -print -exec rm -r argument reports the toplevel directories you're operating on
a -exec rm -rv argument reports all you're removing

Delete infinite nested directories

I found out on one of my servers an infinite succession of empty sub-directories.
I tried :
rm -Rf <dir>
Then
find . -name /<dir>/* -exec rm -f {} \;
And eventually
rsync -a --delete /emptydir/* /<dir>/*
But none of these worked out.
Does anyone have a better idea?
Thanks a lot.
PS : just so you know, I tried a "find" command with "mindepth" argument and I still have something after 8000 subdirs...
There can't be an indefinite loop of sub dirs if you ignore symlinks. I guess you are a victim of symlinks.
Therefore use the following command, it will only find directories not symlinks:
find -type d -exec rm -rf {} \;
You might want to delete directories depth-first:
find . -depth -type d -exec rm -rf {} \;
find doesn't follow symbolic links by default.

bash script rm cannot delete folder created by php mkdir

I cannot delete folder created by php mkdir
for I in `echo $*`
do
find $I -type f -name "sess_*" -exec rm -f {} \;
find $I -type f -name "*.bak" -exec rm -f {} \;
find $I -type f -name "Thumbs.db" -exec rm -f {} \;
find $I -type f -name "error.log" -exec sh -c 'echo -n > "{}"' -f {} \;
find $I -type f -path "*/cache/*" -name "*.*" -exec rm -f {} \;
find $I -path "*/uploads/*" -exec rm -rdf {} \;
done
I want to delete under /uploads/ all files and folders please help me thanks...
You should consider changing your find command to use the -o pragma to join your conditions together as the final exec is basically the same. This will avoid recursing the file system repeatedly.
The other answers address your concern about php mkdir. I'll just add that it has nothing to do with the fact it was created with php mkdir rather than any other code or command. It is due to the ownership and permissions.
I think this is most likely because php is running in apache or another http server under a different user than you are invoking the bash script. Or perhaps the files uploaded in uploads/ are owned by the http server's user and not the user invoking it.
Make sure that you run the bash script under the same user as your http server.
To find out which user owns which file do:
ls -l
If you run you bash script as root, you should be able to delete it anyway, but that is not recommended.
Update
To run it as root for nautilus script use the following as your nautilus script:
gksudo runmydeletescript
Then put all the other code into another file with the same path as whatever you have put for runmydeletescript and run chmod +x on it. This is extremely dangerous!
You should probably add -depth to the command to delete sub-directories of upload before the directory itself.
I worry about the -path but I'm not familiar with it.
Also consider using + instead of \; to reduce the number of commands executed.

Recursive erase hidden files

I'm trying to recursive erase all files that begin with "._" (aka mac dot files) on my server using SSH.
The files are listed with a ls -a but this won't work:
rm -rf ._*
I know there's a way. Mind to share?
Cheers!
find . -name ._\* -print0 | xargs -0 rm -f
find . -name ._\* -type f -delete
Specify that it's files and just call the find-delete on item directly.
find . -name ._\* -exec rm -f {} \;
by the way rm -rf is for removing directories recursively

Resources