You can use the rm command to remove all non-hidden files and subdirectories (along with their contents) from a directory using the command line in Ubuntu.
sudo rm -rf /path/to/directory/*
For example, if your current working directory is /var/www and you want to delete all files and subdirectories inside the directory named sample inside of it, you can simply use the command:
sudo rm -rf sample/*
Or, if you prefer absolute path you can run the following command.
sudo rm -rf /var/www/sample/*
Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash / is an absolute path.
The options have the following meanings:
-r : Recursive remove. rm will recursively remove directory and everything under it.
-f : Forced remove. Unwritable files are forcibly removed. By default, rm will ask permission before removing unwritable files. Use this option with care.
Additionally, for interactive removal you can use the option -i with the rm command (i.e. -rfi). With this option rm will ask permission before removing anything.
Moreover, if you only want to remove the files in a directory while keeping the subdirectories as is, you can use the rm command without the -r option like this:
Use the
rmCommandYou can use the
rmcommand to remove all non-hidden files and subdirectories (along with their contents) from a directory using the command line in Ubuntu.For example, if your current working directory is
/var/wwwand you want to delete all files and subdirectories inside the directory namedsampleinside of it, you can simply use the command:Or, if you prefer absolute path you can run the following command.
Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash
/is an absolute path.The options have the following meanings:
-r: Recursive remove.rmwill recursively remove directory and everything under it.-f: Forced remove. Unwritable files are forcibly removed. By default,rmwill ask permission before removing unwritable files. Use this option with care.Additionally, for interactive removal you can use the option
-iwith thermcommand (i.e.-rfi). With this optionrmwill ask permission before removing anything.Moreover, if you only want to remove the files in a directory while keeping the subdirectories as is, you can use the
rmcommand without the-roption like this: