You're working on a Linux /Unix distribution and do not find the shell command that lets you erase the contents of a directory ...
You're working on a Linux/Unix distribution and do not find the shell command that lets you erase the contents of a directory without deleting this same, ie the folder container. Or you want to delete the contents of folders recursively, etc.
Well ! First of all, to solve any problem on Linux or any operating system based on the Unix kernel there are many methods, procedures and algorithms which we can use and all them work perfectly. So on the web you can find many solutions and tutorials about UNIX command which we can manipulate to delete files and folders but the shell commands which I will mention in this article are the most used.
$ rm Default command to erase files and folder on Linux
$ rm
: The default Shell command for deleting ordinary files and directories on Linux or UNIX distributions.Example:
$ rm File_Name
: This command Shell serves to remove empty file or folder named "File_Name
"$ rm -r Unix Command: Remove a folder and its content recursively
Example:
$ rm -r Folder_Name
: This command Shell serves to remove a folder called "Folder_Name
" and its content recursively.$ rm -rf Shell command: Erase the content without the container folder on Linux
If you want to delete the content of a folder without removing the same container folder you can displace on the container folder with
$ cd Folder_Name
Unix command then use $ rm -r *
or without $ cd
command you can directly execute $ rm -rf Folder_Name/*
Note:
We have added the
-f
option to force the deletion of files and to do not ask us the confirmation.Another simple solution is with the following Shell command:
$ rm Folder_Name/* -Rf
Another trick and the last:
$ rm -rf Folder_Name ; mkdir Folder_Name
What makes this last command is to delete the folder and its contents recursively with the
-r
option, no confirmation message with -f
and eventually create new empty folder with the same name with the $ mkdir
command and separate the two commands with ;
That is all !! If you know another trick to delete files, documents and folders with the command shell in unix comment it down in the comments section to discuss it. See you in another tutorial ^_^
COMMENTS