Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to get the names of files which are starting with a string testfile. Also i want to create a XML file in the same location and write these file names into the XML.

<path>
 <dir>
     <file>testfile1</file>
 </dir>
 <dir>
     <file>testfile2</file>
 </dir>
<path>
share|improve this question
    
Not so sure if you can do it with shell script alone. I'm sure you could do it in C++ / Java. If it's only the structure you've shown (only files in current directory and not subdirectories) it should be possible in scripts as well. Sorry but could you specify if you want to do this in Windows or in GNU/Linux / MacOSX and with which shell interpreter ? –  user51166 Dec 25 '11 at 13:39
2  
I'm afraid I'll have to repeat myself, but you do not want to create XML with shell scripts. Use a proper programming language (like C++ or Java) or scripting language (like Perl, Python or Ruby). For a Ruby approach, see this script of mine, which creates an XML tree based on a directory listing. –  slhck Dec 25 '11 at 13:43
    
@slhck Well, it's doable, but it ain't pretty. Just wrote a script for it :-) –  Daniel Beck Dec 25 '11 at 20:27
add comment

2 Answers

As @slhck said, the shell isn't really the right tool for writing XML (although it's not as bad as trying to parse XML in the shell...), but it's not too hard to do a quick&dirty script:

targetdir="/path/to/dir/of/files"
prefix="testfile"
outfile="$targetdir/out.xml"

# Write the opening tag(s):
echo "<path>" >"$outfile"

# Loop through the matching files, writing entries for each one:
for f in "$targetdir/$prefix"*; do
    cat <<END_INSERT >>"$outfile"
 <dir>
  <file>$(basename "$f")</file>
 </dir>
END_INSERT
done

# Write the closing tag(s):
echo "</path>" >>"$outfile"

So what's wrong with the above, that you'd want to use something with a real XML library instead? Well, consider what'd happen with the above if any filenames happen to contain "<" or ">" (which are perfectly legal characters in unix filenames)? Doing this right involves encoding the filenames with HTML entities, and (AFAIK) the shell doesn't have good tools for this; a good XML library will just handle this sort of thing for you automatically.

A couple of notes on the script: first, note that the first echo redirects with ">", while all the subsequent writes to outfile use ">>" -- that's because ">" empties the file before writing, so you want this only on the first write.

Second, I use echo for the first and last writes, but cat with a here-document in the loop -- this is just a matter of convenience, because echo is easiest for single-line writes, but here-docs are easier to do multi-line writes in. You could easily use echo everywhere, or cat << everywhere if you wanted to be consistent.

Third, I tend to double-quote everything that contains a variable. This is a way of avoiding trouble with special characters (like spaces) in things like filenames. In general, I follow the principle that you should quote everything unless there's a specific reason not to. It's bad enough the XML will have trouble with special characters, I don't want trouble at the shell level as well.

share|improve this answer
add comment

It is possible for the bash shell to create an XML file containing the names of files matching a pattern (in this case testfile*):

#!/bin/bash

XML_LIST="$HOME/xml.list"
touch $XML_LIST
#find files matching the regex
find /home/ -name "testfile*" -print0 | xargs -n1 -0  >> /tmp/temp_filelist

echo "<path>" > $XML_LIST

#read list of files from TMP_FILE
#declare array 
declare -a FILE_NAMES
#open file for reading to array
exec 10</tmp/temp_filelist
while read LINE <&10; do
    FILE_NAMES[$count]=$LINE
    if [ "$LINE" != "" ]; then
        echo " <dir>" >> $XML_LIST
        echo "     <file>$LINE</file>" >> $XML_LIST
        echo " </dir>" >> $XML_LIST
    fi
done
#close temp read file 
exec 10>&-

echo "</path>" >> $XML_LIST
echo "done"

Reading this into the shell is going to be more complicated, but still entirely possible.

For this kind of scripting I would recommend another Linux native language, Python, which is easy to learn and has a specialized XML library to make possible the above write process, as well as reading the XML file, in less lines of code!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.