<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ファイルが存在 &#8211; スグブログ</title>
	<atom:link href="https://sugublog.org/tag/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%8C%E5%AD%98%E5%9C%A8/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Mon, 20 Nov 2023 14:03:56 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sugublog.org/wp-content/uploads/2021/07/icon_20210703-min-150x150.png</url>
	<title>ファイルが存在 &#8211; スグブログ</title>
	<link>https://sugublog.org</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【シェルスクリプト】ifを使いファイルが存在するかで場合分けする方法</title>
		<link>https://sugublog.org/shell-script-file-exist/</link>
					<comments>https://sugublog.org/shell-script-file-exist/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Wed, 05 Apr 2023 03:52:30 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[ファイルが存在]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5455</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-19-1024x503.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトでifでファイルが存在するか確認して、処理を場合分けしたい場面はよくあります。 どのようにすれば良いかわからないという人の為に、今回記事にします。 普段シェルスクリプトを使用している筆者が、実例を元に解説 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-19-1024x503.jpg" class="webfeedsFeaturedVisual" /></p>
<p><span class="swl-marker mark_orange"><strong>シェルスクリプト</strong>で<strong>if</strong>で<strong>ファイルが存在</strong>するか確認して、処理を場合分けしたい場面</span>はよくあります。</p>



<p>どのようにすれば良いかわからないという人の為に、今回記事にします。</p>



<p class="is-style-icon_info">普段シェルスクリプトを使用している筆者が、実例を元に解説します。</p>


<div class="p-blogParts post_content" data-partsID="5030">
<p>windowsのプロンプトで使用できるbatやpythonやphpを使用して、業務効率化やWebサイトを作成しております。javaやシェルスクリプトなど、様々なプログラミングを行った実績ありの筆者です。</p>
</div>


<p>またファイルが存在するかだけでなく、存在しない場合、その他のオプション、実際業務で使用できる応用も一緒に解説します。</p>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-9330904718533231" data-ad-slot="6086594686"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">シェルスクリプトのifを使用してファイルが存在するかで場合分け</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="768" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg" alt="ファイルが存在" class="wp-image-3840" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-1024x768.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-300x225.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1-768x576.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-1.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h3 class="wp-block-heading">シェルスクリプトでファイルが存在するかチェックする基礎</h3>



<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトでファイルが存在</span></strong>するかどうかを判定するには以下を使用します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>if [ -e ファイル名 ]; then
 処理するコマンド
fi</code></pre></div>



<h3 class="wp-block-heading">シェルスクリプトの解説</h3>



<p>場合分けするにはスクリプトではお馴染みの<strong>「if」</strong>を使用します。</p>



