Cygwin: difference between '\rm -fr' and 'rm -fr' command? - linux

I have one shell script running on windows environment on cygwin environment. This script have one purging function which deletes certain folder on the system bases on certain condition.
I prepare the list of all the folder that I want to delete and then use following command:
rm -rfv $purge (where purge is the list of directories I want to delete)
Now when I tested this script, the directories are not getting deleted at all. First I thought there is some issue with by purge list, but on debugging I came to know that purge list is fine.
After lots of debugging and trials I just made small change in command:
\rm -rfv $purge
It just a kind of hit and trial and script starts working fine.
Now as far as I know \rm and rm -f both means forceful delete.
Now how can I justify this that why 'rm -f' what now working earlier but '\rm -f' did.
I want to know the basic difference between these two commands.

The rm can be (in theory) one of:
shell builtin command (however I don't know any shell with such builtin)
external command (most likely /bin/rm)
a shell function
an alias
If you put \ before it (or quote any part of it, for example "rm" or even 'r'm) shell will ignore all aliases (but not functions).
As jlliagre mentioned, you can ask shell what rm is and what is \rm using type builtin.
Experiment:
$ type rm
rm is /bin/rm
$ rm() { echo "FUNC"; command rm "$#"; }
$ type rm
rm is a function
$ alias rm='echo ALIAS; rm -i'
$ type rm
rm is aliased to `echo ALIAS; rm -i'
Now, we have alias rm, function rm and original external rm command:
Let's see how to call each other:
$ rm # this will call alias, calling function calling real rm
$ rm
ALIAS
FUNC
rm: missing operand
$ \rm # this will ignore alias, and call function calling real rm
FUNC
rm: missing operand
$ command rm # this will ignore any builtin, alias or function and call rm according to PATH
rm: missing operand
To understand it deeply, see help builtin, help command, help alias and man sh.

That means your rm command is aliased or a function. Backslashing it tells the shell to use the real rm command.
Edit: You can tell what rm refers to with the type command, eg:
$ type rm
rm is /bin/rm
.
$ type rm
rm is aliased to `rm -i'
.
$ type rm
rm is a function
...

Related

How to copy without getting prompt for overwrite (overwrite all) all hidden files, folders and subfolders from one folder to anotherin linux? [duplicate]

I'm trying to use the cp command and force an overwrite.
I have tried cp -rf /foo/* /bar, but I am still prompted to confirm each overwrite.
You can do yes | cp -rf xxx yyy, but my gutfeeling says that if you do it as root - your .bashrc or .profile has an alias of cp to cp -i, most modern systems (primarily RH-derivatives) do that to root profiles.
You can check existing aliases by running alias at the command prompt, or which cp to check aliases only for cp.
If you do have an alias defined, running unalias cp will abolish that for the current session, otherwise you can just remove it from your shell profile.
You can temporarily bypass an alias and use the non-aliased version of a command by prefixing it with \, e.g. \cp whatever
This is probably caused by cp being already aliased to something like cp -i. Calling cp directly should work:
/bin/cp -rf /zzz/zzz/* /xxx/xxx
Another way to get around this is to use the yes command:
yes | cp -rf /zzz/zzz/* /xxx/xxx
As some of the other answers have stated, you probably use an alias somewhere which maps cp to cp -i or something similar. You can run a command without any aliases by preceding it with a backslash. In your case, try
\cp -r /zzz/zzz/* /xxx/xxx
The backslash will temporarily disable any aliases you have called cp.
You probably have an alias somewhere, mapping cp to cp -i; because with the default settings, cp won't ask to overwrite. Check your .bashrc, your .profile etc.
See cp manpage: Only when -i parameter is specified will cp actually prompt before overwriting.
You can check this via the alias command:
$ alias
alias cp='cp -i'
alias diff='diff -u'
....
To undefine the alias, use:
$ unalias cp
As other answers have stated, this could happend if cp is an alias of cp -i.
You can append a \ before the cp command to use it without alias.
\cp -fR source target
So I run into this a lot because I keep cp aliased to cp -iv, and I found a neat trick. It turns out that while -i and -n both cancel previous overwrite directives, -f does not. However, if you use -nf it adds the ability to clear the -i. So:
cp -f /foo/* /bar <-- Prompt
cp -nf /foo/* /bar <-- No Prompt
Pretty neat huh? /necropost
By default cp has aliase to cp -i. You can check it, type alias and you can see some like:
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
To solve this problem just use /bin/cp /from /to command instead cp /from /to
The simplest way for me:
yes | cp source destination
you can use this command as well:
cp -ru /zzz/zzz/* /xxx/xxx
it would update your existing file with the newer one though.
cp is usually aliased like this
alias cp='cp -i' # i.e. ask questions of overwriting
if you are sure that you want to do the overwrite then use this:
/bin/cp <arguments here> src dest
I found this
'cp' -rf * /data/danalonso_testing/target/
Source: https://superuser.com/questions/358843/how-to-replace-all-the-contents-from-one-folder-with-another-one/358851
cp -u ...
cp --update ...
also works.
Another way to call the command without the alias is to use the command builtin in bash.
command cp -rf /zzz/zzz/*
-n is "not to overwrite" but his question is totally opposite what you replied for.
To avoid this confirmation you can simply run the cp command wiht absolute path, it will avoid the alias.
/bin/cp sourcefile destination
If you want to keep alias at the global level as is and just want to change for your script.
Just use:
alias cp=cp
and then write your follow up commands.
I simply used unalias to remove the "cp -i" alias, then do the copy, then set back the alias. :
unalias cp
cp -f foo foo.copy
alias cp="cp -i"
Not the most beautiful code, but easy to set and efficient. I also check the alias is already set back with a simple
alias |grep cp
If this is a small text file, you may consider this way too:
cat my.cnf > /etc/my.cnf
Not sure about the efficiency or side effects for large or binary files.
It is not cp -i. If you do not want to be asked for confirmation,
it is cp -n; for example:
cp -n src dest
Or in case of directories/folders is:
cp -nr src_dir dest_dir

ZSH+GREP+REGEX. Why this snippet act as rm -r /

This is a little anecdote from earlier on why not running root is vital.
I was sorting my home directory and deleted a few compressed files I had, I wrote
ls . | grep -P 'zip|tar|7z' | xargs rm and thought, hey I could also write this as rm -r $(ls . | grep -P '...') I suppose.
The second part I didn't mean to use it since there was nothing to delete, it was morelike a mental exercise, I wrote it next to the last command with a 'divider' to visually compare them.
ls . | grep -P 'zip|tar|7z' | xargs rm **//** rm -r $(ls . | grep -P '...')
Being **//** the "divider" and ... the mental "substitute" for 'zip|tar..'
I thought this wouldn't run but to my surprise, it acted as rm -r /and tried to delete everything, luckily permissions saved me and nothing was deleted.
But I'm curious why it'd work that way,
my guess is that rm **//** somehow translated to rm / but I'm not sure.
In the zsh shell, **//** would expand to all names under / as well to all names below the current directory (recursively).
From an empty directory on my system:
$ echo **//**
/altroot /bin /boot /bsd /bsd.booted /bsd.rd /bsd.sp /dev /etc /extra /home /mnt /root /sbin /sys /tmp /tmp_mnt /usr /var /vol
Why? Well, **/ matches all directories recursively under the current directory. More importantly, it matches the current directory, but since the current directory's name is not available inside the current directory, there's no entry returned for that.
However, when you add a / to that to create **//, then you get a lone / back for the current directory. Again in an empty directory:
$ echo **//
/
Then, if you add a further ** to make **//**, you pick up all names from the root directory, together with all names from the current directory and below (directory names from the current directory and below would occur twice in the list).
Your xargs is calling
rm **//** rm -r $(ls . | grep -P '...')
If you're using GNU rm, it will helpfully rearrange the command line so that it is interpreted the same as
rm -r **//** rm $(ls . | grep -P '...')
What this does should now be clear.
If you want to delete all regular files in the current directory that have filename suffixes .zip, .tar or .7z, use
rm ./*.(zip|tar|7z)(.)
in the zsh shell. If want to do that recursively down into subdirectories, use
rm ./**/*.(zip|tar|7z)(.)
The glob qualifier (.) makes the globbing pattern only match regular files. You could even restrict it to files above a certain size, say 10MB, with ./**/*.(zip|tar|7z)(.Lm+10).
One difference is that the ls ... | xargs .... solution also works if there are really a lot of files involved, while your rm $( .... ) might produce a argument list too long error. But if this is not an issue in your case, an even simpler attempt would be (assuming here Zsh; I don't understand why you tagged this bash, since you explicitly refer to Zsh only in your question)
rm *(zip|tar|7z)*(N)
which would express your original statement; I believe however that you really meant
rm -- *.(zip|tar|7z)(N)
because the solution you posted would also remove a file tarpit.txt, for instance. The (N) flag is a frail attempt to treat the case, that you don't have any file matching the pattern. Without the (N), you would get an error message from Zsh, and rm would receive the unexpanded file pattern, and, since it is unlikely that a file of this name exists, would output a second error message. By using (N), Zsh would simply pass nothing in this case (without complaining), and in fact rm would be invoked without arguments. Of course you would then get a rm: missing operand on stderr, and if you don't like this, you can filter this message.
UPDATES:
As Kusalananda has pointed out in his/her comment, omitting the (N) would, by default, make zsh only print an error message, if no files match the pattern, but not cause rm to be invoked.
Also added the -- flag to rm to allow removal of, i.e., a file called -rf.tar.

How to remove a file with special characterictics

Hi I just created a file by mistake, doing a tar actually, anyway the problem I have is that I can't remove that file. It is called --exclude-tag-under=hey.txt
I am trying to use rm -rf command but it doesn't do the trick. this is the output
[root]# rm -rf '--exclude-tag-under\=hey.txt'
rm: unrecognized option '--exclude-tag-under\=hey.txt'
Try 'rm --help' for more information.
the problem here is that the command rm is recognizing the file as a flag and thats a problem, I've tried also
rm -rf *hey.txt
but it doesnt work neither
I've also tried to change the name of the file but its the same problem
Prepend ./ like this: rm ./--exclude-tag-under\=hey.txt
When in doubt, check the man pages.
Running man rm will give you the rm man page, which, on Linux and OpenBSD (the ones I have tested) at least, will have a section saying:
To remove a file whose name starts with a '-', for example '-foo', use
one of these commands:
rm -- -foo
rm ./-foo
Use rm -- --exclude-tag-under=hey.txt
$ ls
--exclude-tag-under=hey.txt
test
$ rm -- --exclude-tag-under=hey.txt
$ ls
test

Ask Confirmation Before Delete files/directories

I'm facing many issues and lost many files because of rm -rf.
If I use rm -rfi xxx.xx, then it promote confirmation. But I forget/unable to give -i command when I'm using.
Is any other way to provide default -i or
I need like , Whenever I use rm , it must promote confirmation.
Any idea?
You can add an alias
alias rm="rm -i"
This ensures that each time you use rm it is replaced with rm -i so that each time it asks for a confirmation
If you are using a bash, you can add the alias to the .bashrc file so that you need not add the alias each time.
$ rm test.c
rm: remove regular file `test.c'? y
Make alias for rm command:
alias rm='rm -i'
Create alias for rm-command:
alias rm="rm -i"

Unix command deleted every directory even though not specified

I am very new to the unix. I ran the following command.
ls -l | xargs rm -rf bark.*
and above command removed every directory in the folder.
Can any one explained me why ?
The -r argument means "delete recursively" (ie descend into subdirectories). The -f command means "force" (in other words, don't ask for confirmation). -rf means "descend recursively into subdirectories without asking for confirmation"
ls -l lists all files in the directory. xargs takes the input from ls -l and appends it to the command you pass to xargs
The final command that got executed looked like this:
rm -rf bark.* <output of ls -l>
This essentially removed bark.* and all files in the current directory. Moral of the story: be very careful with rm -rf. (You can use rm -ri to ask before deleting files instead)
rm(1) deleted every file and directory in the current working directory because you asked it to.
To see roughly what happened, run this:
cd /etc ; ls -l | xargs echo
Pay careful attention to the output.
I strongly recommend using echo in place of rm -rf when constructing command lines. Only if the output looks fine should you then re-run the command with rm -rf. When in doubt, maybe just use rm -r so that you do not accidentally blow away too much. rm -ir if you are very skeptical of your command line. (I have been using Linux since 1994 and I still use this echo trick when constructing slightly complicated command lines to selectively delete a pile of files.)
Incidentally, I would avoid parsing ls(1) output in any fashion -- filenames can contain any character except ASCII NUL and / chars -- including newlines, tabs, and output that looks like ls -l output. Trying to parse this with tools such as xargs(1) can be dangerous.
Instead, use find(1) for these sorts of things. To delete all files in all directories named bark.*, I'd run a command like this:
find . -type d -name 'bark.*' -print0 | xargs -0 rm -r
Again, I'd use echo in place of rm -r for the first execution -- and if it looked fine, then I'd re-run with rm -r.
The ls -l command gave a list of all the subdirectories in your current present-working-directory (PWD).
The rm command can delete multiple files/directories if you pass them to it as a list.
eg: rm test1.txt test2.txt myApp will delete all three of the files with names:
test1.txt
test2.txt
myApp
Also, the flags for the rm command you used are common in many a folly.
rm -f - Force deletion of files without asking or confirming
rm -r - Recurse into all subdirectories and delete all their contents and subdirectories
So, let's say you are in /home/user, and the directory structure looks like so:
/home/user
|->dir1
|->dir2
`->file1.txt
the ls -l command will provide the list containing "dir1 dir2 file1.txt", and the result of the command ls -l | xargs rm -rf will look like this:
rm -rf dir1 dir2 file1.txt
If we expand your original question with the example above, the final command that gets passed to the system becomes:
rm -rf di1 dir2 file1.txt bark.*
So, everything in the current directory gets wiped out, so the bark.* is redundant (you effectively told the machine to destroy everything in the current directory anyway).
I think what you meant to do was delete all files in the current directory and all subdirectories (recurse) that start with bark. To do that, you just have to do:
find -iname bark.* | xargs rm
The command above means "find all files in this directory and subdirectories, ignoring UPPERCASE/lowercase/mIxEdCaSe, that start with the characters "bark.", and delete them". This could still be a bad command if you have a typo, so to be sure, you should always test before you do a batch-deletion like this.
In the future, first do the following to get a list of all the files you will be deleting first to confirm they are the ones you want deleted.
find -iname bark.* | xargs echo
Then if you are sure, delete them via
find -iname bark.* | xargs rm
Hope this helps.
As a humorous note, one of the most famous instances of "rm -rf" can be found here:
https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commit/a047be85247755cdbe0acce6f1dafc8beb84f2ac
An automated script runs something like rm -rf /usr/local/........., but due to accidentally inserting a space, the command became rm -rf /usr /local/......, so this effectively means "delete all root folders that start with usr or local", effectively destroying the system of anyone who uses it. I feel bad for that developer.
You can avoid these kinds of bugs by quoting your strings, ie:
rm -rf "/usr/ local/...." would have provided an error message and avoided this bug, because the quotes mean that everything between them is the full path, NOT a list of separate paths/files (ie: you are telling rm that the file/folder has a SPACE character in its name).

Resources