Remove dir and all files linux

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.

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.

Читайте также:  Driver asus touchpad linux

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

Источник

How to Remove Files and Directories in Linux Command Line [Beginner’s Tutorial]

Learn how to delete files and remove directories with rm command in Linux.

How to delete a file in Linux? How to delete a directory in Linux? Let’s see how to do both of these tasks with one magical command called rm.

How to delete files in Linux

Let me show you various cases of removing files.

1. Delete a single file

If you want to remove a single file, simply use the rm command with the file name. You may need to add the path if the file is not in your current directory.

If the file is write protected i.e. you don’t have write permission to the file, you’ll be asked to confirm the deletion of the write-protected file.

rm: remove write-protected regular file 'file.txt'?

You can type yes or y and press enter key to confirm the deletion. Read this article to know more about Linux file permissions.

2. Force delete a file

If you want to remove files without any prompts (like the one you saw above), you can use the force removal option -f.

3. Remove multiple files

To remove multiple files at once, you can provide all the filenames.

rm file1.txt file2.txt file3.txt

You can also use wildcard (*) and regex instead of providing all the files individually to the rm command. For example, if you want to remove all the files ending in .hpp in the current directory, you can use rm command in the following way:

4. Remove files interactively

Of course, removing all the matching files at once could be a risky business. This is why rm command has the interactive mode. You can use the interactive mode with the option -i.

Читайте также:  Как правильно настроить линукс

It will ask for confirmation for each of the file. You can enter y to delete the file and n for skipping the deletion.

rm: remove regular file 'file1.txt'? y rm: remove regular file 'file2.txt'? n

You just learned to delete files in the terminal. Let’s see how to remove directories in Linux.

How to remove directories in Linux

There is a command called rmdir which is short for remove directory. However, this rmdir command can only be used for deleting empty directories.

If you try to delete a non-empty directory with rmdir, you’ll see an error message:

rmdir: failed to remove 'dir': Directory not empty

There is no rmdir force. You cannot force rmdir to delete non-empty directory.

This is why I am going to use the same rm command to delete folders as well. Remembering the rm command is a lot more useful than rmdir which in my opinion is not worth the trouble.

1. Remove an empty directory

To remove an empty directory, you can use the -d option. This is equivalent to the rmdir command and helps you ensure that the directory is empty before deleting it.

2. Remove directory with content

To remove directory with contents, you can use the recursive option with rm command.

This will delete all the contents of the directory including its sub-directories. If there are write-protected files and directories, you’ll be asked to confirm the deletion.

3. Force remove a directory and its content

If you want to avoid the confirmation prompt, you can force delete.

4. Remove multiple directories

You can also delete multiple directories at once with rm command.

Awesome! So now you know how to remove directory in Linux terminal.

Summary

Here’s a summary of the rm command and its usage for a quick reference.

Purpose Command
Delete a single file rm filename
Delete multiple files rm file1 file2 file3
Force remove files rm -f file1 file2 file3
Remove files interactively rm -i *.txt
Remove an empty directory rm -d dir
Remove a directory with its contents rm -r dir
Remove multiple directories rm -r dir1 dir 2 dir3

I hope you like this tutorial and learned to delete files and remove directories in Linux command line. If you have any questions or suggestions, please leave a comment below.

Источник

How to Remove Files and Directories Using Linux Command Line

This tutorial, will show you how to use the rm , unlink , and rmdir commands to remove files and directories in Linux.

How to Remove Files #

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.

The unlink command allows you to remove only a single file, while with rm , you can remove multiple files at once.

Be extra careful when removing files or directories, because once the file is deleted it cannot be easily recovered.

    To delete a single file, use the rm or unlink command followed by the file name:

If the file is write-protected, you will be prompted for confirmation, as shown below. To remove the file type y , and hit Enter . Otherwise, if the file is not write-protected, it will be deleted without prompting.

 rm: remove write-protected regular empty file 'filename'?
rm filename1 filename2 filename3

You can also use a wildcard ( * ) and regular expansions to match multiple files. For example, to remove all .pdf files in the current directory, use the following command:

How to Remove Directories (Folders) #

In Linux, you can remove/delete directories with the rmdir and rm .

Читайте также:  What is kernel image in linux

rmdir is a command-line utility for deleting empty directories, while with rm you can remove directories and their contents recursively.

    To remove an empty directory, use either rmdir or rm -d followed by the directory name:

rm -r dirname1 dirname2 dirname3

Conclusion #

By now you should have a good understanding of how to use the Linux rm , rmdir and unlink commands and you should be able to safely remove files and directories from the command line.

Feel free to leave a comment if you have any questions.

Источник

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