Skip to main content
deleted 6 characters in body
Source Link
l0b0
  • 53.6k
  • 48
  • 225
  • 398

If you've set up any of the standard shebang lines it looks like file (at least version 5.39) should be able to detect them:

$ echo '#!/usr/bin/env bash' > 1 $ echo '#!/bin/bash' > 2 $ echo '#!/usr/bin/bash' > 3 $ file * 1: Bourne-Again shell script, ASCII text executable 2: Bourne-Again shell script, ASCII text executable 3: Bourne-Again shell script, ASCII text executable 

So we should be able to do this:

for path in ./* do if [[ "$(file -- "$path")" = "${path}: Bourne-Again shell script, ASCII text executable" ]] then mv -- "$path" "${path}.bash" fi done 

WorksThis works for the above examplesexample files (and should work for unusual filenames, including those containing space characters, starting with a hyphen, or ending with a newline):

$ ls 1.bash 2.bash 3.bash 

This will probably not work if your files do not have shebang lines (which is reasonable for files which are sourced by other scripts or the shebang lines are wrong (such as #!/bin/sh).

(I've used ".bashbash" since that is more explicit than ".sh)sh".)

If you've set up any of the standard shebang lines it looks like file should be able to detect them:

$ echo '#!/usr/bin/env bash' > 1 $ echo '#!/bin/bash' > 2 $ echo '#!/usr/bin/bash' > 3 $ file * 1: Bourne-Again shell script, ASCII text executable 2: Bourne-Again shell script, ASCII text executable 3: Bourne-Again shell script, ASCII text executable 

So we should be able to do this:

for path in ./* do if [[ "$(file -- "$path")" = "${path}: Bourne-Again shell script, ASCII text executable" ]] then mv -- "$path" "${path}.bash" fi done 

Works for the above examples:

$ ls 1.bash 2.bash 3.bash 

This will probably not work if your files do not have shebang lines (which is reasonable for files which are sourced by other scripts or the shebang lines are wrong (such as #!/bin/sh).

(I've used .bash since that is more explicit than .sh).

If you've set up any of the standard shebang lines it looks like file (at least version 5.39) should be able to detect them:

$ echo '#!/usr/bin/env bash' > 1 $ echo '#!/bin/bash' > 2 $ echo '#!/usr/bin/bash' > 3 $ file * 1: Bourne-Again shell script, ASCII text executable 2: Bourne-Again shell script, ASCII text executable 3: Bourne-Again shell script, ASCII text executable 

So we should be able to do this:

for path in ./* do if [[ "$(file "$path")" = "${path}: Bourne-Again shell script, ASCII text executable" ]] then mv "$path" "${path}.bash" fi done 

This works for the above example files (and should work for unusual filenames, including those containing space characters, starting with a hyphen, or ending with a newline):

$ ls 1.bash 2.bash 3.bash 

This will probably not work if your files do not have shebang lines (which is reasonable for files which are sourced by other scripts or the shebang lines are wrong (such as #!/bin/sh).

(I've used ".bash" since that is more explicit than ".sh".)

Source Link
l0b0
  • 53.6k
  • 48
  • 225
  • 398

If you've set up any of the standard shebang lines it looks like file should be able to detect them:

$ echo '#!/usr/bin/env bash' > 1 $ echo '#!/bin/bash' > 2 $ echo '#!/usr/bin/bash' > 3 $ file * 1: Bourne-Again shell script, ASCII text executable 2: Bourne-Again shell script, ASCII text executable 3: Bourne-Again shell script, ASCII text executable 

So we should be able to do this:

for path in ./* do if [[ "$(file -- "$path")" = "${path}: Bourne-Again shell script, ASCII text executable" ]] then mv -- "$path" "${path}.bash" fi done 

Works for the above examples:

$ ls 1.bash 2.bash 3.bash 

This will probably not work if your files do not have shebang lines (which is reasonable for files which are sourced by other scripts or the shebang lines are wrong (such as #!/bin/sh).

(I've used .bash since that is more explicit than .sh).