Deleting Directories with Confirmation
When deleting directories, it's often a good practice to ask for confirmation before permanently removing the files. This can help prevent accidental deletion of important data.
You can add the -i
(interactive) option to the rm
and rmdir
commands to prompt for confirmation before deleting a directory.
For example, to delete a directory with confirmation, you can use the following command:
rm -ri /path/to/directory
This will prompt you for confirmation before deleting each file or directory:
rm: remove directory '/path/to/directory'?
You can then type y
to confirm the deletion, or n
to skip it.
Similarly, you can use the -i
option with the rmdir
command to delete an empty directory with confirmation:
rmdir -i /path/to/directory
This will prompt you for confirmation before deleting the directory:
rmdir: remove directory '/path/to/directory'?
Using the -i
option can be especially helpful when deleting directories with the rm -r
command, as it can prevent you from accidentally deleting important files or directories.
Keep in mind that the -i
option will prompt for confirmation for each file or directory being deleted, which can be time-consuming for large directory structures. In such cases, you may want to consider using the -I
(interactive-once) option instead, which will only prompt for confirmation once for the entire operation.
rm -rI /path/to/directory
In the next section, we'll discuss how to handle non-empty directories.