Linux delete directory with all 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.

Читайте также:  What is linux device model

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.

Источник

Linux delete directory with all files

NAME

rm - remove files or directories

SYNOPSIS

DESCRIPTION

This manual page documents the GNU version of rm. rm removes each specified file. By default, it does not remove directories. If the -I or --interactive=once option is given, and there are more than three files or the -r, -R, or --recursive are given, then rm prompts the user for whether to proceed with the entire operation. If the response is not affirmative, the entire command is aborted. Otherwise, if a file is unwritable, standard input is a terminal, and the -f or --force option is not given, or the -i or --interactive=always option is given, rm prompts the user for whether to remove the file. If the response is not affirmative, the file is skipped.

OPTIONS

Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i). Without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred.

AUTHOR

Written by Paul Rubin, David MacKenzie, Richard M. Stallman, and Jim Meyering.

REPORTING BUGS

Report rm bugs to bug-coreutils@gnu.org GNU coreutils home page: http://www.gnu.org/software/coreutils/> General help using GNU software: http://www.gnu.org/gethelp/> Report rm translation bugs to http://translationproject.org/team/>
Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

unlink(1), unlink(2), chattr(1), shred(1) The full documentation for rm is maintained as a Texinfo manual. If the info and rm programs are properly installed at your site, the command info coreutils 'rm invocation' should give you access to the complete manual.

© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.

Читайте также:  After effects аналоги linux

Источник

How to Remove a Directory in Linux

Removing a directory in Linux is a pretty simple task if you are using the GUI. However, if you don’t have access to the GUI, you can also remove directories using terminal commands.

In this tutorial, we will show you how to remove a directory in Linux via commands in the terminal window or command line.

How to remove directories in Linux

  • A system running a Linux distribution.
  • An account with sudo privileges.
  • Access to the terminal window or command line.

Note: For other Linux directory management articles, see How to Move Directories in Linux and How to Rename a Directory in Linux.

How to Remove a Directory in Linux?

There are two Linux commands you can use to remove a directory from the terminal window or command line:

  • The rm command removes complete directories, including subdirectories and files.
  • The rmdir command removes empty directories.

It is important to note that the rm and rmdir commands permanently remove directories without moving them to the Trash directory. This means that you cannot restore a directory removed using these commands.

Note: Even though rm and rmdir permanently remove files and directories, users with enough skill and time still have a chance of restoring some of the removed files. If you want to learn more about removing files permanently, have a look at our shred command tutorial.

rm Command

The rm command in Linux removes files and directories.

It uses the following syntax:

rm [options] [file or directory name]

Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces.

The different rm command options include:

  • — f : Forces the removal of all files or directories.
  • -i : Prompts for confirmation before removing.
  • -I : Prompts once before removing more than three files or when removing recursively.
  • -r : Removes directories and their content recursively.
  • -d : Removes empty directories.
  • -v : Provides a verbose output.
  • —help : Displays the help text.
  • —version : Displays the command version.

Trying to use the rm command without any options to remove a directory results in an error message:

Читайте также:  Ksc установка kes linux

An error message displays when you try to remove a directory using the rm command without options

If you want to remove an empty directory, add the -d flag to the rm command:

Note: If you want to remove a directory whose name starts with a hyphen (), use the rm — [directory name] or rm ./[directory name] syntax.

The example below shows that the rm command with the — d flag removes the Example directory:

Removing an empty directory using the rm command

Use the -r flag to delete a directory that contains subdirectories and files.

The image below shows the tree hierarchy of the Example directory, which contains Dir1 and Dir2 subdirectories, with multiple text files in each:

An example of a directory and file hierarchy

Using the -r flag removes the entire directory, including subdirectories and files, while the -v flag lists each step of the process as the output:

Recursively removing multiple directories, subdirectories, and files using the rm command

The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm.

Displaying a prompt as the output when removing a directory

Write-protected directories require user input when deleting. Create such a directory with:

To remove the directory, use:

rm -d write protected directory

Type Y and press Enter to confirm deletion. To avoid the confirmation, use the -f flag or elevate the command privileges to sudo:

If the write-protected directory contains other files and directories, use the following command:

Note: rm -rf / is a dangerous Linux command that forces a recursive deletion on the root directory, rendering your system unusable.
Avoid using -f and sudo while removing directories, unless you know what you’re doing. To learn more about this command, visit our article What Is sudo rm -rf in Linux and Is It Dangerous.

rmdir Command

The Linux rmdir command removes empty directories only. The command uses the following syntax:

rmdir [options] [directory name]

The rmdir command includes the following options:

  • —ignore-fail-on-non-empty : Doesn’t show an error message when trying to remove a non-empty directory.
  • -p : Removes the directory along with its parent in the hierarchy.
  • -v : Provides a verbose output.
  • —help : Displays help text.
  • —version : Displays the command version.

Using the rmdir command on a non-empty directory produces an error:

Using the rmdir command on a non-empty directory

In this case, the Example directory contains the Test subdirectory:

Showing the hierarchy of the Example directory

To remove these directories using the rmdir command, add them in reverse order of hierarchy. Using the -v option lists each step of the process as the output:

rmdir -v Example/Test Example

Removing multiple directories with the rmdir command

A simpler method of doing this is to use the -p option with the subdirectory’s name. This removes both the subdirectory and its hierarchical parent:

Removing a subdirectory and its parent using the rmdir command

The rmdir command allows you to remove multiple directories with similar names using wildcards. For instance, if you want to remove directories named Example1, Example2, and Example3:

Using wildcards with the rmdir command to remove multiple directories

After reading this tutorial, you should be able to remove directories in Linux using commands in the terminal window or command line.

To learn more about other commands in Linux, check out our Linux commands cheat sheet.

Источник

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