Home > other >  Bash Script - Need find command to output file paths enclosed in double quotes without line breaks
Bash Script - Need find command to output file paths enclosed in double quotes without line breaks

Time:12-25

I have been using cscope/ctags database. However after sometime I noticed that some files in my cscope.files that stores the result of my find command, are broken into two or more lines. This causes them being ignored by cscope/ctags while indexing.

Currently I use it in an alias :

alias prp_indx='
    rm cscope.in.out cscope.out cscope.files tags
    find . -name '\''*.[chS]'\'' >> cscope.files
    find . -name '\''*.cpp'\'' >> cscope.files
    find . -name '\''*.hpp'\'' >> cscope.files
    find . -name '\''*.cxx'\'' >> cscope.files
    find . -name '\''*.hxx'\'' >> cscope.files
    cscope -b -q -k; ctags -R
'

Please help me with an appropriate command that I can use in my alias/function to achieve the file names with double quotes without paths broken in many lines.

CodePudding user response:

There is no reason I can think of for find to split a file name on several lines, except if the name itself has newline characters in it.

If you have such file names, it is probably better to rename these files as I think cscope does not really support file names with newlines in them. At least, I don't think there is a way to list such files in a cscope.files file, even with quoting or any kind of escaping (but if you know how to do, please let us know, such that we can adapt what follows accordingly). So, the best you could do is to let cscope do the search (-R) instead of providing a cscope.files file. If you do so cscope will indeed find and analyse these files, but then, when interacting with cscope you will discover that it gets confused and splits the names anyway...

If you do not have such unusual file names, but there are unwanted newline characters in your cscope.files file, there must be something else that tampers with it.

Anyway, prefer a function. Compared to functions, aliases mainly have drawbacks. With a bash function:

prp_indx () {
  rm cscope.in.out cscope.out cscope.files tags
  find . -name '*.[chS]' -o -name '*.[ch]pp' -o -name '*.[ch]xx' > cscope.files
  cscope -b -q -k
  ctags -R "$@"
}

Note: if you can have directories with names matching one of the 3 patterns add a -type test to exclude directories:

find . ! -type d \( -name '*.[chS]' -o -name '*.[ch]pp' -o -name '*.[ch]xx' \) > cscope.files

If you have unusual file names containing spaces, double-quotes and/or backslashes, you can add a post-processing with, e.g., sed:

sed -i 's/["\]/\\&/g;s/^\|$/"/g' cscope.files

This will add a backslash before any double-quote or backslash, plus double-quote all file names. Add this sed command to the function definition, after the find command.

  • Related