<?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%E5%90%8D%E5%8F%96%E5%BE%97/feed/" rel="self" type="application/rss+xml" />
	<link>https://sugublog.org</link>
	<description>ブログ運営について発信中</description>
	<lastBuildDate>Thu, 07 Mar 2024 09:15:27 +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>【bat基礎】ファイル名を取得する様々な方法！簡単に取得可能です</title>
		<link>https://sugublog.org/bat-filename-get/</link>
					<comments>https://sugublog.org/bat-filename-get/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Sat, 25 Nov 2023 18:07:06 +0000</pubDate>
				<category><![CDATA[bat]]></category>
		<category><![CDATA[バッチファイル]]></category>
		<category><![CDATA[ファイル名取得]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5992</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-19-1024x503.jpg" class="webfeedsFeaturedVisual" /></p>batファイルでファイル名を取得する方法は様々あります。実行しているbatファイル名やカレントディレクトリにあるファイル名取得なども可能です。 今回はその方法を現役エンジニアの筆者が詳しく説明します！ %0やforなどを [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-19-1024x503.jpg" class="webfeedsFeaturedVisual" /></p><div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="blue"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/02/cropped-dog003-150x150.png" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">batファイルでファイル名取得したい人</span></div><div class="c-balloon__body -thinking -border-on"><div class="c-balloon__text">
<p>batファイルでファイル名を取得する方法が分かりません。実行しているファイルやカレントディレクトリにあるファイル名を取得したい！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-fz u-fz-l">batファイル</span></span></strong>で<strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-fz u-fz-l">ファイル名を取得</span></span></strong>する方法は様々あります。実行しているbatファイル名やカレントディレクトリにあるファイル名取得なども可能です。</p>



<p>今回はその方法を現役エンジニアの筆者が詳しく説明します！</p>



<p class="is-style-icon_info">%0やforなどを使用すれば取得可能です。</p>


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


<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">batでファイル名を取得する様々な方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="410" src="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-2-1024x410.jpg" alt="ファイル名取得" class="wp-image-4559" srcset="https://sugublog.org/wp-content/uploads/2021/11/programing_1122-2-1024x410.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-2-300x120.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-2-768x307.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/programing_1122-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-fz u-fz-l">ファイル名を取得する方法</span></span></strong>ですが２パターンあります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batファイルで２パターンのファイル名を取得する</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>実行しているbatファイル名を取得する</li>



<li>カレントディレクトリのファイル名を取得する</li>
</ul>
</div></div>



<p>このどちらも詳しく説明していきます。</p>



<h2 class="wp-block-heading">実行しているbatファイル名を取得する簡単な方法</h2>



<p>まずは<strong><span class="swl-inline-color has-swl-deep-01-color">実行しているbatファイル名を取得</span></strong>する方法です。</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/07/analytics-2-300x157.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/bat-execution/">【bat】バッチを実行する方法！実行できない時の対処法などをご紹介</a>
						<span class="p-blogCard__excerpt">batファイルが既にありそれを実行してと言われたけど、どうやって実行すればわからない人の悩みに答えます。 またbatを実行しようとしたけど、できなかった時の原因と対&#8230;</span>					</div>
				</div>
			</div>
		</div>


<p class="is-style-icon_info">実行しているファイル名取得する方法は%0を使用します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off
echo %0
pause</code></pre></div>



<p>注意点としてbatファイルを実行するパスにより%0に入る値が変化します。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>batファイルの実行のやり方</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>batファイルをダブルクリックして実行する場合</li>



<li>batファイルが存在するパスまで移動して実行する場合</li>
</ul>
</div></div>



<h3 class="wp-block-heading">batファイルをダブルクリックして実行する場合</h3>



<p><strong><span class="swl-bg-color has-swl-deep-02-background-color">batファイルをダブルクリック</span></strong>すると、ユーザーのホームディレクトリから実行されるので%0に入る<span class="swl-marker mark_blue">batファイル名は絶対パスを含みます。</span></p>



<p>batファイルをダブルクリックした結果</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>&quot;C:\script\bat\18_ファイル名取得\ファイル名取得.bat&quot;</code></pre></div>



<h3 class="wp-block-heading">batファイルが存在するパスまで移動して実行する場合</h3>



<p><span class="swl-bg-color has-swl-deep-03-background-color">batファイルのパスまで移動して実行する時</span>は、%0には<span class="swl-marker mark_green">batファイル名のみ取得</span>されます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>C:\script\bat\18_ファイル名取得&gt;ファイル名取得.bat
ファイル名取得.bat</code></pre></div>



<h3 class="wp-block-heading">batファイルをダブルクリックしてファイル名のみ取得する方法</h3>



<p>batファイルをダブルクリックして実行する人も多いと思います。その時にbatファイル名のみ取得する方法はないのでしょうか？</p>



<p class="has-border -border02">その時は<strong>「%~nx0」</strong>を指定すれば<strong><span class="swl-inline-color has-swl-deep-01-color">batファイル名</span></strong>のみ取得できます。</p>



<p class="is-style-icon_book">「%~n0」はファイル名のみ、「%~x0」は識別子のみ、二つ合わせて「%~nx0」で取得できます。　</p>



<p>「%~n0」を使用したbatファイルを実行した結果、ファイル名のみ取得できています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>ファイル名取得.bat</code></pre></div>



<p>その他にもbatファイルの様々な情報を取得できますので、簡単にまとめておきます。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>オプション</th><th>意味</th></tr></thead><tbody><tr><td>%~d0</td><td>バッチファイルのドライブ名</td></tr><tr><td>%~p0</td><td>バッチファイルのパス</td></tr><tr><td>%~dp0</td><td>バッチファイルのドライブ名＋パス</td></tr><tr><td>%~f0</td><td>バッチファイル名を含めたパス</td></tr><tr><td>%~n0</td><td>バッチファイル名</td></tr><tr><td>%~x0</td><td>バッチファイルの識別子</td></tr><tr><td>%~nx0</td><td>バッチファイル名（識別子含む）</td></tr><tr><td>%~s0</td><td>バッチファイル名を含めたパス（省略形）</td></tr><tr><td>%~a0</td><td>バッチファイルの属性</td></tr><tr><td>%~t0</td><td>バッチファイルの更新日時</td></tr><tr><td>%~z0</td><td>バッチファイルの容量（バイト）</td></tr></tbody></table><figcaption class="wp-element-caption">その他の項目を取得するオプション一覧</figcaption></figure>



<p class="is-style-icon_pen">上でまとめた詳しい情報は以下の記事にまとめていますので、興味のある人はどうぞ</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/11/Server_20211109-16-300x157.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/bat-argument/">【bat】バッチファイルで引数を取得する方法!スペース含む時なども解説</a>
						<span class="p-blogCard__excerpt">バッチファイルを使用するとWindowsで色んな事が楽に出来ます。更にバッチファイルで引数も取得して扱えたら便利になると思いませんか？ そこで今回はバッチファイルで&#8230;</span>					</div>
				</div>
			</div>
		</div>


<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<h2 class="wp-block-heading">カレントディレクトリのファイル名を取得する方法</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="575" src="https://sugublog.org/wp-content/uploads/2021/11/Web_site_1114-1024x575.jpg" alt="ファイル名取得" class="wp-image-4506" srcset="https://sugublog.org/wp-content/uploads/2021/11/Web_site_1114-1024x575.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Web_site_1114-300x169.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Web_site_1114-768x431.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Web_site_1114.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">カレントディレクトリにあるファイル名を取得</span></strong>したい場合もあるかと思います。</p>



<p>forを使用してファイル名を取得する方法です。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off

for %%i in (*) do echo %%i
pause</code></pre></div>



<p>for文の（*）は<span class="swl-bg-color has-swl-deep-02-background-color">カレントディレクトリのファイル名全て</span>が入ります。</p>



<p>フォルダ構成は以下としてbatファイルを実行してみます。</p>


<div class="wp-block-image is-style-browser_mac">
<figure class="aligncenter size-full"><img decoding="async" width="707" height="331" src="https://sugublog.org/wp-content/uploads/2023/11/bat-for-filename-capture.png" alt="" class="wp-image-5996" srcset="https://sugublog.org/wp-content/uploads/2023/11/bat-for-filename-capture.png 707w, https://sugublog.org/wp-content/uploads/2023/11/bat-for-filename-capture-300x140.png 300w" sizes="(max-width: 707px) 100vw, 707px" /></figure>
</div>


<p>実行した結果は以下となりました。カレントディレクトリのみにあるファイル名を取得しています。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>ファイル名取得.bat
ファイル１.txt
ファイル２.txt
続行するには何かキーを押してください . . .</code></pre></div>



<p>またカレントディレクトリ配下の<span class="swl-bg-color has-swl-deep-03-background-color">サブフォルダ</span>も対象にしたい場合は<span class="swl-marker mark_green">「/r」オプション</span>を使用します。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>@echo off

for /r %%i in (*) do echo %%i
pause</code></pre></div>



<p>これでカレントディレクトリ配下のファイル名も取得できますので実行してみます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>C:\script\bat\18_ファイル名取得\ファイル名取得.bat
C:\script\bat\18_ファイル名取得\ファイル１.txt
C:\script\bat\18_ファイル名取得\ファイル２.txt
C:\script\bat\18_ファイル名取得\sub_dir\sub_file_1.txt
C:\script\bat\18_ファイル名取得\sub_dir\sub_file_2.txt
続行するには何かキーを押してください . . .</code></pre></div>



<p>ファイル名を取得できました。</p>



<p>forで様々な情報を取得できるので詳しくは以下の記事を参考にしてください。</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/07/PC007-5-300x200.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/windows-bat-for-base/">【bat】バッチで繰り返しを行う！forコマンドの使用方法をご説明</a>
						<span class="p-blogCard__excerpt">バッチファイルを使用しますが、forコマンドが上手くいきません そのような悩みを持っている人も多いのではないでしょうか？ bat（バッチファイル）で繰り返しを行いた&#8230;</span>					</div>
				</div>
			</div>
		</div>

<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="blue"><div class="c-balloon__icon -square"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://sugublog.org/wp-content/uploads/2021/03/dog004-150x150.jpeg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://sugublog.org/wp-content/uploads/2021/03/dog004-150x150.jpeg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">バッチでファイル名取得が出来る様になった人</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>batファイルでファイル名取得できるようになったので、バッチファイルで出来る事が多くなりました！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/bat-filename-get/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【シェルスクリプト】ファイル名を取得する方法！エラー対処法あり</title>
		<link>https://sugublog.org/shell-script-get-filename/</link>
					<comments>https://sugublog.org/shell-script-get-filename/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[suguru]]></dc:creator>
		<pubDate>Mon, 24 Apr 2023 07:03:14 +0000</pubDate>
				<category><![CDATA[シェルスクリプト]]></category>
		<category><![CDATA[ファイル名取得]]></category>
		<guid isPermaLink="false">https://sugublog.org/?p=5510</guid>

					<description><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-1024x586.jpg" class="webfeedsFeaturedVisual" /></p>シェルスクリプトでファイル名を取得して使用する場合も多くあります。 そんな時どうすれば良いのかを記事にしました。 またエラーが出た時の体験や、ディレクトリのみ取得したりと付加価値のある記事にしました。 実行した結果も詳し [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://sugublog.org/wp-content/uploads/2021/11/DB_1103-3-1024x586.jpg" class="webfeedsFeaturedVisual" /></p>
<p><span class="swl-marker mark_orange">シェルスクリプトで<strong><span class="swl-inline-color has-swl-deep-01-color">ファイル名を取得</span></strong>して使用する場合も多くあります。</span></p>



<p>そんな時どうすれば良いのかを記事にしました。</p>



<p>またエラーが出た時の体験や、ディレクトリのみ取得したりと付加価値のある記事にしました。</p>


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


<p>実行した結果も詳しく載せているので、この記事を読めば<span class="swl-bg-color has-swl-deep-01-background-color">シェルスクリプトでファイル名を取得する方法が簡単に理解できます。</span></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">シェルスクリプトで変数からファイル名を取得する方法！</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="505" src="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-4-1024x505.jpg" alt="ファイル取得" class="wp-image-4411" srcset="https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-4-1024x505.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-4-300x148.jpg 300w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-4-768x379.jpg 768w, https://sugublog.org/wp-content/uploads/2021/11/Server_20211109-4.jpg 1188w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p>ファイルを取得する方法は複数あります。</p>



<p>主に以下の方法があります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトでファイル名を取得する方法2選</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>「${変数##*/}」でファイル名取得する</li>



<li>「basename」コマンドでファイル名を取得する</li>
</ul>
</div></div>



<h2 class="wp-block-heading">変数から一部を抽出してファイル名を取得する</h2>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="717" src="https://sugublog.org/wp-content/uploads/2021/08/DB0804-3-1024x717.jpg" alt="ファイルを取得" class="wp-image-3842" srcset="https://sugublog.org/wp-content/uploads/2021/08/DB0804-3-1024x717.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-3-300x210.jpg 300w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-3-768x538.jpg 768w, https://sugublog.org/wp-content/uploads/2021/08/DB0804-3.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


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



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

file=&quot;/var/log/test1&quot;
echo ${file##*/}
</code></pre></div>



<h3 class="wp-block-heading">変数から一部を抽出してファイル名を取得するコード説明</h3>



<p>「file」という変数に絶対パスで「test1」というファイルを入れています。</p>



<p>変数の一部を取得してファイル名だけを表示したい時は、<strong><span class="swl-inline-color has-swl-deep-01-color">「${file##*/}」</span></strong>を使用します。それを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/12/PC教室20211216-3-300x232.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-dollar-mean/">【シェルスクリプト】「$」の意味を詳しく解説！実例あり</a>
						<span class="p-blogCard__excerpt">シェルスクリプトで「$」はよく出てきますが、どういう意味なのかわからないと言った事はないでしょうか？ シェルスクリプトで「$」はよく出てきますが、どういう意味な&#8230;</span>					</div>
				</div>
			</div>
		</div>

<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 class="is-style-icon_info">絶対パスの前から「/」を探して最長一致（最後に見つかった）の「/」以降を表示してくれます。</p>



<p>つまりディレクトリは省いてファイル名のみ取得してくれます。</p>



<p>変数から一部を抽出してファイル名を取得するスクリプトを、実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-lang="Bash"><code>[root@localhost workspace]# bash get-filename.sh
test1</code></pre></div>



<p class="is-style-balloon_box2">シェルスクリプトを実行する方法の詳細は、以下の記事を参考にしてください。</p>



<p class="is-style-bg_stripe"><a href="https://sugublog.org/shell-script-execution/" target="_blank" rel="noreferrer noopener">シェルスクリプトを実行する様々な方法の記事</a></p>



<p class="is-style-icon_good">今回のファイル名の「test1」が取得出来ています。</p>



<h3 class="wp-block-heading">変数から一部を抽出するその他のシェルスクリプト</h3>



<p>変数の一部を取得するその他のコードを以下にまとめておきます。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>シェルスクリプト</th><th>最〇一致</th><th>どこから探すか</th></tr></thead><tbody><tr><td>${変数##*/}</td><td>最長一致</td><td>前から「/」を探す</td></tr><tr><td>${変数#*/}</td><td>最短一致</td><td>前から「/」を探す</td></tr><tr><td>${変数%%/*}</td><td>最長一致</td><td>後ろから「/」を探す</td></tr><tr><td>${変数%/*}</td><td>最短一致</td><td>後ろから「/」を探す</td></tr><tr><td>${変数#文字列}</td><td>最短一致</td><td>文字列以降が展開</td></tr></tbody></table><figcaption class="wp-element-caption">変数の一部を取得するその他のシェルスクリプト一覧</figcaption></figure>



<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-9330904718533231"
     crossorigin="anonymous"></script>
<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-9330904718533231"
     data-ad-slot="8455594968"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p class="is-style-icon_book">変数の一部を取得するその他のコードについて詳しく知りたいあなたは以下の記事を参照下さい。</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/07/script111-300x175.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/shellscript-cutting-and-extracting-variable-strings/">【シェルスクリプト】変数の文字列を切り出して抽出する方法！</a>
						<span class="p-blogCard__excerpt">shellscriptで変数の少し違う文字列を別の変数を作成して使用していると、変数管理が複雑化するので何とかしたい そういった時に変数の文字列を切り出して抽出する事で&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h3 class="wp-block-heading">変数から一部を抽出してディレクトリのみ取得する</h3>



<p><span class="swl-marker mark_orange">これの応用で<strong>ファイル名ではなくディレクトリのみ</strong>取得してみましょう。</span></p>



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

file=&quot;/var/log/test1&quot;
echo ${file%/*}</code></pre></div>



<p>上のシェルスクリプトを記述して実行して見ます。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost workspace]# bash get-directory.sh
/var/log</code></pre></div>



<p>ディレクトリのみ取得出来ています。</p>



<h3 class="wp-block-heading">変数から一部を抽出してファイル名を取得する時のエラー実体験</h3>



<p>ファイル名を取得する時に<span class="swl-bg-color has-swl-deep-01-background-color">エラー</span>がでた事があったので紹介しておきます。</p>



<p>以下のコードでシェルスクリプトを実行してみます。</p>



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

file=&quot;/var/log/test1&quot;
echo ${file##/*}</code></pre></div>



<p>実行した結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost workspace]# bash get-filename_error.sh
/var/log/test1</code></pre></div>



<p>ファイル名が取得できていません。</p>



<p>よく見ると<span class="swl-bg-color has-swl-deep-01-background-color">「${file##/*}」</span>になっています。正解は<span class="swl-bg-color has-swl-deep-03-background-color">「${file##*/}」</span>です。</p>



<p>「ディレクトリ/ファイル名」の構造になっている為「/*」でファイル名が取得できると思っていましたが逆でした。</p>



<h2 class="wp-block-heading">「basename」コマンドでファイル名を取得する</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>


<p><strong><span class="swl-inline-color has-swl-deep-01-color">「basename」コマンドでファイル名を取得する</span></strong>コードを、以下の様に記述します。</p>



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

file=&quot;/var/log/test1&quot;
echo $(basename $file)</code></pre></div>



<h3 class="wp-block-heading">「basename」コマンドでファイル名を取得するコードの説明</h3>



<p>同じく変数に絶対パスでファイル名を指定しています。</p>



<p>「$()」とする事で、カッコの中のコマンドの実行結果を表示する事ができます。</p>



<p><span class="swl-marker mark_orange">カッコの中に「basename」コマンドと引数の変数を入れています。</span></p>



<p>「basename」コマンドでファイル名を取得するシェルスクリプトを実行すると、以下の結果となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost workspace]# bash get-filename.sh
test1</code></pre></div>



<p>結果同じくファイル名の「test1」を取得できました。</p>



<h3 class="wp-block-heading">コマンドでディレクトリのみ取得する</h3>



<p>同じくコマンドでディレクトリのみ取得してみます。</p>



<p class="is-style-icon_announce">「dirname」コマンドを使用します。</p>



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



<div class="hcb_wrap"><pre class="prism line-numbers lang-bash" data-file="get-directory-dirname.sh" data-lang="Bash"><code>#!/bin/bash

file=&quot;/var/log/test1&quot;
echo $(dirname $file)</code></pre></div>



<p>結果は以下となりました。</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>[root@localhost workspace]# bash get-directory-dirname.sh
/var/log</code></pre></div>



<p>「var/log」とディレクトリのみ取得する事が出来ました。</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">シェルスクリプトで変数からファイル名を取得する方法！まとめ</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/09/moving_0926-2-1024x683.jpg" alt="ファイル取得" class="wp-image-4208" srcset="https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2-1024x683.jpg 1024w, https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2-300x200.jpg 300w, https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2-768x512.jpg 768w, https://sugublog.org/wp-content/uploads/2021/09/moving_0926-2.jpg 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<p><strong><span class="swl-inline-color has-swl-deep-01-color"><span class="swl-marker mark_orange">シェルスクリプトで絶対パスのファイル名のみ取得する方法</span></span></strong>は以下の方法があります。</p>



<div class="swell-block-capbox cap_box is-style-onborder_ttl"><div class="cap_box_ttl"><span>シェルスクリプトでファイル名を取得する方法2選</span></div><div class="cap_box_content">
<ul class="wp-block-list">
<li>「${変数##*/}」でファイル名取得する</li>



<li>「basename」コマンドでファイル名を取得する</li>
</ul>
</div></div>



<p>ちなみにシェルスクリプトを実行しているファイル名を参照する時は「$0」を使用します。</p>



<p>変数の箇所を「$0」にすれば同じ事が可能です。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sugublog.org/shell-script-get-filename/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
