Ask Confirmation Before Delete files/directories - linux

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"

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

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

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

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
...

How to force cp to overwrite without confirmation

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

What is the safest way to empty a directory in *nix?

I'm scared that one day, I'm going to put a space or miss out something in the command I currently use:
rm -rf ./*
Is there a safer way of emptying the current directory's contents?
The safest way is to sit on your hands before pressing Enter.
That aside, you could create an alias like this one (for Bash)
alias rm="pwd;read;rm"
That will show you your directory, wait for an enter press and then remove what you specified with the proper flags. You can cancel by pressing ^C instead of Enter.
Here is a safer way: use ls first to list the files that will be affected, then use command-line history or history substitution to change the ls to rm and execute the command again after you are convinced the correct files will be operated on.
If you want to be really safe, you could create a simple alias or shell script like:
mv $1 ~/.recycle/
This would just move your stuff to a .recycle folder (hello, Windows!).
Then set up a cron job to do rm -rf on stuff in that folder that is older than a week.
I think this is a reasonable way:
find . -maxdepth 1 \! -name . -print0 | xargs -0 rm -rf
and it will also take care of hidden files and directories. The slash isn't required after the dot and this then will also eliminate the possible accident of typing . /.
Now if you are worried what it will delete, just change it into
find . -maxdepth 1 \! -name . -print | less
And look at the list. Now you can put it into a function:
function enum_files { find . -maxdepth 1 \! -name . "$#"; }
And now your remove is safe:
enum_files | less # view the files
enum_files -print0 | xargs -0 rm -rf # remove the files
If you are not in the habit of having embedded new-lines in filenames, you can omit the -print0 and -0 parameters. But i would use them, just in case :)
Go one level up and type in the directory name
rm -rf <dir>/*
I use one of:
rm -fr .
cd ..; rm -fr name-of-subdirectory
I'm seldom sufficiently attached to a directory that I want to get rid of the contents but must keep the directory itself.
When using rm -rf I almost always use the fully qualified path.
Use the trash command. In Debian/Ubuntu/etc., it can be installed from the package trash-cli. It works on both files and directories (since it's really moving the file, rather than immediately deleting it).
trash implements the freedesktop.org trash specification, compatible with the GNOME and KDE trash.
Files can be undeleted using restore-trash from the same package, or through the usual GUI.
You could always turn on -i which would prompt you on every file, but that would be really time consuming for large directories.
I always do a pwd first.
I'll even go as far as to create an alias so that it forces the prompt for my users. Red Hat does that by default, I think.
You could drop the `f' switch and it should prompt you for each file to make sure you really want to remove it.
If what you want to do is to blow away an entire directory there is always some level of danger associated with that operation. If you really want to be sure that you are doing the right thing you could always do a move operation to some place like /tmp, wait for some amount of time to make sure that everything is okay with the "deletion" in place. Then go into the /tmp directory and ONLY use relative paths for a forced and recursive remove operation. Additional, in the move do a rename to "delete-directoryname" to make it easier not to make a mistake.
For example I want to delete /opt/folder so I do:
mv /opt/folder /tmp/delete-folder
.... wait to be sure everything is okay - maybe a minute, maybe a week ....
cd /tmp
pwd
rm -rf delete-folder/
The most important tip for doing an rm -rf is to always use relative paths. This keeps you from ever having typed a / before having completed your typing.
There's a reason I have [tcsh]:
alias clean '\rm -i -- "#"* *~'
alias rmo 'rm -- *.o'
They were created the first time I accidentally put a space between the * and the .o. Suffice to say, what happened wasn't what I expected to happen...
But things could have been worse. Back in the early '90s, a friend of mine had a ~/etc directory. He wanted to delete it. Unfortunately he typed rm -rf /etc. Unfortunately, he was logged in as root. He had a bad day!
To be evil: touch -- '-rf *'
To be safe, use '--' and -i. Or get it right once and create an alias!
Here are the alias I am using on macOS. It would ask for every rm command before executing.
# ~/.profile
function saferm() {
echo rm "$#"
echo ""
read -p "* execute rm (y/n)? : " yesorno
if [ $yesorno == "y" ]; then
/bin/rm "$#"
fi
}
alias srm=saferm
alias rm=srm

Resources