Bird
0
0

You want to write a PowerShell on Linux script that lists all files in /var/log modified in the last 24 hours. Which approach is correct?

hard📝 Application Q8 of 15
PowerShell - Cross-Platform PowerShell
You want to write a PowerShell on Linux script that lists all files in /var/log modified in the last 24 hours. Which approach is correct?
A$files = Get-Content /var/log | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) }
B$files = ls /var/log | Where { $_.Modified -gt (Get-Date).AddHours(-24) }
C$files = Get-ChildItem /var/log -Filter '*.log' | Where-Object { $_.LastAccessTime -lt (Get-Date) }
D$files = Get-ChildItem -Path /var/log | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) }
Step-by-Step Solution
Solution:
  1. Step 1: Use Get-ChildItem with correct path and filter

    Get-ChildItem lists files; filtering by LastWriteTime property with Get-Date minus 1 day selects recent files.
  2. Step 2: Validate property and method usage

    LastWriteTime is the correct property; AddDays(-1) subtracts one day; Where-Object is correct filter cmdlet.
  3. Final Answer:

    $files = Get-ChildItem -Path /var/log | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-1) } -> Option D
  4. Quick Check:

    Filter by LastWriteTime with AddDays(-1) [OK]
Quick Trick: Use LastWriteTime and Get-Date.AddDays(-1) to filter recent files [OK]
Common Mistakes:
  • Using incorrect property names like Modified or LastAccessTime
  • Using ls alias without proper filtering
  • Using Get-Content instead of Get-ChildItem for files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes