Unix command delete folder and files


















If you read this far, tweet to the author to show them you care. Tweet a thanks. Learn to code for free. Get started. Forum Donate. Ilenia Magoni. In this guide you will learn how to delete directories and files from the Linux command line. The Linux rm Command The rm short for remove command is pretty useful. The options are as follows: -r , "recursive" — this option allows you to delete folders and recursively remove their content first -i , "interactive" — with this option, it will ask for confirmation each time before you delete something -f , "force" — it ignores non-existent files and overrides any confirmation prompt essentially, it's the opposite of -i.

It will not remove files from a directory if the directory is write-protected. It works only if the directory is empty. It has these files and folders inside: Let's use this directory to show how the various options work. Let's recreate the folder and try again.

How do I remove a directory and all its contents? Ask Question. Asked 9 years, 5 months ago. Active 6 years, 2 months ago. Viewed k times. In bash all I know is that rmdir directoryname will remove the directory but only if it's empty.

Is there a way to force remove subdirectories? Improve this question. Piper Piper 2, 2 2 gold badges 17 17 silver badges 15 15 bronze badges. Add a comment. Active Oldest Votes. The following command will do it for you. Use caution though. Improve this answer. Phillip Nordwall Phillip Nordwall 3, 1 1 gold badge 13 13 silver badges 5 5 bronze badges. A safer command to start with would be rm -r directoryname.

The rm command in Linux removes files and directories. Note: To remove multiple files or directories using the rm command, add multiple file or directory names, separated by blank spaces. Trying to use the rm command without any options to remove a directory results in an error message:. 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. The example below shows that the rm command with the - d flag removes the Example directory:.

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

The -i option displays a prompt asking you to confirm directory removal. Type Y and press Enter to confirm. The Linux rmdir command removes empty directories only. The command uses the following syntax:. Using the rmdir command on a non-empty directory produces an error:. To remove these directories using the rmdir command, add them in reverse order of hierarchy.



0コメント

  • 1000 / 1000