Python Learning-Print the most commonly used 10 Linux commands and find duplicate files in directories

Source: Internet
Author: User

First, print the most commonly used 10 Linux commands

#!/usr/bin/python#coding=utf-8import osfrom Collections Import Counterc = Counter () with open (Os.path.expanduser (' ~/. Bash_history ') as F:for line in f:cmd = Line.strip (). Split () if Cmd:c[cmd[0]]+=1print C.most_common (10)

The effect is as follows:



Second, find the duplicate files in the directory

#!/usr/bin/python#coding=utf-8from __future__ import print_functionimport sysimport  Hashlibimport osimport fnmatchchunk_size=8192def is_file_match (filename,patterns):for  Pattern in patterns:if fnmatch.fnmatch (Filename,pattern): Return truereturn falsedef  find_specific_files (root,patterns=[' * '],exclude_dirs=[]):for root,dirnames,filenames in  Os.walk (Root): For filename in filenames:if is_file_match (filename,patterns):yield  Os.path.join (root,filename) For d in exclude_dirs:if d in dirnames: Dirnames.remove (d) def get_chunk (filename): with open (filename)  as f:while true:chunk= F.read (chunk_size) if not chunk:breakelse:yield chunkdef get_file_checksum (filename): h= HASHLIB.MD5 () for chunk in get_chunk (filename): h.update (Chunk) return h.hexdigest () def  main  (): Sys.argv.append ("") directorY=sys.argv[1]if not os.path.isdir (directory):raise systemexit  ("{0} is not  A directory. Format (directory)  record={}for item in find_specific_files (directory): Checksum=get_file_checksum (item) if checksum in record:print  (' find duplicate  Files: {0} vs {1} '. Format (record[checksum],item)) else:record[checksum]=itemif __name__  ==  ' __main__ ': Main ()

The effect is as follows:

Python Learning-Print the most commonly used 10 Linux commands and find duplicate files in directories

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.