How to enter all subdirectories and execute command on files inside? [duplicate]

拟墨画扇 提交于 2019-12-11 06:05:50

问题


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 !

来源:https://stackoverflow.com/questions/42931420/how-to-enter-all-subdirectories-and-execute-command-on-files-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!