WSLでローカルのWindowsディレクトリを正しく設定する方法は?

WSLでローカルのWindowsディレクトリを正しく設定する方法は?

Linux用のWindowsサブシステムのコマンドラインからこのスクリプトを実行しました。最新のファイルを検索したいのですが、「該当するファイルやディレクトリがありません」というエラーが発生します。スクリプトを機能させるにはどうすればよいのか疑問に思います。

これは私のスクリプトです。

# Set the path to the directory where you want to search for the latest file
directory="C:/Users/nguyen_q/Downloads/Test files/*.csv"

# Get the current date in the format YYYY-MM-DD
echo CURRENT_DATE=$(date +%Y%m%d) 

# Set the variable to hold the name of the latest file
latest_file="" 

# Find all files in the specified directory whose name starts with "MNSUP"
echo files="$(find "$directory" -maxdepth 1 -name "MNSUP*")" 

# Sort the files by modified time, with the most recently modified file first
echo files="$(ls -t $files)" 

# Set the first file in the list as the latest file
echo latest_file="$(echo "$files" | head -n 1)" 

# Check if the latest file contains the current date in the file name
if [[ "$latest_file" == *"$CURRENT_DATE"*.csv ]]; then
  # Print a message if the latest file matches the current date
   echo "$latest_file TODAYYYYY."

else
  # Print a message if the latest file does not match the current date
  echo "$latest_file"

  # Send an email to the user
  echo "The latest file is not present and was not sent to MN.Please re run the job" | mail -s "Latest File Error" "[email protected]"    
fi 

# Print the latest file
# echo "The latest file currently in the folder is: $latest_file"```

ベストアンサー1

短い要約:

変更:

directory="C:/Users/nguyen_q/Downloads/Test files/*.csv"

到着

directory="/mnt/c/Users/nguyen_q/Downloads/Test files/*.csv"

説明する:

スクリプト全体をテストしないと、このNo such file or directoryメッセージは初期findコマンドからのものであることがほぼ確実です。

問題はWSLを実行するときLinux、Windowsとはまったく異なる方法でファイルやディレクトリにアクセスします。

directory="C:/Users/nguyen_q/Downloads/Test files/*.csv"

...Windows パスです。 Linuxにはドライブの概念はC:\ありません。C:/Windowsバックスラッシュをスラッシュに変換しようとしたことを確認しました。これは良いスタートですが、パスはまだ機能しません。

これをテストするのが最も簡単ですいいえスクリプトを使用するには、Bashシェルで次のコマンドを実行します。

directory="C:/Users/nguyen_q/Downloads/Test files/*.csv"
ls $directory

それでもNo such file or directoryエラーが発生します。

あなたがしなければならないことは、以下を介してディレクトリにアクセスすることです。WSLは、たとえば各Windowsドライブに対して自動的に作成されますC:\。これは(デフォルトでは)次のとおりです。

/mnt/c

したがって、次のことを試してください。

directory="/mnt/c/Users/nguyen_q/Downloads/Test files/*.csv"
ls $directory

これはうまくいきます。その後、それに応じてスクリプトを更新できます。

注: Windowsドライブのファイルは現在WSL2からアクセスできます。かなりLinux ext4ファイルシステムでWSLによって直接生成されたファイルよりも遅い。

より多くの情報を知りたい場合:

おすすめ記事