Linux delete all but one directory

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 afterwards. But then any hardlink referring to that directory will be a distinct directory afterwards.

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 -mindepth 2 -delete worked great!

You need -mindepth 1 if you are specifying a directory ( find -mindepth 1 -delete ). Otherwise Johannes is right it will not delete the current working directory (when using find -delete ).

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.

Читайте также:  Kali linux перезагрузка сети

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.

Источник

Remove only files in directory on linux NOT directories [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Some files don’t have extensions so rm *.* wont work.

There are thousands of files in this folder.

10 Answers 10

find PATH -maxdepth 1 -type f -delete 

BUT this won’t prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they’re the correct files.

Читайте также:  Проверка работы сервиса linux

This is my favorite solution, since it is one command; the fact that it doesn’t invoke -exec is also a plus for me, since that requires knowledge of two distinct commands unnecessarily. IMO this should be the accepted answer.

on some versions this throws a warning, better turn the arguments around like: find /path/to/file -maxdepth 1 -type f

In 2020, I findally learned there is a -delete parameter — much simpler than -1 and a following xargs. Works on [this version of] OS X as well, as find is annoyingly different between some *IX distributions.

You can use find with -type f for files only and -maxdepth 1 so find won’t search for files in sub-directories of /path/to/directory . rm -i will prompt you on each delete so you can confirm or deny the delete. If you dont care about being asked for confirmation of each delete, change it to rm -fv ( -f for force the delete). The -v flag makes it so that with each delete, a message is printed saying what file was just deleted.

find /path/to/directory -maxdepth 1 -type f -exec rm -iv <> \; 

This should meet the criteria:

NOT directories
NOT subdirectories
NOT files in these subdirectories.

It’s worth mentioning that this will also remove files from all sub-directories, not only the current directory.

@staven This won’t work then, want only the files directly under the specified directory to be deleted..

I can never make exec do what I want, so I make the loop explicit: for r in $(find /path/to/directory -type f -maxdepth 1);do rm -v $r;done

Since this is high on google search, the simplest answer is:

where $directoryPath is the directory you want to empty. Credits should go to cbm3384 (that for some reason has gotten negative votes for this answer, why?)

If you do not want to confirm:

If you don’t believe try man rm or

mkdir -p 1/2/3; echo 'hello1' > 1/hello1.txt; echo 'hello2' > 1/2/hello2.txt;echo 'hello3' > 1/2/3/hello3.txt rm 1/2/* 

The above creates a directory structure, that has ‘helloX.txt’ in each folder (X is the directory level). rm 1/2/* deletes hello2.txt and leaves the other structure intact.

Also rm */*/* deletes only hello2.txt . It is the only that matches the pattern.

Just an example of a Makefile that cleans cakephp tmp-directory and leaves the directory structure intact:

clean: -rm -f tmp/* -rm -f tmp/*/* -rm -f tmp/*/*/* -rm -f tmp/*/*/*/* 

Minus in front of the rm means «do not halt on errors» (unremoved directory returns an error). If you want some level to be saved, just remove that line, e.g. second rm line removes logs.

Let me know if you have a system that does something else (BSD?).

EDIT: I tested this on ubuntu 12.04, osx lion and sourceforge.net shell. All behave like the explanation above.

Источник

How do I remove a directory and all its contents?

The following command will do it for you. Use caution though if this isn’t your intention as this also removes files in the directory and subdirectories.

Читайте также:  Чем изменить раздел линукс

«-f» is «—force» which overrides some sanity checks and prompting. A safer command to start with would be rm -r directoryname .

@JimParis I think the word «safer» is relative. Suppose you are writing a script to run on a remote computer. That script has a command which is supposed to remove a directory. Here, it would be «safer» to use rm -rf directoryname coz you wouldn’t want your script to pause execution, because it’s waiting for user input. Of course, you have to be sure that deleting the directory would do no harm.

if rm -rf directoryname fails you, try using rm -R -f directoryname , or rm —recursive -f directoryname .

If you are not having any luck with these, you should consider reinstalling rm or switching shells.

These were the options available on my rm man page, I looked it up by typing man rm to view my options on recursive deletion and the force options.

Does your rm man page list -r ? What does it do? (Try it in a directory that you create just for testing purposes, with only dummy files (and maybe subdirectories) in it.) What operating system are you using?

P.S. If rm -r doesn’t work, that would be an OS issue, not a shell issue. (Strictly speaking, it would be an issue with the version of rm that you’re using, so you could address it by installing a different version of rm , or searching your system to see whether you already have a different version of rm in some directory other than /bin .)

Ah, right. I forgot to mention I’m on Ubuntu 14.04 When I ran man rm in my terminal, it gave me a text file with the less text viewer. I scrolled found an indented entry with a whole that had the -R and —recursive options cozied up with the -r option, signifying that all of those arguments are identical.

edit: have you tried sudo rm -r directoryName ? The unwritten rules of the basic commands is that -r will allow a program to run recursively on every file your filesystem (starting where ever you choose!) and that -f will forcefully do things, even if it’s dangerous. ‘cd’, ‘mv’, ‘ls’ mostly holds this principle true. ls -r / is gonna be a duzie, and cp -rf / /dev/null will destroy everything on your filesystem.

Other answers show how to completely remove a directory’s content, but IMO they don’t address the literal question of the original post — that is, how can one delete subdirectories (as opposed to usual files). In other words, how can one delete empty directory structures while keeping subdirectories containing files ?

This can be achieved with find :

find directoryname -type d -delete 

This command will recursively search for directories ( -type d ) through directoryname and -delete them only if their subdirectories or themselves don’t contain any files.

Источник

Оцените статью
Adblock
detector