{"id":2633,"date":"2023-07-29T15:51:31","date_gmt":"2023-07-29T15:51:31","guid":{"rendered":"https:\/\/draculaservers.com\/tutorials\/?p=2633"},"modified":"2023-08-10T13:36:30","modified_gmt":"2023-08-10T13:36:30","slug":"check-if-a-directory-exists-in-bash","status":"publish","type":"post","link":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/","title":{"rendered":"How to Check if a Directory Exists in Bash?"},"content":{"rendered":"<div class=\"cl-preview-section\">\n<p>In the world of Bash scripting, one common task is to check if a directory exists before proceeding with further actions. Whether you are writing automation scripts, performing system maintenance, or managing files and directories, it\u2019s essential to ensure that the directory you are about to work with exists to avoid errors and unexpected behaviors.<\/p>\n<p>In this comprehensive guide, you will learn various methods to check if a directory exists in Bash, with practical examples and best practices.<\/p>\n<\/div>\n\n<p>Let&#8217;s get started!<\/p>\n<div class=\"cl-preview-section\">\n<h2 id=\"method-1-using-the-test-command\"><strong>Method 1: Using the\u00a0<code>test<\/code>\u00a0Command<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>The\u00a0<code>test<\/code>\u00a0command (also represented by\u00a0<code>[ ]<\/code>) is a built-in command in Bash that allows you to perform various tests, including directory existence checks. To check if a directory exists, you can use the\u00a0<code>-d<\/code>\u00a0option with the\u00a0<code>test<\/code>\u00a0command:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span> -d <span class=\"token string\">\"\/path\/to\/directory\"<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory exists.\"<\/span>\r\n<span class=\"token keyword\">else<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory does not exist.\"<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>In this example, we use the\u00a0<code>-d<\/code>\u00a0option to check if the directory at \u201c\/path\/to\/directory\u201d exists. If the directory is present, the script will display \u201cThe directory exists.\u201d Otherwise, it will show \u201cThe directory does not exist.\u201d<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"method-2-using-the---construct\"><span id=\"method-2-using-the-construct\"><strong>Method 2: Using the\u00a0<code>[[ ]]<\/code>\u00a0Construct<\/strong><\/span><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>The\u00a0<code>[[ ]]<\/code>\u00a0construct is an enhanced version of the\u00a0<code>test<\/code>\u00a0command, providing additional capabilities and improved syntax. To check if a directory exists using\u00a0<code>[[ ]]<\/code>, you can do the following:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">[<\/span> -d <span class=\"token string\">\"\/path\/to\/directory\"<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory exists.\"<\/span>\r\n<span class=\"token keyword\">else<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory does not exist.\"<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>The\u00a0<code>[[ ]]<\/code>\u00a0construct is preferred over\u00a0<code>[ ]<\/code>\u00a0in most cases due to its enhanced functionality and improved readability.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"method-3-using-the-mkdir-command-with-caution\"><strong>Method 3: Using the\u00a0<code>mkdir<\/code>\u00a0Command (With Caution)<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>While not a recommended method, it\u2019s worth mentioning that you can use the\u00a0<code>mkdir<\/code>\u00a0command with the\u00a0<code>-p<\/code>\u00a0option to create the directory if it doesn\u2019t exist. However, this approach should be used with caution, as it creates the directory if it\u2019s missing, which may not be the desired behavior in all scenarios.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token function\">mkdir<\/span> -p <span class=\"token string\">\"\/path\/to\/directory\"<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>If the directory already exists,\u00a0<code>mkdir -p<\/code>\u00a0will not cause any issues. However, if the directory doesn\u2019t exist, it will be created.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"draculas-high-performance-sneaker-bot-servers\">Dracula&#8217;s High-Performance Sneaker Bot Servers<\/h2>\n<p>Score limited-edition kicks and stay ahead in the game with our high-performance Sneaker Servers. Designed for sneakerheads and resellers, these servers offer blazing-fast speeds and unwavering stability. Never miss a release again with DraculaServers&#8217; Sneaker Servers by your side. Level up your sneaker-copping game now.<\/p>\n<p>Ready to secure the freshest kicks? Discover the power of our Sneaker Servers Here! Don&#8217;t let slow servers hold you back\u2014unleash your sneaker-bot&#8217;s potential with DraculaServers today.<\/p>\n<p>Check out Sneaker Servers <a href=\"https:\/\/draculaservers.com\/sneaker-bot-servers.php\" target=\"_blank\" rel=\"noopener\">Here!<\/a><\/p>\n<h2 id=\"method-4-using-the-stat-command\"><strong>Method 4: Using the\u00a0<code>stat<\/code>\u00a0Command<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>The\u00a0<code>stat<\/code>\u00a0command provides detailed information about files and directories. We can use it to check if a directory exists by capturing the output of the command and examining the results.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token function\">stat<\/span> <span class=\"token string\">\"\/path\/to\/directory\"<\/span> <span class=\"token operator\">&gt;<\/span>\/dev\/null 2<span class=\"token operator\">&gt;<\/span><span class=\"token operator\">&amp;<\/span>1<span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory exists.\"<\/span>\r\n<span class=\"token keyword\">else<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory does not exist.\"<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>In this example, we redirect both standard output (<code>stdout<\/code>) and standard error (<code>stderr<\/code>) to\u00a0<code>\/dev\/null<\/code>\u00a0to suppress any output. The exit status of the\u00a0<code>stat<\/code>\u00a0command determines if the directory exists. If it does, the script will display \u201cThe directory exists.\u201d Otherwise, it will show \u201cThe directory does not exist.\u201d<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"method-5-using-the-find-command\"><strong>Method 5: Using the\u00a0<code>find<\/code>\u00a0Command<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>The\u00a0<code>find<\/code>\u00a0command is typically used for searching files and directories based on various criteria. While not the most efficient way to check for directory existence, you can use\u00a0<code>find<\/code>\u00a0for this purpose:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token function\">find<\/span> <span class=\"token string\">\"\/path\/to\/directory\"<\/span> -maxdepth 0 -type d -print -quit 2<span class=\"token operator\">&gt;<\/span>\/dev\/null<span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory exists.\"<\/span>\r\n<span class=\"token keyword\">else<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory does not exist.\"<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>In this approach, we use\u00a0<code>find<\/code>\u00a0with the\u00a0<code>-maxdepth 0<\/code>\u00a0option to restrict the search to the current directory only (not searching in subdirectories). The\u00a0<code>-type d<\/code>\u00a0option specifies that we are looking for directories. The\u00a0<code>-print<\/code>\u00a0action is used to print the directory name if it exists, and\u00a0<code>-quit<\/code>\u00a0is used to stop\u00a0<code>find<\/code>\u00a0after finding the first matching directory. We redirect\u00a0<code>stderr<\/code>\u00a0to\u00a0<code>\/dev\/null<\/code>\u00a0to suppress any error messages.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"best-practices-and-considerations\"><strong>Best Practices and Considerations<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>When checking for directory existence in Bash, consider the following best practices:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"use---or--\"><span id=\"1-use-or\"><strong>1. Use\u00a0<code>[ ]<\/code>\u00a0or\u00a0<code>[[ ]]<\/code>:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>As mentioned earlier, the\u00a0<code>[[ ]]<\/code>\u00a0construct is preferred over\u00a0<code>[ ]<\/code>\u00a0due to its improved syntax and additional capabilities. It is more intuitive and generally easier to read.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"quote-the-directory-path\"><span id=\"2-quote-the-directory-path\"><strong>2. Quote the Directory Path:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>Always enclose the directory path in double quotes to handle spaces and special characters correctly. For example:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">[<\/span> -d <span class=\"token string\">\"\/path with spaces\/directory\"<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token comment\"># Do something<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"avoid-using-mkdir--p\"><span id=\"3-avoid-using-mkdir-p\"><strong>3. Avoid Using\u00a0<code>mkdir -p<\/code>:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>As mentioned earlier, using\u00a0<code>mkdir -p<\/code>\u00a0to check directory existence is not recommended. It may lead to unintended directory creation and could be a security risk.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"capture-and-use-exit-status\"><span id=\"4-capture-and-use-exit-status\"><strong>4. Capture and Use Exit Status:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>When using commands like\u00a0<code>stat<\/code>\u00a0or\u00a0<code>find<\/code>\u00a0to check for directory existence, rely on the exit status to determine the result. Redirecting output to\u00a0<code>\/dev\/null<\/code>\u00a0helps to suppress unnecessary messages.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"use-logical-not-for-negative-checks\"><span id=\"5-use-logical-not-for-negative-checks\"><strong>5. Use Logical NOT for Negative Checks:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>To check if a directory does not exist, you can use the logical NOT (<code>!<\/code>) operator with the condition. For example:<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<pre class=\" language-bash\"><code class=\"prism language-bash\"><span class=\"token keyword\">if<\/span> <span class=\"token operator\">!<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">[<\/span> -d <span class=\"token string\">\"\/path\/to\/directory\"<\/span> <span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token keyword\">then<\/span>\r\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string\">\"The directory does not exist.\"<\/span>\r\n<span class=\"token keyword\">fi<\/span>\r\n<\/code><\/pre>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>This approach improves code readability.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h3 id=\"consider-cross-platform-compatibility\"><span id=\"6-consider-cross-platform-compatibility\"><strong>6. Consider Cross-Platform Compatibility:<\/strong><\/span><\/h3>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>If your Bash script needs to be executed on different platforms, keep in mind that file system conventions and commands may vary. Be cautious when using OS-specific features or commands.<\/p>\n<\/div>\n<div class=\"cl-preview-section\">\n<h2 id=\"conclusion\"><strong>Conclusion<\/strong><\/h2>\n<\/div>\n<div class=\"cl-preview-section\">\n<p>In Bash scripting, checking for the existence of a directory is a fundamental task to ensure the success and reliability of your scripts. By employing the various methods we\u2019ve explored, such as using the\u00a0<code>test<\/code>\u00a0command, the\u00a0<code>[[ ]]<\/code>\u00a0construct, or the\u00a0<code>stat<\/code> command, you can confidently determine if a directory exists before proceeding with other operations.<\/p>\n<p>Remember to adhere to best practices, such as quoting directory paths, capturing exit statuses, and avoiding unnecessary directory creation. With these techniques at your disposal, you can write robust and error-free Bash scripts that gracefully handle directory existence checks with ease.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the world of Bash scripting, one common task is to check if a directory exists before proceeding with further actions. Whether you are writing automation scripts, performing system maintenance, or managing files and directories, it\u2019s essential to ensure that the directory you are about to work with exists to avoid errors and unexpected behaviors. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":2634,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,61],"tags":[167,168,102,111],"class_list":["post-2633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux-basics","category-linux-commands","tag-bash","tag-bash-directory","tag-linux","tag-linux-tutorials"],"blocksy_meta":[],"featured_image_urls_v2":{"full":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png",1280,720,false],"thumbnail":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-150x150.png",150,150,true],"medium":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-300x169.png",300,169,true],"medium_large":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-768x432.png",768,432,true],"large":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-1024x576.png",1024,576,true],"1536x1536":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png",1280,720,false],"2048x2048":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png",1280,720,false],"pk-small":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-80x80.png",80,80,true],"pk-thumbnail":["https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory-300x225.png",300,225,true]},"post_excerpt_stackable_v2":"<p>In the world of Bash scripting, one common task is to check if a directory exists before proceeding with further actions. Whether you are writing automation scripts, performing system maintenance, or managing files and directories, it\u2019s essential to ensure that the directory you are about to work with exists to avoid errors and unexpected behaviors. In this comprehensive guide, you will learn various methods to check if a directory exists in Bash, with practical examples and best practices. Let&#8217;s get started! Method 1: Using the\u00a0test\u00a0Command The\u00a0test\u00a0command (also represented by\u00a0[ ]) is a built-in command in Bash that allows you to&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/draculaservers.com\/tutorials\/category\/linux-basics\/\" rel=\"category tag\">Linux Basics<\/a>, <a href=\"https:\/\/draculaservers.com\/tutorials\/category\/linux-commands\/\" rel=\"category tag\">Linux Commands<\/a>","author_info_v2":{"name":"Abdul Mannan","url":"https:\/\/draculaservers.com\/tutorials\/author\/abdul-mannan-tbgmail-com\/"},"comments_num_v2":"0 comments","yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials<\/title>\n<meta name=\"description\" content=\"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials\" \/>\n<meta property=\"og:description\" content=\"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"Dracula Servers Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-29T15:51:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-10T13:36:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Abdul Mannan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abdul Mannan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/\"},\"author\":{\"name\":\"Abdul Mannan\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/ac89d0281f4fb596bfaa0bc1e746c8a6\"},\"headline\":\"How to Check if a Directory Exists in Bash?\",\"datePublished\":\"2023-07-29T15:51:31+00:00\",\"dateModified\":\"2023-08-10T13:36:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/\"},\"wordCount\":847,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Directory.png\",\"keywords\":[\"Bash\",\"Bash Directory\",\"linux\",\"Linux Tutorials\"],\"articleSection\":[\"Linux Basics\",\"Linux Commands\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/\",\"url\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/\",\"name\":\"How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Directory.png\",\"datePublished\":\"2023-07-29T15:51:31+00:00\",\"dateModified\":\"2023-08-10T13:36:30+00:00\",\"description\":\"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#primaryimage\",\"url\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Directory.png\",\"contentUrl\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/Directory.png\",\"width\":1280,\"height\":720},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/check-if-a-directory-exists-in-bash\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Check if a Directory Exists in Bash?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/\",\"name\":\"Dracula Servers Tutorials\",\"description\":\"Dedicated Servers\",\"publisher\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#organization\",\"name\":\"Dracula Servers\",\"url\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2016\\\/06\\\/dracula_full_logo_smaller.png\",\"contentUrl\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/wp-content\\\/uploads\\\/2016\\\/06\\\/dracula_full_logo_smaller.png\",\"width\":1625,\"height\":200,\"caption\":\"Dracula Servers\"},\"image\":{\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/draculaservers.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/ac89d0281f4fb596bfaa0bc1e746c8a6\",\"name\":\"Abdul Mannan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g\",\"caption\":\"Abdul Mannan\"},\"description\":\"An individual trying to decipher the enigmas of technology by the sheer driving force of curiosity. Interested in learning new skills and being better at those skills than the lot.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials","description":"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials","og_description":"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.","og_url":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/","og_site_name":"Dracula Servers Tutorials","article_published_time":"2023-07-29T15:51:31+00:00","article_modified_time":"2023-08-10T13:36:30+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png","type":"image\/png"}],"author":"Abdul Mannan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abdul Mannan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#article","isPartOf":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/"},"author":{"name":"Abdul Mannan","@id":"https:\/\/draculaservers.com\/tutorials\/#\/schema\/person\/ac89d0281f4fb596bfaa0bc1e746c8a6"},"headline":"How to Check if a Directory Exists in Bash?","datePublished":"2023-07-29T15:51:31+00:00","dateModified":"2023-08-10T13:36:30+00:00","mainEntityOfPage":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/"},"wordCount":847,"commentCount":0,"publisher":{"@id":"https:\/\/draculaservers.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png","keywords":["Bash","Bash Directory","linux","Linux Tutorials"],"articleSection":["Linux Basics","Linux Commands"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/","url":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/","name":"How to Check if a Directory Exists in Bash? - Dracula Servers Tutorials","isPartOf":{"@id":"https:\/\/draculaservers.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#primaryimage"},"image":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#primaryimage"},"thumbnailUrl":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png","datePublished":"2023-07-29T15:51:31+00:00","dateModified":"2023-08-10T13:36:30+00:00","description":"Using the\u00a0test\u00a0command, the\u00a0[[ ]]\u00a0construct, or the\u00a0stat command, you can check if a directory exists before with in Bash.","breadcrumb":{"@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#primaryimage","url":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png","contentUrl":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2023\/07\/Directory.png","width":1280,"height":720},{"@type":"BreadcrumbList","@id":"https:\/\/draculaservers.com\/tutorials\/check-if-a-directory-exists-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/draculaservers.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How to Check if a Directory Exists in Bash?"}]},{"@type":"WebSite","@id":"https:\/\/draculaservers.com\/tutorials\/#website","url":"https:\/\/draculaservers.com\/tutorials\/","name":"Dracula Servers Tutorials","description":"Dedicated Servers","publisher":{"@id":"https:\/\/draculaservers.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/draculaservers.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/draculaservers.com\/tutorials\/#organization","name":"Dracula Servers","url":"https:\/\/draculaservers.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/draculaservers.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2016\/06\/dracula_full_logo_smaller.png","contentUrl":"https:\/\/draculaservers.com\/tutorials\/wp-content\/uploads\/2016\/06\/dracula_full_logo_smaller.png","width":1625,"height":200,"caption":"Dracula Servers"},"image":{"@id":"https:\/\/draculaservers.com\/tutorials\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/draculaservers.com\/tutorials\/#\/schema\/person\/ac89d0281f4fb596bfaa0bc1e746c8a6","name":"Abdul Mannan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2809442d44177cab4f90e1d9b3295560462063881ca1374b6d597d8f0b48fc21?s=96&d=mm&r=g","caption":"Abdul Mannan"},"description":"An individual trying to decipher the enigmas of technology by the sheer driving force of curiosity. Interested in learning new skills and being better at those skills than the lot."}]}},"_links":{"self":[{"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/posts\/2633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/comments?post=2633"}],"version-history":[{"count":3,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/posts\/2633\/revisions"}],"predecessor-version":[{"id":2639,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/posts\/2633\/revisions\/2639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/media\/2634"}],"wp:attachment":[{"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/media?parent=2633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/categories?post=2633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draculaservers.com\/tutorials\/wp-json\/wp\/v2\/tags?post=2633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}