Remove dir linux with files

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 ?

Читайте также:  Linux copy all but one folder

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.

Источник

How to Remove (Delete) Directory in Linux

There are several different ways to remove directories in Linux systems. If you use a Desktop file manager such as Gnome’s Files or KDE’s Dolphin, then you can delete files and directories using the manager’s graphical user interface. But, if you are working on a headless server or want to remove multiple directories at once, your best option is to delete the directories (folders) from the command line.

In this article, we will explain how to delete directories in Linux using the rmdir , rm , and find commands.

Before You Begin #

When removing a directory using a desktop file manager, the directory is actually moved to the Trash and can be easily recovered.

Be extra careful when removing files or directories from the command line because once the directory is deleted using the commands explained in this article, it cannot be fully recovered.

On most Linux filesystems, deleting a directory requires write permission on the directory and its content. Otherwise, you will get “Operation not permitted” error.

Directory names with a space in them must be escaped with a backslash ( / ).

Removing Directories with rmdir #

rmdir is a command-line utility for deleting empty directories. It is useful when you want to delete a directory only if it is empty, without needing to check whether the directory is empty or not.

To delete a directory with rmdir , type the command followed by the name of the directory you want to remove. For example, to delete a directory named dir1 you would type:

If the directory is not empty, you will get the following error:

rmdir: failed to remove 'dir1': No such file or directory 

In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

Removing Directories with rm #

rm is a command-line utility for deleting files and directories. Unlike rmdir the rm command can delete both empty and non-empty directories.

By default, when used without any option rm does not remove directories. To delete an empty directory, use the -d ( —dir ) option and to delete a non-empty directory, and all of its contents use the -r ( —recursive or -R ) option.

For example to delete a directory named dir1 along with all of its contents you would type:

If a directory or a file within the directory is write-protected, you will be prompted to confirm the deletion. To remove a directory without being prompted, use the -f option:

To remove multiple directories at once, invoke the rm command, followed by the names of the directories separated by space. The command below will remove each listed directory and their contents:

The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file. If the directory contains a lot of files, this can be a little annoying, so you may consider using the -I option what will prompt you only once before proceeding with the deletion.

Читайте также:  Linux основе red hat

To remove the directory type y and hit Enter .

rm: remove 1 argument recursively? y 

You can also use regular expansions to match and delete multiple directories. For example, to remove all first-level directories in the current directory that ends with _bak , you would use the following command:

Using regular expansions when removing directories may be risky. It is recommended first to list the directories with the ls command so that you can see what directories will be deleted before running the rm command.

Removing Directories with find #

find is a command-line utility that allows you to search for files and directories based on a given expression and perform an action on each matched file or directory.

The most common scenario is to use the find command to delete directories based on a pattern. For example, to delete all directories that end with _cache in the current working directory, you would run:

find . -type d -name '*_cache' -exec rm -r <> +

Let’s analyze the command above:

  • /dir — recursively search in the current working directory ( . ).
  • -type d — restricts the search to directories.
  • -name ‘*_cache’ — search only directories that end with _cache
  • -exec — executes an external command with optional arguments, in this case, that is rm -r .
  • <> + — appends the found files to the end of the rm command.

Removing all empty directories #

To remove all empty directories in a directory tree you would run:

find /dir -type d -empty -delete

Here is an explanation for the options used:

  • /dir — recursively search in the /dir directory.
  • -type d — restricts the search to directories.
  • -empty — restricts the search only to empty directories.
  • -delete — deletes all found empty directories in the subtree. -delete can delete only empty directories.

Use the -delete option with extreme caution. The find command line is evaluated as an expression, and if you add the -delete option first, the command will delete everything below the starting points you specified.

Always test the command first without the -delete option and use -delete as the last option.

/bin/rm: Argument list too long #

This error message appears when you use the rm command to remove a directory that contains a huge number of files. This happens because the number of files is larger than the system limit on the size of the command line argument.

There are several different solutions to this problem. For example, you can cd to the directory and manually or using a loop to remove sub-directories one by one.

The easiest solution is first to delete all files within the directory with the find command and then delete the directory:

find /dir -type f -delete && rm -r /dir

Conclusion #

With rm and find you can delete directories based on different criteria fast and efficient.

Читайте также:  Kali linux 64 bit installer

Deleting directories is a simple and easy process, but you must be cautious not to delete important data.

If you have any questions or feedback, feel free to leave a comment.

Источник

How to remove files and directories quickly via terminal (bash shell) [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.

From a terminal window: When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders. If I have a directory nested with files and folders within folders with files and so on, is there a way to delete all the files and folders without all the strenuous command typing? If it makes a difference, I am using the Mac Bash shell from a terminal, not Microsoft DOS or Linux.

Just in case you wish to restore the files in future , don’t use «rm» for such cases . Use «rm-trash» : github.com/nateshmbhat/rm-trash

4 Answers 4

-r «recursive» -f «force» (suppress confirmation messages)

+1 and glad you added the «Be careful!» part. definitely a «Sawzall» command that can quickly turn a good day into a bad one.. if wielded carelessly.

@itsmatt: You know what they say. give someone a Sawzall, and suddenly every problem looks like hours of fun!

On a Mac? Do this instead: brew install trash then trash -rf some_dir This will move the unwanted directory into your trashbin instead of just vanishing Prestige-style into the ether. (source)

Would remove everything (folders & files) in the current directory.

But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.

Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir .

The other two options you should know are -i and -f . -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you’re absolutely sure you’re deleting the right thing. You can specify these with -r or not; it’s an independent setting.

And as usual, you can combine switches: rm -r -i is just rm -ri , and rm -r -f is rm -rf .

Also note that what you’re learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm ‘s syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.

Источник

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