0

I have a working folder with all output files generated from command. When I tried to re-coding one of my file, after a few attempts, I realized the files in my folder left only 1/3 of them! I first thought they are gone, and wanted to repeat the experiment XD

But when I tried to create a folder named "bin", it said:

mkdir: cannot create directory ‘bin’: File exists

and there is even files in the "bin" directory!

What is happening here? How can I check? 😅

Update: I was having window dual-boot with linux. I opened up my Windows (with not related intention) and just happened to realise that the missing folder could not be opened and was marked as corrupted (still not sure why until now). So I repaired the file as instructed by Windows, and I got them all back!) Thanks everyone for the help anyway! Appreciate them!

1
  • Good answer below. I will speculate that you have some code like mkdir ${dirPath}/bin and that the dirPath variable (forr some reason) is not set , possibly a typo? Just a guess. ,,,, better debugging can be achieved with set -x (set +x to disable) AND export PS4='${LINENO} >' (export PS4="+" is the default value) . It will show each Line of executed code with all env-vars, cmd-substitutions, etc, expanded so you know exactly what command the shell tried to Run. All dbl-quoted strings are "normalized" to single-quoted strings.
    – shellter
    Commented May 4 at 14:07

1 Answer 1

2

It's correctly telling you that bin already exists and thus cannot be created (perhaps a little confusing is that it refers to it as a file, but that's just semantics).

If you want to ensure a directory is exists, where it might or might not already exist, the simplest way is with the -p flag:

mkdir -p bin

e.g.

[root@6b5f5482c4df tmp]# ls -ld bin
ls: cannot access 'bin': No such file or directory
[root@6b5f5482c4df tmp]# mkdir bin
[root@6b5f5482c4df tmp]# ls -ld bin
drwxr-xr-x 2 root root 4096 May  3 14:09 bin
[root@6b5f5482c4df tmp]# mkdir bin
mkdir: cannot create directory ‘bin’: File exists
[root@6b5f5482c4df tmp]# mkdir -p bin
[root@6b5f5482c4df tmp]# ls -ld bin
drwxr-xr-x 2 root root 4096 May  3 14:09 bin
[root@6b5f5482c4df tmp]#

NB - even with -p, if bin were to exist as an actual file, it would still fail correctly -- you would have to remove such a file to rectify. Continuing from the above state:

[root@6b5f5482c4df tmp]# rmdir bin
[root@6b5f5482c4df tmp]# ls -ld bin
ls: cannot access 'bin': No such file or directory
[root@6b5f5482c4df tmp]# touch bin
[root@6b5f5482c4df tmp]# ls -ld bin
-rw-r--r-- 1 root root 0 May  3 14:15 bin
[root@6b5f5482c4df tmp]# mkdir -p bin
mkdir: cannot create directory ‘bin’: File exists
[root@6b5f5482c4df tmp]#

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .