Skip to content
The Chat Archive
qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K

On the Linux command line, for a list of files, I need to check if the file exists,…

  1. 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

  2. Assistant

    You can achieve this using a shell script (e.g., in bash) that processes a list of files, checks whether each file exists, and extracts the required information. Here's a complete example:

    ✅ Requirements:

    • Input: A list of file paths (can be absolute or relative).
    • Output: A CSV file with columns: exists,path,file name,extension,mtime.

    📜 Script: file_info_to_csv.sh

    #!/bin/bash
    
    # Input file containing list of file paths, one per line
    INPUT="file_list.txt"
    
    # Output CSV file
    OUTPUT="file_info.csv"
    
    # Write CSV header
    echo "exists,path,file name,extension,mtime" > "$OUTPUT"
    
    # Process each file
    while IFS= read -r file; do
      # Skip empty lines
      [ -z "$file" ] && continue
    
      # Check if file exists
      if [ -e "$file" ]; then
        exists="true"
        # Get components
        dir_path="$(dirname -- "$file")"
        file_name="$(basename -- "$file")"
        extension="${file_name##*.}"
        mtime=$(date -r "$file" +"%Y-%m-%d %T")
      else
        exists="false"
        dir_path="$(dirname -- "$file")"
        file_name="$(basename -- "$file")"
        extension="${file_name##*.}"
        mtime=""
      fi
    
      # Escape quotes and commas in file names (basic handling)
      dir_path=$(printf '%s' "$dir_path" | sed 's/"/""/g')
      file_name=$(printf '%s' "$file_name" | sed 's/"/""/g')
    
      # Write to CSV
      printf '"%s","%s","%s","%s","%s"\n' \
        "$exists" \
        "$dir_path" \
        "$file_name" \
        "$extension" \
        "$mtime" >> "$OUTPUT"
    
    done < "$INPUT"
    

    📋 Example Input File (file_list.txt)

    /path/to/file1.txt
    another/file2.jpg
    missing_file.log
    ~/Documents/report.pdf
    

    🧾 Sample Output (file_info.csv)

    exists,path,file name,extension,mtime
    true,/path/to,file1.txt,txt,2024-12-25 10:30:45
    true,another,file2.jpg,jpg,2024-12-24 09:15:22
    false,,missing_file.log,log,
    true,/home/user/Documents,report.pdf,pdf,2024-12-23 16:44:33
    

    🛠️ Usage Instructions

    1. Save the script as file_info_to_csv.sh.

    2. Make it executable:

      chmod +x file_info_to_csv.sh
      
    3. Prepare your input file (file_list.txt) with one file path per line.

    4. Run the script:

      ./file_info_to_csv.sh
      
    5. The resulting CSV will be saved as file_info.csv.


    Let me know if you want this done in Python instead!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.