Shell Bash Add extension to files without one

时间:2018-02-26 17:56:09

标签: bash shell find

I am trying to create a scrits that can find files without an extension and add "html" to the end of it. I was able to create a small script to get anything without a "." in the end, however sometimes people have file with "." in it's name without being related to it's extension and i don't known what to do about that.

Here's is an example:

File1.xml
File2.txt
File3
File.4
File5.sh
File6

This is what i have so far:

#! /bin/bash

cd TestFiles
dir=$(pwd)

echo "Searching Files Without Extension"


array=$(find . -type f ! -name "*.*")

echo $array

for file in $array; do
    filename=$(basename "$file");
    echo $filename
    #extension=$(echo "$filename" | cut -d'.' -f2)
    mv -f $filename $filename".html"

done

ls

Again, the code above works but only if the file does not have a "." on it's name.

1 个答案:

答案 0 :(得分:2)

Simple and compatible with all POSIX-compliant shells:

for f in *; do
  case $f in *.*) continue;; esac
  mv -- "$f" "${f}.html"
done