How to create a directory only if that directory is not already exist

Home Questions How to create a directory only if that directory is not already exist

This topic contains 1 reply, has 2 voices, and was last updated by  Thushara Muralidharan 2 years, 3 months ago.

  • Author
    Posts
  • #6661 Reply

    admin
    Keymaster
    Online
    Topics: 65
    Replies: 65

    Often we use to create a folder in a directory which contains lot of sub directories. When we try to create a directory, we will get the below error message if that directory is already present.
    mkdir: Failed to make directory “test”; File exists

    I want to know how to hide the above error message and to create folder only if its not already exist. If its already exist then it should ignore that folder.

    Any of eTechTricks friends, please help me…

  • #6740 Reply

    Thushara Muralidharan
    Participant
    Offline
    Topics: 12
    Replies: 10

    Use -p option with mkdir command. This will omit if that directory is already present.

    mkdir -p dir

    It will not threw error message even that directory is already present. It will ignore that directory.

Reply To: How to create a directory only if that directory is not already exist

You can use BBCodes to format your content.
Your account can't use Advanced BBCodes, they will be stripped before saving.

Your information: