backing up WSL by checking date/time of file in WSL via Command Prompt

N M 0 Reputation points
2024-09-15T11:58:04.58+00:00

is there a way to check the date of a file located in WSL via the Command Prompt? I would like to initiate a backup of WSL through a batch file if the .bash_history file has been modified

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,413 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Neuvi Jiang 1,300 Reputation points Microsoft Vendor
    2024-09-17T06:18:33.0566667+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.