<p>ファイルが存在するかどうかは、テストコマンドの「[」を使用します。</p>



<p>オプションで「-e」を使用します。ファイルが存在すれば真、なければ偽となります。</p>



<p>結果、<strong><span class="swl-inline-color has-swl-deep-02-color">ファイルが存在すれば処理するコマンドが実行</span></strong>されます。なければ実行されません。</p>



<h2 class="wp-block-heading">シェルスクリプトでifを使用してファイルが存在するかやってみた</h2>



<p>では実際にシェルスクリプトで試してみます。</p>



<p>以下のシェルスクリプトを作成してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

if [ -e file_exist.txt ]; then
 echo &#39;ファイルは存在します&#39;
else
 echo &#39;ファイルは存在しません&#39;
fi</code></pre></div>



<p>シェルスクリプトと同じ階層に指定したファイルが存在すれば「ファイルは存在します」と、なければ「ファイルは存在しません」とechoコマンドで表示する</p>


<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/school_08-3-300x188.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-echo/">【シェルスクリプト基礎】echoコマンドの使い方！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトのechoコマンドの使い方で疑問に思っている事などないでしょうか？ 思い通りに文字列を表示できなかったりしないでしょうか？ シェルスクリプトでecho&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p>このシェルスクリプトと同じディレクトリに、以下のコマンドで「file_exist.txt」を作成します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>touch file_exist.txt</code></pre></div>



<p>実行して見ます。</p>



<h3 class="wp-block-heading">シェルスクリプトを実行する方法</h3>



<p>実行コマンドは以下です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>bash シェルスクリプト名</code></pre></div>



<p>以下でも可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>source シェルスクリプト名</code></pre></div>



<p>または「source」省略形の以下でも大丈夫です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>. シェルスクリプト名</code></pre></div>



<p class="is-style-icon_announce">※sourceコマンドは実行権が不要なので便利ですが、変数や関数などが保持される事に注意です。</p>



<p>または実行権をつけて以下でもよいです。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>chmod +x シェルスクリプト名
./シェルスクリプト名</code></pre></div>



<p>シェルスクリプトを実行するコマンドの説明は以下の記事で詳しく説明しています。</p>


<div class="swell-block-postLink">			<div class="p-blogCard -external" data-type="type1" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">スグブログ</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/08/script_0809-2.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://sugublog.org/shell-script-execution/" target="_blank" rel="noopener noreferrer">【シェルスクリプト基礎】コマンドを実行する様々な方法をご紹介！ | 【初心者向け】エラー対処法あり！</a>
						<span class="p-blogCard__excerpt">シェルスクリプトを実行する時に困った事はありませんか？ シェルスクリプトのコマンド実行での失敗メッセージ一覧 「実行権限がありません」 「コマンドが見つかりません&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">ファイルが存在する時のシェルスクリプト実行結果</h3>



<p>シェルスクリプトを実行すると以下の結果となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# . file_exist.sh
ファイルは存在します</code></pre></div>



<p>ファイルがあるので「ファイルが存在します」と表示されました。</p>



<h3 class="wp-block-heading">ファイルが存在しない時のシェルスクリプト実行結果</h3>



<p>では以下のコマンドで「file_exist.txt」を削除します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>rm -f file_exist.txt</code></pre></div>



<p class="is-style-icon_pen">-fオプションは強制的に削除します。これがなければ削除してよいか確認がはいります。</p>



<p>このファイルが存在しない状態でシェルスクリプトを実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# . file_exist.sh
ファイルは存在しません</code></pre></div>



<p>ファイルがないので「ファイルが存在しません」と表示されました。</p>



<p>成功しました。</p>



<h2 class="wp-block-heading">ifで使用できるその他のテストオプション</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="683" src="https://sugublog.org/wp-content/uploads/2021/07/search-1024x683.jpg" alt="シェルスクリプトをテスト" class="wp-image-3589"/></figure>
</div>


<p>その他にも<strong>if</strong>と<strong>テストコマンド</strong>でよく使用するオプションをご紹介しておきます。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><thead><tr><th>オプション</th><th>説明</th></tr></thead><tbody><tr><td>-e</td><td>ファイルが存在する</td></tr><tr><td>-f</td><td>ファイルである</td></tr><tr><td>-d</td><td>ディレクトリである</td></tr><tr><td>-n</td><td>文字列が0でない</td></tr><tr><td>-z</td><td>文字列が0である</td></tr><tr><td>-v</td><td>変数が定義されている</td></tr><tr><td>-r</td><td>ファイルが存在し、読み取り可能である</td></tr><tr><td>-w</td><td>ファイルが存在し、書き込み可能である</td></tr><tr><td>-x</td><td>ファイルが存在し、実行可能である</td></tr><tr><td>-h</td><td>ファイルが存在し、シンボリックリンクである</td></tr><tr><td>-s</td><td>ファイルが存在し、サイズが0より大きい</td></tr></tbody></table><figcaption class="wp-element-caption">よく使用するシェルスクリプトテストオプション一覧</figcaption></figure>



<h2 class="wp-block-heading">ifを使いファイルが存在しないかで場合分けする方法</h2>



<p>おまけで逆のオプションを指定する方法もお伝えしておきます。</p>



<p>今回で言うと<strong><span class="swl-bg-color has-swl-deep-01-background-color">ファイルが存在しない場合</span></strong>はどのように記述するかです。</p>



<p>否定系の記述は「!」を前につけると可能です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>if [ ! -e ファイル名 ]; then
 処理するコマンド
fi</code></pre></div>



<p>先程のシェルスクリプトを以下に変えます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>if [ ! -e file_exist.txt ]; then
 echo &#39;ファイルは存在しません&#39;
else
 echo &#39;ファイルは存在します&#39;
fi</code></pre></div>



<p>ではこちらも実際にシェルスクリプトを実行してみます。</p>



<p>シェルスクリプトの実行結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# . file_exist.sh
ファイルは存在しません</code></pre></div>



<p>成功しました。</p>



<h2 class="wp-block-heading">【応用】ifを使い業務などで実際使用するシェルスクリプトご紹介</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="560" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg" alt="業務で使用できるシェルスクリプト" class="wp-image-4561" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-1024x560.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-300x164.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4-768x420.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-4.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>では業務などで実際使用できる例もご紹介しておきます。</p>



<p>以下のシェルスクリプトを記述してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

file=file_exist.txt
if [ -n &quot;$file&quot; ] && [ ! -e &quot;$file&quot; ]; then
 touch &quot;$file&quot;
 echo &#39;ファイルが存在していないので作成しました&#39;
else
 echo &#39;ファイル名が空か存在しているので何もしませんでした&#39;
fi
</code></pre></div>



<p>ファイル名が空でない、かつファイルが存在しない場合のみファイルを作成するシェルスクリプトです。</p>



<h3 class="wp-block-heading">応用のシェルスクリプトの説明</h3>



<p>「file」という変数にファイル名を代入しています。</p>



<p>「[」コマンドのオプション「-n」で<span class="swl-inline-color has-swl-deep-01-color"><strong>ファイル名が空でない事</strong></span>を判定しています。</p>



<p class="is-style-icon_pen">※こればなければ「file」変数が空の場合でも判定が真となり、ファイルは作成されないが「作成しました」と表示されてしまう</p>



<p>「&amp;&amp;」でファイル名が空でなければ次の判定に行きます。file変数に「file_exist.txt」が入っており真なので右の判定に行きます</p>



<p>「! -e」オプションで<strong><span class="swl-inline-color has-swl-deep-01-color">ファイルが存在しない</span></strong><span class="swl-inline-color has-swl-deep-01-color"><strong>事</strong></span>を判定しています。</p>



<p>これで<span class="swl-marker mark_orange"><strong>ファイルが存在していなければ作成、存在していれば何もしない</strong></span>と言った処理が可能になります。</p>



<p>実際にシェルスクリプトを実行して見ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# . file_exist.sh
ファイルが存在していないので作成しました</code></pre></div>



<p>ファイルが作成されたので「ls」コマンドで確認してみましょう。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# ls
file_exist.sh  file_exist.txt</code></pre></div>



<p>「file_exist.txt」ファイルが存在する事が確認出来ました。</p>



<p>では先程のシェルスクリプトをもう一回実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost file_exist]# . file_exist.sh
ファイル名が空か存在しているので何もしませんでした</code></pre></div>



<p>今度はファイルが存在するので、何もしないメッセージが表示されました。</p>



<h2 class="wp-block-heading">シェルスクリプトのifを使いファイルが存在するかで場合分けする方法まとめ</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="586" src="https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-1024x586.jpg" alt="ファイルが存在" class="wp-image-4332" srcset="https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-1024x586.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-300x172.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-768x439.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3.jpg 1191w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">シェルスクリプトでifを使用してファイルが存在するか</span></strong>どうかを判定するには以下を使用します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>if [ -e ファイル名 ]; then
 処理するコマンド
fi</code></pre></div>



<p>その他にもよく使用するオプションは以下でした。</p>



<figure class="wp-block-table td_to_th_"><table class="has-fixed-layout"><thead><tr><th>オプション</th><th>説明</th></tr></thead><tbody><tr><td>-e</td><td>ファイルが存在する</td></tr><tr><td>-f</td><td>ファイルである</td></tr><tr><td>-d</td><td>ディレクトリである</td></tr><tr><td>-n</td><td>文字列が0でない</td></tr><tr><td>-z</td><td>文字列が0である</td></tr><tr><td>-v</td><td>変数が定義されている</td></tr><tr><td>-r</td><td>ファイルが存在し、読み取り可能である</td></tr><tr><td>-w</td><td>ファイルが存在し、書き込み可能である</td></tr><tr><td>-x</td><td>ファイルが存在し、実行可能である</td></tr><tr><td>-h</td><td>ファイルが存在し、シンボリックリンクである</td></tr><tr><td>-s</td><td>ファイルが存在し、サイズが0より大きい</td></tr></tbody></table><figcaption class="wp-element-caption">よく使用するシェルスクリプトテストオプション一覧</figcaption></figure>



<p>業務でも使用できるファイルが存在するかどうかで場合分けするシェルスクリプトなどを紹介しました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>#!/bin/bash

file=file_exist.txt
if [ -n &quot;$file&quot; ] && [ ! -e &quot;$file&quot; ]; then
 touch &quot;$file&quot;
 echo &#39;ファイルが存在していないので作成しました&#39;
else
 echo &#39;ファイル名が空か存在しているので何もしませんでした&#39;
fi
</code></pre></div>



<p>シェルスクリプトでifを使用してファイルが存在するかどうかで、場合分けする時に参考になると嬉しいです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shell-script-file-exist/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
