linux获取文件的路径名及其文件名的方法
1、简单测试实例
for i in `find . -type f -name "*.c"`
do
echo $i
basename $i 获取*.c文件名
dirname $i 获取*.c对应的目录名
done
2、实际应用
diff Linux源码,并将有不一样的源码整理在一起,要求:文件名以new和old区分。
(1)、简单测试
rm -rf ~/Desktop/20091107
cp_source()
{
#for i in `find . -type d -print`
for i in `find $1 -type f -name "*.s"`
do
#echo $i
#basename $i
dirname $i
mkdir -p ~/Desktop/20091107/`dirname $i`
cp $i ~/Desktop/20091107/`dirname $i`/`basename $i`_"$2"
done
}
cd linux-0.11 && cp_source "." "new"
cd ../linux-0.11 && cp_source "." "old"
(2)、实际实现(以diff -Nur linux-2.6.30 linux-2.6.31.5为例)
rm -rf ~/Desktop/20091107
#cat linux-patch.txt | grep 'diff -Nur ' | sed 's/^.*linux-2.6.30g'| sed 's/ linux-2.6.31.5.*$//g' > ~/Desktop/diff.txt
old_source_noprefix=`cat linux-patch.txt | grep 'diff -Nur ' | sed 's/^.*linux-2.6.30g'| sed 's/ linux-2.6.31.5.*$//g'`
new_source_noprefix=`cat linux-patch.txt | grep 'diff -Nur ' | sed 's/^.*linux-2.6.31.5g'`
old_source_haveprefix=`cat linux-patch.txt | grep 'diff -Nur ' | sed 's/^.*linux-2.6.30/linux-2.6.30/g'| sed 's/ linux-2.6.31.5.*$//g'`
new_source_haveprefix=`cat linux-patch.txt | grep 'diff -Nur ' | sed 's/^.*linux-2.6.31.5/linux-2.6.31.5/g'`
cp_source()
{
#for i in `find . -type d -print`
for i in $1
do
#echo $i
#basename $i
#dirname $i
#Used "diff -Nur", some file didn't exist!
mkdir -p ~/Desktop/20091107/`dirname $i` && cp ./$2/$i ~/Desktop/20091107/`dirname $i`/`basename $i`_"$3" 2>/dev/null
done
}
cp_source "$old_source_noprefix" "linux-2.6.30" "old"
cp_source "$new_source_noprefix" "linux-2.6.31.5" "new"
更多推荐
所有评论(0)