Page Actions
Wiki Actions
User Actions
Submit This Story

sqlite 把玩

穷极无聊,机器上有个目录文件极多,于是突然想起来把文件信息写进sqlite…sqlite 的-line参数还是很好用的…很适合脚本里用. 表结构很简单:

CREATE TABLE FILES(
                  perms text,
                  links int,
                  user text,
                  groups text,
                  bytes int,
                  yyyymmdd text,
                  time text,
                  file text  );

脚本

#!/bin/bash
DIR=.
rm -f $DIR/files.sqlite3
ls -l | sed 's/\s\s*/,/g' | tail -n +2 > $DIR/import.csv
 
echo "CREATE TABLE FILES( \
perms text, \
links int, \
user text, \
groups text, \
bytes int, \
yyyymmdd text, \
time text, \
file text); " > $DIR/create.sql
 
sqlite3 -line files.sqlite3 ".read $DIR/create.sql"
 
echo -e ".separator \",\"\n" > $DIR/load.sql
echo -e ".import $DIR/import.csv FILES\n" >> $DIR/load.sql

ls -l没有包含子目录,以后有空用想想怎么用ls -RL或者find来把子目录也包括进去…sed什么的麻烦了,估计python/perl会简单些…

,

Discussion

MeaCulpa, 2010/06/04 02:54

是啊,但是还没想好怎样在数据库里表达子目录的信息,最简单的也许可以每个文件或目录都有个parent字段…

Ian Yang, 2010/05/18 03:51

文件名都没有特殊字符可以用 find|xargs (或者find -exec) 来代替 ls -l即可遍历子目录

  find | xargs ls -ld | sed ......
Enter your comment
 
 
blog/2010/05/ls_db.txt · Last modified: 2010/05/18 02:14 by MeaCulpa     Back to top
Recent changes RSS feed Creative Commons License Powered by PHP Driven by DokuWiki