I need a bash script that lists files recursively in a directory in the following way:
filename,size (in kb),numeric value (in filename)
e.g.:
/directory/1/file-100.txt,50,100
/directory/1/file-200.txt,45,200
/directory/2/file-100.txt,20,100
/directory/2/file-500.txt,100,500
Al开发者_运维知识库so, I need it to ignore directories that start with ".svn
"
Give this a try:
find -name ".svn" -prune -o -type f -printf "%p,%k,\n" | sed 's/\([^-]\+-\([0-9]\+\)\..*\)/\1\2/'
Only two utilities used (GNU versions probably required).
maybe not the best way but should work:
find /path -type f | grep -v '.svn' | xargs du -k |\
awk '{print $2","$1}' | sed 's/-\([0-9]*\)\(\..*\)/-\1\2,\1/'
Features:
- Avoid possible expensive traverse down .svn-dirs.
- Have find report size instead of 'du'
- Make awk do some more magic, no need for sed.
Suggestion on solution:
$ find . -path '*/.svn' -prune -o -type f -printf '%p %k\n' |\
awk -v OFS=, '{ print $1, $2, gensub(/[^0-9]*([0-9]*)/, "\\1", "g", $1) }'
find-options:
- -path ... Match .svn directories
- -prune If match (-path) is a directory, don't descend.
- -o ... Or ...
- -type f Match files only.
- -printf ... Print a custom line containing path (%p) and size in 1K-blocks (%k).
awk:
- OFS= What separator to use for fields when printing, instead of whitespace.
- $1 Filepath
- $2 Filesize
- gensub(...) Sorry, see http://www.gnu.org/manual/gawk/html_node/String-Functions.html
Edit: Updated per Dennis suggestion. Can't believe I missed %k actually, I blame 3AM.
精彩评论