can we exclude single file while doing rm -rf /opt/*
I want to exclude a file abc under /opt/ while doing rm -rf /opt/*
|
If you insist on running "rm -rf" on system directories you will destroy your system.
Find a better way. The "find" command would be a good start. |
Quote:
Can I exclude a single file from /test/* while doing rm -rf on it. where /test is not a system directory. |
Quote:
|
something like this.
Code:
while read f ; do |
If using a bash or a shell that supports extended globing you can use the following to exclude files. I don't know if it will work the same with -rf options and you might get an Argument list too long error.
Code:
shopt -s extglob Code:
find /directory_name ! -name 'file.txt' -type f -exec rm -f {} + |
Actually, if you want to delete a directory and all its sub-directories and all files including the hidden ones, a GUI file-manager works well.
It might stop you accidentally deleting the wrong directory tree... |
After accidentally removing all of the files in a user's home directory, I have taken to never using the -f option to rm any more. It's much less work to type 'y' and enter a few times than to recover a couple GB of files from backup.
That's just me, tho. YMMV. Short answer to the OP's question is No, you cannot execute an rm -rf /somedir/* and exclude a file within that tree. Others have already provided solutions. |
Maybe things in /opt were installed by package manager, if yes I would rather use the package manager and uninstall things I don't want anymore
|
@scasey : I sympathize, but when you have 1000s of files to remove .... & I do on servers occasionally
|
Quote:
|
Quote:
Still, if I want to just prune a defunct user's home to purge the 2GB of mail files he left behind, I'll definitely use rm -f... |
1. Construct a list of all sub-directories & files that rm -rf would destroy.
2. Check it carefully. 3. delete the line(s) containing vital files etc 4. Feed your list into rm. |
Quote:
Best is rm. If aliased, escape with \rm. And remember the echo command. First replace or prefix the \rm with echo! |
Take a backup first. :D
|
All times are GMT -5. The time now is 09:30 PM. |