claude-sonnet-4-20250514-thinking-32k· Anthropic LMArena Human Preference 140K
On the Linux command line, for a list of files, I need to check if the file exists,…
- User
On the Linux command line, for a list of files, I need to check if the file exists, separate the file name from the directory path, get the file extension and the file's mtime. The result will be a CSV file with the columns: exists,path,file name,extension,mtime
- Assistant
Here's a bash script to process a list of files and generate the CSV output you need:
#!/bin/bash # Function to process a single file process_file() { local file="$1" local exists="false" local path="" local filename="" local extension="" local mtime="" # Check if file exists if [ -f "$file" ]; then exists="true" # Get directory path path=$(dirname "$file") # Get filename filename=$(basename "$file") # Get extension (everything after the last dot) extension="${filename##*.}" # If no extension found, set to empty if [ "$extension" = "$filename" ]; then extension="" fi # Get mtime (modification time) mtime=$(stat -c "%Y" "$file" 2>/dev/null || stat -f "%m" "$file" 2>/dev/null) else # File doesn't exist, but we can still extract path and filename path=$(dirname "$file") filename=$(basename "$file") extension="${filename##*.}" if [ "$extension" = "$filename" ]; then extension="" fi mtime="" fi # Output CSV line echo "$exists,$path,$filename,$extension,$mtime" } # Print CSV header echo "exists,path,file name,extension,mtime" # Process files from arguments or stdin if [ $# -gt 0 ]; then # Process files from command line arguments for file in "$@"; do process_file "$file" done else # Process files from stdin (one per line) while IFS= read -r file; do process_file "$file" done fiUsage examples:
- From command line arguments:
./script.sh /path/to/file1.txt /home/user/file2.log /etc/config.conf- From a file containing the list:
./script.sh < file_list.txt # or cat file_list.txt | ./script.sh- Direct output to CSV file:
./script.sh /path/to/file1.txt /path/to/file2.txt > output.csvOne-liner version (if you prefer a command-line approach):
for file in "$@"; do if [ -f "$file" ]; then echo "true,$(dirname "$file"),$(basename "$file"),${$(basename "$file")##*.},$(stat -c "%Y" "$file" 2>/dev/null || stat -f "%m" "$file" 2>/dev/null)" else echo "false,$(dirname "$file"),$(basename "$file"),${$(basename "$file")##*.}," fi doneNotes:
- The script handles both existing and non-existing files
- The
statcommand uses-c "%Y"for Linux and-f "%m"for macOS/BSD - Extensions are extracted as everything after the last dot
- Files without extensions will have an empty extension field
- The mtime is in Unix timestamp format (seconds since epoch)
If you need the mtime in a human-readable format, replace the stat command with:
mtime=$(stat -c "%y" "$file" 2>/dev/null || stat -f "%Sm" "$file" 2>/dev/null)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.