1

i'll start off by saying I'm a newby when it comes to bash.

I am trying to create a script that would enter all directories and sub-directories (and sub-sub-..-sub-directories) and execute a command on the files inside those directories.

The command I need to run is simply adding a bit of text at the beginning of the file and saving it in (another) file with ".com" at the end of the filename in the same directory as the original.

So far I've been able to create a script that does exactly this but doesn't go deeper than one subdirectory (it won't go into the directories of the directories)

for f in /scratch/aurelien/test/*;
do
[ -d $f ] && cd "$f" &&
for file in ./* ; do printf "TEXT TO ADD TO BEGINNING OF FILE" > ${file}.com && 
tail -n +3 <"$file" >> "${file}.com" ; done &&
done;

After going in circles for a while I've decided to try using Find to do this:

 find /scratch/aurelien/test/ -type f -execdir printf "TEXTTOADDTOBEGINNINGOFFILE" > {}.com &&
 tail -n +3 <"${}" >> "${}.com" \;

but i'm not sure i've understood well the roll of the {} in the find command, and using execdir I get an error message:

find: The current directory is included in the PATH environment variable, which is insecure in combination with the -execdir action of find. Please remove the current directory from your $PATH (that is, remove "." or leading or trailing colons)

Any help would be greatly appreciated !

noobstack
  • 21
  • 3
  • It's pretty clear, isn't it? It's refusing to accept your PATH because it contains `.` somewhere. – tripleee Mar 21 '17 at 15:35
  • I think the strings `> {}.com`, `&&`, `>>` and `<"${}"` are going to be interpreted by the shell before the find command runs. – Jdamian Mar 21 '17 at 15:47
  • @tripleee Well it's not very clear to me as the only "." in the scripts are the ones used for the ".com" which have nothing to do with the PATH I would have thought. – noobstack Mar 21 '17 at 15:50
  • It's in your environment, not in your script. If you have `PATH=/usr/local/bin:/usr/bin/:/bin:.` the last one is the offending one. – tripleee Mar 21 '17 at 15:51
  • @Jdamian Does this mean I cannot use as variables the files that the find command finds in the command line of find itself ? – noobstack Mar 21 '17 at 15:52
  • You can wrap it in something like `-exec bash -c 'complex commands here' _`. That's a pretty complex command line but once you get the gist of it it's not very hard to understand. – tripleee Mar 21 '17 at 15:56
  • I added a pointer to another duplicate which covers that, although it's not a perfect fit (the OP there wants to avoid using the shell). – tripleee Mar 21 '17 at 16:03
  • @tripleee Thanks, this modification makes it _almost_ work, it descends in all directories and creates the .com file I want with the correct name and printf content, however it seems it does not execute the 'tail -n +3 <"${}" >> "${}.com"' part of the code. – noobstack Mar 21 '17 at 16:06
  • The `&&` in your attempt is outside of the `find -exec`. You want `find -blah blah -execdir bash -c 'printf "things">"$1".com && tail -n 3 "$1" >>"$1".com' _ {} ;` – tripleee Mar 21 '17 at 16:08
  • When you pass something to `bash -c 'commands' _ {}' the Bash instance gets the argument as `$1` from the `find` parameter `{}`. They are different ways to refer to the same thing from different contexts / languages. – tripleee Mar 21 '17 at 16:25
  • @tripleee I think I've got it to work finally, It seems that changing ${} to $1 did the job. I still don't understand why previously it didn't do the tail command as I had put it in the field as you had but it doesn't matter too much. Ï'm not familiar with bash's strict formatting, could spaces have caused this problem? Anyways thank you very much for your help. – noobstack Mar 21 '17 at 16:45

0 Answers0