1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LinuxからWindowsへファイルをコピー(スクリプト)

Last updated at Posted at 2025-09-19

準備

①Samba clientのインストール

yum install samba-client

②Windowsに共有フォルダを作成

単体ファイルコピー

1つのファイルのみをコピーする方法を紹介します。
接続先はWindows側のホスト名またはIPアドレスを指定してください。
ユーザー名、パスワードはWindows側で設定した共有フォルダにアクセス可能な情報を指定してください。

smbclient //windows_server/share -U <ユーザー名>%<パスワード> -c "put <Linux側のファイルパス>◯◯.txt <Windows側のファイルパス>◯◯.txt"

注意事項として、Windows側のファイルパスは円マーク(\)で区切ります。(Linux側はスラッシュ)

複数ファイルコピー

複数ファイルの場合は「mput」コマンドを使用します。
単体ファイルコピーと同様に「-c」の後ろにコマンドを記述する方法で紹介している記事が多いのですが、私の環境では上手く動作しなかったので、下記方法でスクリプトを組みました。

smbclient //windows_server/share -U <ユーザー名>%<パスワード> <<EOF
cd "<Windows側のファイルパス>"
lcd "<Linux側のファイルパス>"
prompt OFF
mput *
quit
EOF

2行目の「cd」でファイルをコピーしたい階層まで移動(share直下でいい場合は省いてOK)
3行目の「lcd」でコピーしたい複数ファイルが格納されているディレクトリを指定
4行目の「prompt OFF」はファイルコピー時の確認を省く為に記述しています。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?