Friday, April 26, 2024
38
rated 0 times [  38] [ 0]  / answers: 1 / hits: 87218  / 1 Year ago, thu, january 12, 2023, 11:28:43

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?


More From » command-line

 Answers
6

If your goal is to execute a one-line command that:




  • Removes and recreates the directory ~/Desktop/foo if it already exists.

  • Just creates the directory ~/Desktop/foo if it does not already exist.



Then you can use:



rm -r ~/Desktop/foo; mkdir ~/Desktop/foo


; is equivalent to a newline, but it lets you execute multiple commands on a single line (i.e., as a "single command").




  • If the directory you're removing may contain readonly files, you'll need the -f flag to remove them without prompting the user interactively. This is okay, but I do recommend being especially careful with rm -rf .... See man rm for details.

  • You need the rm command to finish before the mkdir command executes; this is the reason to use ; instead of &. (A command preceding & runs asynchronously in the background.)

  • You need the mkdir command to run when the rm command succeeds; this is the reason to use ; instead of ||.

  • You need the mkdir command to run when the rm command fails (usually failure will mean the directory didn't already exist); this is the reason to use ; instead of &&.

  • The rm command might fail even when the directory already existed, in which case the mkdir command will fail also, but the error messages will make sense and there's probably no need to add a middle step checking for foo'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.




why mkdir doesn't has this option ?




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 since rm is expected to remove files (that is its purpose, thus the inherent danger is intuitive and users typically know to be careful when running rm commands).


[#23448] Saturday, January 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fenddy

Total Points: 361
Total Questions: 103
Total Answers: 113

Location: Turkmenistan
Member since Sun, Aug 2, 2020
4 Years ago
fenddy questions
Tue, Nov 22, 22, 10:11, 1 Year ago
Tue, Sep 27, 22, 09:16, 2 Years ago
Wed, Dec 28, 22, 13:09, 1 Year ago
Fri, Jun 18, 21, 14:04, 3 Years ago
;