I would like to put the full directory path of the filename inside the file.So, if there is foo/bar/main.cpp, then the path is added at the top of the file main.cpp.Can anyone help me with a bash command?I tried all the find , exec and xargs methods, but doing echo or cat provide me the content of the file and not the filename itself. Maybe the answers would make it clear how filenames are extracted from find exec or any other method.Expected output in a file /foo/bar/main.cpp///foo/barint main ( .... ) {}
adding filepath in the files
bash
If you use gnu find/sed, this should work:cd /find subpath_with_files -name .hg -prune -o -type f -exec sed -i -e '1i\{}' {} +If you want to add it as a comment, you could use something like this:cd /find subpath_with_files -name .hg -prune -o -type f -exec sed -i '1i\# Filename: {}' {} +The trick is that {} can be used more than once in the exec parameter of find.I'm pruning .hg assuming that this is in a mercurial repository.  This can be .git or other.  The files you are modifying should be in a repository of some kind so that you can know that the change worked as desired, and you can roll back with a revert command if not.