Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash-preview-04-17· GoogleHugging Face LMArena Human Preference 140K

How do I get the file name without extension in the find command with -exec?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-mini-2025-04-14’s answer.
  1. User

    How do I get the file name without extension in the find command with -exec?

  2. Gemini Assistant

    Okay, getting the filename without the extension using find -exec requires executing a small shell command or script that can perform the string manipulation. You can achieve this in a few ways, primarily by calling sh -c to leverage shell features like basename and parameter expansion or tools like sed.

    Here are a couple of common and robust methods:

    Method 1: Using sh -c with basename and sed (Recommended for robustness)

    This method uses basename to get just the file part of the path and then sed to strip off the extension (the part starting with the first dot that isn't the first character). This is generally robust and handles files with multiple dots or leading dots (like .bashrc) well.

    find . -type f -exec sh -c 'basename "$1" | sed "s/\..*//"' _ {} \;
    
    • find . -type f: Finds files (-type f) starting from the current directory (.).
    • -exec ... \;: Executes the command for each found file.
    • sh -c '...': Runs a short script (...) in a new shell.
    • basename "$1" | sed "s/\..*//": This is the core manipulation:
      • basename "$1": Gets the filename part from the full path passed as the first argument ($1) to the sh -c script. The _ after sh -c '...' is a placeholder for $0 inside the script, and {} is passed as the first argument, becoming $1.
      • |: Pipes the output of basename to sed.
      • sed "s/\..*//": Substitutes (s/.../.../) the first dot (\.) followed by any characters (.*) with an empty string (//). This effectively removes the extension. It correctly handles names like .bashrc (doesn't remove the leading dot) and archive.tar.gz (removes .tar.gz, leaving archive).
    • _ {}: The _ is a dummy argument for $0 inside the sh -c script. {} is replaced by the current file path found by find and becomes the first positional argument ($1) to the sh -c script.

    Example Output:

    For files like:

    ./dir/file.txt
    ./another_file.tar.gz
    ./.bashrc
    ./just_a_name
    ./with.dot.in.name.log
    

    The output would be:

    file
    another_file
    .bashrc
    just_a_name
    with
    

    (Note: with.dot.in.name.log becomes with because it removes everything from the first dot).

    Method 2: Using sh -c with Shell Parameter Expansion (For removing only the last extension)

    If you want to remove only the last extension (.gz from .tar.gz), you can use shell parameter expansion ${var%.*}.

    find . -type f -exec sh -c 'b=$(basename "$1"); echo "${b%.*}"' _ {} \;
    
    • b=$(basename "$1"): Gets the filename part into variable b.
    • echo "${b%.*}": Prints the value of b with the shortest suffix matching .* (a dot followed by anything) removed.
      • file.txt -> file
      • archive.tar.gz -> archive.tar
      • .bashrc -> .bashrc (This expansion doesn't remove a leading dot if it's the only dot)
      • just_a_name -> just_a_name

    Example Output:

    For the same files:

    ./dir/file.txt
    ./another_file.tar.gz
    ./.bashrc
    ./just_a_name
    ./with.dot.in.name.log
    

    The output would be:

    file
    another_file.tar
    .bashrc
    just_a_name
    with.dot.in.name
    

    This version keeps parts of extensions before the final one. Choose this if that's your desired behavior.

    Which method to choose?

    • Method 1 (sed): If "filename without extension" means the "root" name before the first dot (e.g., archive from archive.tar.gz), and you want to correctly handle hidden files like .bashrc. This is often the more common interpretation.
    • Method 2 (${b%.*}): If "filename without extension" means removing only the final extension (e.g., archive.tar from archive.tar.gz).

    Both methods use standard tools available on virtually any Linux/Unix system and are safe for filenames with spaces or special characters due to the double quotes ("$1") and the way find -exec passes arguments.

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