By mistake, I have created a directory with tilde its name, (the ~ character). Now, how to correct it?
Here are the practical experiments and results.
Listing the directory with tilde in its name
codetryout:~$ ls -l total 4 drwx---r-x 2 codetryout codetryout 219 Sep 25 00:03 mysql drwx---r-x 9 codetryout codetryout 155 Sep 22 14:22 www drwxr-xr-x 8 codetryout codetryout 134 Sep 22 08:10 testing drwxr-xr-x 3 root root 4096 Sep 25 00:02 logs drwxr-xr-x 3 codetryout codetryout 21 Sep 23 23:25 '~~'
rm command man page reference
The rm command man page says, the filename with some special characters such as -, can be removed by using — option
To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo
Let’s try that,
codetryout:~$ rm -v -- '~~'/ rm: cannot remove '~~/': Is a directory
rm did not remove since in our case it is a directory, not a file.
Solution for removing the directories with tilde in its name
Let us specify this is a directory and remove it forcefully by adding r and f options.
codetryout:~$ rm -vrf -- '~~'/ removed directory '~~/mysql-bkp' removed directory '~~/' codetryout:~$
Finally, the command removed the directory and its subdirectories recursively.
Syntax to note here:
rm -vrf -- "FOLDER-NAME-WITH-TILDE"
Verifying the results after deletion.
codetryout:~$ ls -l total 4 drwx---r-x 2 codetryout codetryout 219 Sep 25 00:03 mysql drwx---r-x 9 codetryout codetryout 155 Sep 22 14:22 www drwxr-xr-x 8 codetryout codetryout 134 Sep 22 08:10 testing drwxr-xr-x 3 root root 4096 Sep 25 00:02 logs codetryout:~$
This is tested in Ubuntu server, should work on any bash terminal (Linux, CentOS, RedHat, Ubuntu, and Docker Linux containers)
Warning: rm -rf command will permanently delete the file or directory, make sure that you are deleting the intended file or directory