Using rm -rf with a directory - bash

I tried to search on SO, but not able to find the difference between the following commands. if I have a directory named dir, how the below commands differ?
rm -rf dir/*
rm -rf dir/
rm -rf dir
Also how do the user permissions on the directory affect the outcome, if the id running the command is not the owner or not even in the group of the owner?
I am adding the command to do rm -rf in a shell script I am working on and need help in understanding the difference between the above commands.

rm -rf dir/*
Removes files within the directory (without removing the directory itself).
Note, hidden files won't be removed.
rm -rf dir/
Trailing slash indicates that dir is a directory. If it was a file, it wouldn't get removed. In your case this is identical to rm -rf dir, but in general it differs (see below)
rm -rf dir
In your case, identical to the one above.
In general, tools such as rm usually follow IEEE/OpenGroup standards when it comes to pathname resolution, which means that dir/ is equivalent to dir/.. One implication of that is that if diris a symlink to a directory rm -rf dir/ will remove the content of the directory (including the hidden files) but not the link or the directory itself, whereas rm -rf dir will only remove the symlink.
You need to have write permissions on a file or directory that you are removing, plus exec permissions on a directory that rm needs to traverse to remove files. You can read more about Unix filesystem permissions here.

Related

How to remove a directory and its descendents at once in shell?

I tried to delete a directory which contains other directories, but it shows "rmdir: failed to remove './will': Directory not empty"
To delete you need rm and "-rf" for recursive force
rm -rf yourdirectoryname

what is the diff between rm -rf $TIGER/${LION}/${RABBIT}/* and rm -rf $TIGER/${LION}/${RABBIT}?

I need to understand the diff between rm -rf $TIGER/${LION}/${RABBIT}/* and rm -rf $TIGER/${LION}/${RABBIT} so that putting this in script won't produce the disaster making it to delete everything it can from root in case that the variables are not set. what is the safe way to use rm -rf in csh/ksh?
Thanks for help !!
Either of those commands would create a disaster if the variables were all unset; they differ only in whether they delete the directory itself, or its non-hidden contents.
If you want to be safe against deleting recursively from the root directory, explicitly test for that case and cancel:
[[ $TIGER && $LION && $RABBIT ]] || {
echo "TIGER, LION and RABBIT must all be set; script exiting"
exit 1
}
rm -rf ...
This recursively deletes all non-hidden files inside the ${RABBIT} directory - ${RABBIT} directory is not deleted:
rm -rf $TIGER/${LION}/${RABBIT}/*
Note hidden files (aka dot files) have filenames beginning with .. These are not matched with typical * expansion unless shell dotglob option is set.
So to delete all files (including hidden files) you could use shopt thus:
shopt -s dotglob # turns shell option dotglob ON
rm -rf $TIGER/${LION}/${RABBIT}/* # Now deletes all (including hidden) files
shopt -u dotglob # FYI - unsets or turns dotglob OFF
This recursively deletes everything including the ${RABBIT} directory.
rm -rf $TIGER/${LION}/${RABBIT}
putting /* at the end will delete contents inside that directory
while only "/" will delete the directory itself as well as contents inside it.

Remove File on One Level of Directories only in KSH

I have a rm command which clears all the files in a particular directory.
#!/usr/bin/ksh
cd /asd/ded/ses/ddd/rty/leg/
rm *.sas7bdat
rm p_bt*
Unfortunately it clears all the files under this directory, but now I just want it to clear in "parent directory" i.e. "/asd/ded/ses/ddd/rty/leg/" and not in "/asd/ded/ses/ddd/rty/leg/21_11" which is the child directory inside it.
I know level rm is possible in bash. Does it change for KSH and if yes then how.
LonelySoul,
Chepner is correct. The default for 'rm' in ksh is to only remove the files in the current directory. You can remove files from the lower directories (recursively) by adding the '-r' option.
If you are observing different behavior, you may have an alias setup somewhere in your profile. Try entering 'whence rm' to see if there is an alias that is causing you unexpected behavior.
Examples.
>pwd
/tmp
>touch abc.txt
>mkdir ced
>touch ced/abc.txt
>rm abc.txt (will remove abc.txt in /tmp, but leave the file in directory ced.
>whence rm
rm -f

Remove symlink on Msys

I used msys to create a symlink. Now I wish to remove it, but here's what I get:
$ ln -s /mypath mylink
$ rm mylink
rm: cannot remove directory `mylink': Is a directory
What's the proper way to remove a symlink on msys?
Symlinks on MSys2 aren't real symlinks but copies. So you can actually use rm:
rm
-r, -R, --recursive remove directories and their contents recursively
Example:
rm -r mylink

bash delete files whose filename contain asterisks

How can one in bash delete files whose filename contain asterisks? I mean, using wildcards. If I do
rm -fr *filter*
I will delete all files in which the word "filter" appears in the filename, but what when the files do contain asterisk?
EDIT: Following your advice, I am not able to delete this
*filter*xyz*.data
rm -rf \*filter\* or rm -rf '*filter*'
rm -rf '*filter*'
Should work well. Use quotes (updated to single based on comment).

Resources