linux_脚本应用
2014-01-01 10:53
393 查看
linux下三个有用的 Python 脚本
2010年4月29日
import os, sys
def pyc_clean(dir):
findcmd = 'find %s -name "*.pyc" -print' % dir
count = 0
for f in os.popen(findcmd).readlines():
count += 1
print str(f[:-1])
os.remove(str(f[:-1]))
print "Removed %d .pyc files" % count
if __name__ ==
"__main__":
pyc_clean(".")
清除.pyc文件 。
view source
print?
import os, sys
findcmd = 'grep -R
"cherrpy.request" .'
print "Searching…:"
for f in os.popen(findcmd).readlines():
if str(f[:-1]).find(".svn") == -1:
print str(f[:-1])
在所有文件中搜索特定字符串。
view source
print?
import os, sys
count = 0
def rename(filetypes, oldname, newname):
global count
for filetype in filetypes:
findcmd = 'find . -name "*.%s" -print' % filetype
for f in os.popen(findcmd).readlines():
file = str(f[:-1])
count += 1
print str("Rename %s" % file)
os.popen('sed -e "s/%s/%s/g" %s > %s.tmp'
% (oldname, newname, file, file))
os.popen('cat %s.tmp > %s' % (file, file))
os.remove("%s.tmp" % file)
rename(["py", "tmpl"], "mosaicCMS", "skeletonz")
print "Changed %s files" % count
使用python搜索和替换文件。
<*注*>:os.popen()可以实现一个“管道”,从这个命令获取的值可以继续被调用。
而os.system不同,它只是调用,调用完后自身退出。
******************************
Ruby批量执行Linux安装程序和脚本
2011年12月29日
require 'find'
module Find
def match(*paths)
matched = []
find(*paths) { |path| matched << path if yield path }
return matched
end
module_function :match
end
def ExecuteAllSh(sourcefile)
sourcefile .each do |s|
system("bash \""<< s << "\"")
end
end
def ExecuteAllPl(sourcefile)
sourcefile .each do |s|
system("perl \""<< s << "\"")
end
end
def ExecuteAllRb(sourcefile)
sourcefile .each do |s|
system("ruby \""<< s << "\"")
end
end
def ExecuteAllPy(sourcefile)
sourcefile .each do |s|
system("python \""<< s << "\"")
end
end
def ExecuteAllRpmBinRun(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end
def ExecuteAllClass(sourcefile)
sourcefile .each do |s|
system("java \""<< s[0...-6] << "\"")
end
end
def ExecuteAllBundle(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end
def ExecuteAllJar(sourcefile)
sourcefile .each do |s|
system("java -jar \""<< s << "\"")
end
end
ExecuteAllSh Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".sh"}
ExecuteAllPl Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".pl"}
ExecuteAllRb Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".rb"}
ExecuteAllPy Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".py"}
ExecuteAllRpmBinRun Find.match("./"){ |p| ext = p[-4...p.size]; ext && (ext.downcase == ".rpm" || ext.downcase == ".bin" || ext.downcase == ".run")}
ExecuteAllClass Find.match("./"){ |p| ext = p[-6...p.size]; ext && ext.downcase == ".class"}
ExecuteAllBundle Find.match("./"){ |p| ext = p[-7...p.size]; ext && ext.downcase == ".bundle"}
ExecuteAllJar Find.match("./"){ |p| ext = p[-4...p.size]; ext && ext.downcase == ".jar"}
2010年4月29日
import os, sys
def pyc_clean(dir):
findcmd = 'find %s -name "*.pyc" -print' % dir
count = 0
for f in os.popen(findcmd).readlines():
count += 1
print str(f[:-1])
os.remove(str(f[:-1]))
print "Removed %d .pyc files" % count
if __name__ ==
"__main__":
pyc_clean(".")
清除.pyc文件 。
view source
print?
import os, sys
findcmd = 'grep -R
"cherrpy.request" .'
print "Searching…:"
for f in os.popen(findcmd).readlines():
if str(f[:-1]).find(".svn") == -1:
print str(f[:-1])
在所有文件中搜索特定字符串。
view source
print?
import os, sys
count = 0
def rename(filetypes, oldname, newname):
global count
for filetype in filetypes:
findcmd = 'find . -name "*.%s" -print' % filetype
for f in os.popen(findcmd).readlines():
file = str(f[:-1])
count += 1
print str("Rename %s" % file)
os.popen('sed -e "s/%s/%s/g" %s > %s.tmp'
% (oldname, newname, file, file))
os.popen('cat %s.tmp > %s' % (file, file))
os.remove("%s.tmp" % file)
rename(["py", "tmpl"], "mosaicCMS", "skeletonz")
print "Changed %s files" % count
使用python搜索和替换文件。
<*注*>:os.popen()可以实现一个“管道”,从这个命令获取的值可以继续被调用。
而os.system不同,它只是调用,调用完后自身退出。
******************************
Ruby批量执行Linux安装程序和脚本
2011年12月29日
require 'find'
module Find
def match(*paths)
matched = []
find(*paths) { |path| matched << path if yield path }
return matched
end
module_function :match
end
def ExecuteAllSh(sourcefile)
sourcefile .each do |s|
system("bash \""<< s << "\"")
end
end
def ExecuteAllPl(sourcefile)
sourcefile .each do |s|
system("perl \""<< s << "\"")
end
end
def ExecuteAllRb(sourcefile)
sourcefile .each do |s|
system("ruby \""<< s << "\"")
end
end
def ExecuteAllPy(sourcefile)
sourcefile .each do |s|
system("python \""<< s << "\"")
end
end
def ExecuteAllRpmBinRun(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end
def ExecuteAllClass(sourcefile)
sourcefile .each do |s|
system("java \""<< s[0...-6] << "\"")
end
end
def ExecuteAllBundle(sourcefile)
sourcefile .each do |s|
system("\""<< s << "\"")
end
end
def ExecuteAllJar(sourcefile)
sourcefile .each do |s|
system("java -jar \""<< s << "\"")
end
end
ExecuteAllSh Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".sh"}
ExecuteAllPl Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".pl"}
ExecuteAllRb Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".rb"}
ExecuteAllPy Find.match("./"){ |p| ext = p[-3...p.size]; ext && ext.downcase == ".py"}
ExecuteAllRpmBinRun Find.match("./"){ |p| ext = p[-4...p.size]; ext && (ext.downcase == ".rpm" || ext.downcase == ".bin" || ext.downcase == ".run")}
ExecuteAllClass Find.match("./"){ |p| ext = p[-6...p.size]; ext && ext.downcase == ".class"}
ExecuteAllBundle Find.match("./"){ |p| ext = p[-7...p.size]; ext && ext.downcase == ".bundle"}
ExecuteAllJar Find.match("./"){ |p| ext = p[-4...p.size]; ext && ext.downcase == ".jar"}
相关文章推荐
- linux下各种文件格式的压缩以及解压缩命令(长期更新)
- 快速上手和使用makefile
- Linux ext3/ext4数据恢复
- linux samba 服务配置及日志管理
- Linux下的两种磁盘分区工具的使用
- 自己动手编译Linux内核
- Linux内核编译与安装
- vmlinux,zImage,bzImage,vmlinuz,uImage,关系
- wine qq 2013 for linux deb包 Ubuntu 64位兼容
- 内核编译之vmlinuz vmlinux system.map initrd
- linux系统函数read和write
- linux系统编程之文件与io(五)
- linux下添加新硬盘的方法
- arm-linux eabi 交叉工具链编译笔记
- 在CentOS上编译安装PostgreSQL
- Linux 设备模型浅析之 uevent 篇(2)
- linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
- linux jdk配置环境变量
- [Linux]Fedora 20 安装配置笔记
- linux 进程