Hi N M,
Thank you for posting in the Q&A Forums.
@echo off
setlocal
:: Set the path to the user's home directory in WSL (modify to suit your situation)
set WSL_HOME=/home/yourusername
:: Set the backup directory in Windows
set BACKUP_DIR=C:\path\to\your\backup\directory
:: Use the wsl command to execute the ls command to get the modification date of .bash_history
:: Note: This is just a demonstration of how to get the date, the ls command does not actually display the date of modification directly.
:: You can use the stat command to get more detailed information about a file
for /f “delims=” %%i in ('wsl stat -c %y “%WSL_HOME%/.bash_history”') do set FILE_DATE=%%i
echo The modification date of .bash_history is: %FILE_DATE%
:: You can add conditions to check if a file has been modified, if needed.
:: Here is the direct backup operation
xcopy /Y “%WSL_HOME%.bash_history” “%BACKUP_DIR%.bash_history_backup”
echo Backup complete.
endlocal
The script above uses stat -c %y to get the year and month of the last modification of the file (this is a formatting option specific to the GNU stat command, which may not be applicable to all Linux distributions). You may need to adapt this command to your WSL distribution. For example, on some systems, you may need to use the date -r command or other methods to get the modification date.
The xcopy command is used to copy files from WSL to a Windows directory. Note that you cannot use WSL paths directly in the xcopy command because the paths in WSL are different from the paths in Windows. The script example above assumes that you already know the exact location of the .bash_history file in WSL and that you're using the wsl command to access it (but in reality, since xcopy is a Windows command, it doesn't work directly on WSL paths; the example here is mostly to illustrate the process). In practice, you may need to export the file from WSL to a Windows-accessible location first, and then copy it.
Best regards
NeuviJ
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.