views:

26

answers:

2

hii, i want to edit she bang line #!/bin/bash line on the top of files in all the files with .sh extension how it done bye scripting . thanks

+1  A: 

You can do this using the sed command:

sed -i "1i #!/bin/bash" *.sh
  • sed: name of the command
  • -i : sed option to edit the file inplace
  • 1 : line number
  • i : to do the insertion before the line number provided before
  • #!/bin/bash: the shebang to be added
  • *.sh: to make this work on all .sh files
codaddict
ca nu please explain what 1i works here
sunil
its works for existing and newly created sh files is there any option that it impact only on newly created sh files not disturbing the previous ones
sunil
No, there is not such option for sed. You'll have to use **find** combined with this sed to modify only newly created files.
codaddict
can u please suggest how to use sed with find "by showing example" i mean is this possible by using ctime options or what
sunil
A: 
find /path -iname "*.sh" -type f -exec sed -i.bak '1i #!/bin/bash' "{}" +;
ghostdog74
what does i.bak option used for
sunil
look up the man page of sed.
ghostdog74