1

I have a dozen of files without extention, and I need to add .sh to all the files are a script Bourne-Again shell. How do you achevie that without using sed, awk,...

I located the files like so:

file * | grep "Bourne-Again shell"

With the command xargs how do you add an extension to the files that resulted from above? If possible file command must be included in the pipes to locate the files.

2 Answers2

1

You can do it like so:

for f in *
    do
    grep "Bourne-Again shell" <(file "$f") >/dev/null && echo mv "$f" "$f".sh
    done

Run the code above from within the directory containing your script files and remove echo when satisfied with the output to do the actual renaming.


Notice:

xargs is not the best fit for this operation, but if you insist then it can be done the ugly way like so:

file * | grep "Bourne-Again shell" | cut -f 1 -d ' ' | rev | cut -c 2- | rev | xargs -I {} -n1 echo mv {} {}.sh

Run the code above from within the directory containing your script files and remove echo when satisfied with the output to do the actual renaming.

Raffa
  • 34,963
0

You can use mv to change the filename.

E.g. mv $FILENAME $FILENAME.sh

file * | grep "Bourne-Again shell" | while read FILE; do mv "${FILE}" "${FILE}.sh"; done
Cas
  • 612