Extract filename and extension in Bash

后端 未结 30 2578
野趣味
野趣味 2020-11-21 05:29

I want to get the filename (without extension) and the extension separately.

The best solution I found so far is:

NAME=`echo \"$FILE\" | cut -d\'.\'          


        
30条回答
  •  后悔当初
    2020-11-21 06:25

    Here are some alternative suggestions (mostly in awk), including some advanced use cases, like extracting version numbers for software packages.

    f='/path/to/complex/file.1.0.1.tar.gz'
    
    # Filename : 'file.1.0.x.tar.gz'
        echo "$f" | awk -F'/' '{print $NF}'
    
    # Extension (last): 'gz'
        echo "$f" | awk -F'[.]' '{print $NF}'
    
    # Extension (all) : '1.0.1.tar.gz'
        echo "$f" | awk '{sub(/[^.]*[.]/, "", $0)} 1'
    
    # Extension (last-2): 'tar.gz'
        echo "$f" | awk -F'[.]' '{print $(NF-1)"."$NF}'
    
    # Basename : 'file'
        echo "$f" | awk '{gsub(/.*[/]|[.].*/, "", $0)} 1'
    
    # Basename-extended : 'file.1.0.1.tar'
        echo "$f" | awk '{gsub(/.*[/]|[.]{1}[^.]+$/, "", $0)} 1'
    
    # Path : '/path/to/complex/'
        echo "$f" | awk '{match($0, /.*[/]/, a); print a[0]}'
        # or 
        echo "$f" | grep -Eo '.*[/]'
    
    # Folder (containing the file) : 'complex'
        echo "$f" | awk -F'/' '{$1=""; print $(NF-1)}'
    
    # Version : '1.0.1'
        # Defined as 'number.number' or 'number.number.number'
        echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?'
    
        # Version - major : '1'
        echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f1
    
        # Version - minor : '0'
        echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f2
    
        # Version - patch : '1'
        echo "$f" | grep -Eo '[0-9]+[.]+[0-9]+[.]?[0-9]?' | cut -d. -f3
    
    # All Components : "path to complex file 1 0 1 tar gz"
        echo "$f" | awk -F'[/.]' '{$1=""; print $0}'
    
    # Is absolute : True (exit-code : 0)
        # Return true if it is an absolute path (starting with '/' or '~/'
        echo "$f" | grep -q '^[/]\|^~/'
    

    All use cases are using the original full path as input, without depending on intermediate results.

提交回复
热议问题