I have a directory name foo
located in ~/Desktop
. Suppose I want to create a directory from a terminal with same name and in the same location. When I tried mkdir
it gave an error:
mkdir: cannot create directory `/home/nux/Desktop/foo': File exists
Thats makes sense, but I want to replace foo
if exists. I looked through man mkdir
but there isn't any option that do that.
So how to overwrite foo
directory?
Why doesn't mkdir
have an option to do this?
If your goal is to execute a one-line command that:
~/Desktop/foo
if it already exists.~/Desktop/foo
if it does not already exist.Then you can use:
;
is equivalent to a newline, but it lets you execute multiple commands on a single line (i.e., as a "single command").-f
flag to remove them without prompting the user interactively. This is okay, but I do recommend being especially careful withrm -rf ...
. Seeman rm
for details.rm
command to finish before themkdir
command executes; this is the reason to use;
instead of&
. (A command preceding&
runs asynchronously in the background.)mkdir
command to run when therm
command succeeds; this is the reason to use;
instead of||
.mkdir
command to run when therm
command fails (usually failure will mean the directory didn't already exist); this is the reason to use;
instead of&&
.rm
command might fail even when the directory already existed, in which case themkdir
command will fail also, but the error messages will make sense and there's probably no need to add a middle step checking forfoo
's existence before trying to create it.See 3.2.3 Lists of Commands in the Bash Reference Manual for more information and explanation about the
;
,&
,||
, and&&
operators.As muru suggested (and Rinzwind elaborated), I do recommend you look into
rsync
to see if it will meet your backup needs. There are some additional useful guides on the rsync documentation page, as well as this Ubuntu rsync guide.mkdir
creates directories (the "mk" stands for "make"). For it also to recursively delete directories and all the files in them would be bad, because it would violate the principle of least astonishment in a way that would likely lead to data loss.rmdir
doesn't even remove directories that have any (non-directory) files in them.rm
has an-r
option, which makes sense sincerm
is expected to remove files (that is its purpose, thus the inherent danger is intuitive and users typically know to be careful when runningrm
commands).No, there is no single command to do what you are asking.
Why?
In this instance, the
mkdir
andrm
commands do what you require, and work well together, sincerm -r
will delete the directory as well, so a subsequentmkdir
will create the directory.1The Art of Unix Programming, Eric S. Raymond, itself quoting Doug McIlroy.
The command to remove a directory is
rm
. So you need 2 commands.As shown in comments you can chain them with
;
(= do both even if 1st fails) or&&
(= only mkdir when the rm does not fail).The
-r
is for removing dirs. The 1st command also removes ALL contents of the directory. If that is NOT your intention both rm and mkdir are not what you are looking for: permissions arechmod
andchown
.Ambiguity.
mkdir
means make directory. Not remove and make directory. And since therm
command is for deleting stuff there is no need forrmdir
to be able to remove a directory.To do that in the cleanest way I use this:
And to use it:
This makes sure that what you are trying to remove is a directory and not a file:
-d
Check if a directory with that name already exists. if so, delete it.-f
Check that no file with this name exists. if so, print error message and exit.Create the new directory and echo success, or if failed, echo failed.