- Removing all but 1 file and 1 directory in linux
- 3 Answers 3
- How to delete a non-empty directory in Terminal?
- 4 Answers 4
- Use the below command :
- In case user doesn’t have the permission to delete the folder:
- In Unix, how do you remove everything in the current directory and below it?
- 10 Answers 10
- Removing almost all directories and files in linux
- 3 Answers 3
Removing all but 1 file and 1 directory in linux
I want to delete all but 1 file and 1 directory from the directory I am currently in. How do I go about doing this? I have a directory which has three directories a, b, c and three files 1.php, 2.php, 3.php. I want to remove directories a and b, as well as files 1.php and 2.php only. I am looking for a solution to doing this in an easier manner. I am looking for a solution that could be used on a higher scale with more folders and files.
You can write it as an answer to your own question and mark it as correct. SU relies on questions getting answered that are marked as correct, and this would be the preferred way.
3 Answers 3
find directory -not \( -name temp -o -name 3.php -o -name c -prune \) -delete
A quick test case showed that it worked on this exact case, at least. If there are subdirs named e.g. c or such, I believe you might exclude them as well. It’s easy to get it to work in general cases, but to define a find command in all generality takes some testing.
Test without the «-delete» statement to see which files it matches.
PRESSTOP: find changes its behaviour when -delete is specified ( find warns about this and won’t go on when -prune is specified). You could always pipe output to xargs and rm , or write -execdir rm <> \; instead of -delete , though.
find directory -not \( -name directory -o -name 3.php -o -name c -prune \) -execdir rm -r <> \; find directory -not \( -name directory -o -name 3.php -o -name c -prune \) -print0 | xargs -0 rm -r
It’s always tricky business, and when the goal is to delete files, make extensive test cases. I have quite a few similar find s running nightly, and I can’t say I wasn’t nervous setting them free 🙂
How to delete a non-empty directory in Terminal?
I’m unable to remove a directory like «New Folder» using all the above detailed commands. It’s double worded. But I want to remove that directory. Any suggestions will be welcomed. T.Divakara, Bengaluru, India
Its the blank space in the file name, try using ‘quotes’ > rmdir ‘New Folder’ < then the folder disapers, or use escape characters for non-vissible characters.
4 Answers 4
Use the below command :
It deletes all files and folders contained in the lampp directory.
In case user doesn’t have the permission to delete the folder:
Add sudo at the beginning of the command :
Otherwise, without sudo you will be returned permission denied. And it’s a good practice to try not to use -f while deleting a directory:
Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:
sudo rm -r /path/to/folderName
FYI: you can use letters -f , -r , -v :
- -f = to ignore non-existent files, never prompt
- -r = to remove directories and their contents recursively
- -v = to explain what is being done
In my humble opinion, it’s a good practice never to add the «f» on first attempt. Its purpose is to ignore certain warning prompts that may be important, especially if you’ve accidentally done it on/from the wrong directory. In my opinion it’s good to try without the «f» first, then only if you are encountering a lot of warning prompts and you’re sure it’s OK to ignore them all, Ctrl+C out of it and repeat the command with the «f».
@thomasrutter . Agree. A file «xxx» owner: root and group: root can BE deleted with the -f switch; and without sudo. This is the message without -f: «rm: remove write-protected regular file ‘/home/william/.cache/netbeans/v08.01/tmp/xxx’? _». _Tread gently.
However, you need to be careful with a recursive command like this, as it’s easy to accidentally delete a lot more than you intended.
It is a good idea to always double-check which directory you’re in, and whether you typed the command correctly, before pressing Enter.
Safer version
Adding -i makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.
Many people suggest using -f (combining it into -Rf or -rf ), claiming that it gets rid of annoying prompts. However, in normal cases you don’t need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won’t be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f : if there are problems with your arguments, then you’ll notice. If you start getting too many prompts about files without write access, then you can try it with -f . Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you’re deleting to prevent these prompts in the first place.
In Unix, how do you remove everything in the current directory and below it?
But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?
10 Answers 10
Practice safe computing. Simply go up one level in the hierarchy and don’t use a wildcard expression:
The two dashes — tell rm that is not a command-line option, even when it begins with a dash.
Because you are specifically matching a named directory and are thus less likely to delete something that you don’t intend to delete.
doesn’t it delete the directory itself too? You have to do mkdir
hmm. i thought he wanted to delete everything in the current directory, but not the directory itself. how do we do that?
Will delete all files/directories below the current one.
If you want to do the same with another directory whose name you have, you can just name that
If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1 . Do it without the -delete to get a list of the things that will be removed.
I needed to delete all the files in sub-directories, but did not want to delete the sub-directories themselves. find
You need -mindepth 1 if you are specifying a directory ( find
I tried: find -mindepth 1 -delete but i got illegal option — m but it worked great when i removed the mindepth option find . -delete
and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.
- I can review the list of files to delete before hitting ENTER.
- The command history will not contain «rm -rf *» with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
- It has also become handy once or twice to answer «wait a second. which files did I just delete?». The file names are visible in the terminal scrollback buffer or the command history.
In fact, I like this so much that I’ve made it the default behavior for TAB with this line in .bashrc:
bind TAB:insert-completions
Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and .. . Use the wildcard globbing instead. But this can be risky.
A safer version IMO is to use:
(this prompts you for confirmation before deleting every file/directory.)
When doing things like this, I’ve found a quick ls -r . first lets you see what you are going to delete. Useful to give a quick idea that you aren’t going to delete the whole disk.
@Yen — because if you do it in the wrong place you can get disastrous results. Using a specific name in the wrong place can only go wrong if the same subdirectory happens to exist there.
It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot ( . ) means the current directory. be carefull not to do rm -rf .. since the double dot ( .. ) means the previous directory.
This being said, if you are like me and have multiple terminal windows open at the same time, you’d better be safe and use rm -ir . Lets look at the command arguments to understand why.
First, if you look at the rm command man page ( man rm under most Unix) you notice that –r means «remove the contents of directories recursively». So, doing rm -r . alone would delete everything in the current directory and everything bellow it.
In rm –rf . the added -f means «ignore nonexistent files, never prompt». That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that’s really what you want.
On the otherhand, in rm -ri . the -i that replaces the -f means «prompt before any removal». This means you’ll get a chance to say «oups! that’s not what I want» before rm goes happily delete all your files.
In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That’s why I now employ rm -ri now.
Removing almost all directories and files in linux
Quick question : I want to delete all but 1 file and 1 directory from the directory I am currently in. How do I do this? The case scenario : I have a directory which has three directories a b c and three files 1.php 2.php 3.php. I want to remove directories a,b and files 1.php and 2.php ONLY! I am having a hard time trying to do this. The solution should scale up, i.e. I don’t want to have to list all the files I do want to delete, only the ones that should stay. What do I do?
to delete a folder use rm -rf folder question is a bit unclear about how the structure looks like, perhaps post the output of ls -R
This question would be better on superuser.com or askubuntu.com, as it is not about programming. (I voted for migration.) If you actually want to write a program which does this, please edit your question accordingly.
@Fredrik : Thank you. The question is how do I delete all but one directory and one file from a directory i am currently in?
@Paulo : Oops! I was debating whether I post it here or on superuser. Then got carried away by similar questions asked here and also because stackoverflow allowed me to tag the post with Linux and Ubuntu tags 🙂
3 Answers 3
shopt -s extglob echo rm -r !(3.php|c)
$ mkdir -p x/a x/b x/c $ cd x $ touch .php $ ls -F 1.php 2.php 3.php a/ b/ c/ $ shopt -s extglob $ echo rm -r !(3.php|c) rm -r 1.php 2.php a b
Beat me to it. Setting shopt -s extglob makes it easy. Wonder why it isn’t on by default as it is in Kornshell?
Thanks Arafinwe. How does that solution scale up when I have 20 directories and 30 files, and I want to delete all but 1 directory. Its going to be tough to mention every single dir name and file name that way, and do the rm operation. Assume that I will need to recursively delete the directories.
For deleting all but one file in a general case, it gets more complicated.
Here is a solution in bash (or other shells, I did not check on which ones it works):
function deleteAllBut() < local pattern="^($1)" for p in "$" do pattern="$pattern|($p)" done pattern=$pattern\$ for f in * do [[ $f ~= $pattern ]] || echo $f done >
to list all local files but these two ones. (This will not delete hidden files, e.g. ones whose names start with a . .)
How does it work? It first builds a regular expression from the command line arguments (which beforehand were expanded by the shell), then iterates through all files in the current directory and echoes all ones that do not match the pattern.
Change the echo $f to rm -r $f to actually delete those files and directories.
The following is the original answer for the original question.
cd rmdir a b rm 1.php 2.php
This assumes your directories are empty. If they are not (and you want to remove the contents, too), use
instead of the second line above. ( -r stands for recursive.)
Of course, you then can combine the last two lines:
or, if you want to be tricky: