The other common wildcard is the question mark ( ?), which stands for one character. Here, * is a wildcard standing for "any string of characters except /" and *.txt is a glob pattern. txt from the current directory to the directory textfiles. For example, the Unix Bash shell command mv *.txt textfiles/ moves ( mv) all files with names ending in. The script does no error checking, but it should give you an idea of how to approach this in Python, which gives you a nice platform independent way of solving this problem.In computer programming, glob ( / ɡ l ɒ b/) patterns specify sets of filenames with wildcard characters. This script takes a little big longer to explain, but essentially if you save that as a script, it will search through the first argument provided on the command line, and return the largest file in that directory. Realpath = os.path.join(dirpath, filename)įiles_sorted_by_size = sorted(files, key = lambda x: os.stat(x).st_size) awk: Select the second column, which contains the filename, and is the largest file in a recursive directory.įor dirpath, dirname, filenames in os.walk(sys.argv):.tail: Only select the last line of output (since the list is sorted, this is the largest file!).Since the first argument is an integer, our results will be sorted properly sort: Sort the results with the numeric switch.I am printing out two columns, the first column being the block size (%s), and the second column being the file name (%n) stat: Stat is an all around awesome command that has so many use cases.For every line of output, we want to run the stat utility on that file xargs: utility to execute commands using arguments provided from standard input.find: Recursively finds all files from current directory, and prints them out with a null character.I'll also provide a Python solution that should let you use this functionality even on Windows Linux command line solution Recursively return entire list of only files from a directory, sorted by file size find. Most people have provided some unique answers, but I prefer mine because it properly handles filenames, and the use case can easily be changed (modify stat, and sort arguments) I'm going to provide a Linux solution that uses the find, xargs, stat, tail, awk, and sort utilities. This is an incredibly commmon need for a variety of reasons (I like finding the most recent backup in a directory), and is a surprisingly simple task. $ du -ah ~/Downloads/ | grep -v "/$" | sort -h | tail -6 If you just want the list of smallest to biggest, but the top 6 offending files you can reverse the sort switch, drop ( -r), and use tail -6 instead of the head -6. Then you can filter out the directories with grep -v '\s/*$': $ du -ah ~/Downloads/ | grep -v '\s/*$' | sort -rh | head -2 This assumes that your directory names do not contain dots, and that the files you are looking for do. If you want to exclude all directories from the output, you can use a trick with the presence of a dot character. Reverse it, biggest to smallest: $ du -ah ~/Downloads/ | sort -rh | head -6ģ.8G /home/saml/Downloads/audible/audio_booksġ.5G /home/saml/Downloads/digital_blasphemy/db1440ppng.zipġ.5G /home/saml/Downloads/digital_blasphemyĭon't show me the directory, just the files: $ du -ah ~/Downloads/ | grep -v "/$" | sort -rh | head -6Ĩ35M /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run I'm using the head -6 just to limit the amount of output! $ du -ah ~/Downloads/ | head -6Ĥ.4M /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020_WirelessFrames_exUG_GLB_en.pdfĦ24K /home/saml/Downloads/kodak_W820_wireless_frame/easyshare_w820.pdfĤ.9M /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020WirelessFrameExUG_GLB_en.pdfĩ.8M /home/saml/Downloads/kodak_W820_wireless_frameĦ04K /home/saml/Downloads/netgear_gs724t/GS7xxT_HIG_5Jan10.pdfĮasy enough to sort it smallest to biggest: $ du -ah ~/Downloads/ | sort -h | head -6Ġ /home/saml/Downloads/apps_archive/monitoring/nagios/nagios-check_sip-1.3/usr/lib64/nagios/plugins/check_ldapsĠ /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/index/write.lockĠ /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/translog/translog-1365292480753Ġ /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/index/write.lockĠ /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/translog/translog-1365292480946Ġ /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/2/index/write.lock If you don't want them then drop that switch. The -h will produce human readable sizes which I prefer. The command du -ah DIR will produce a list of all the files and directories in a given directory DIR. The approach: $ du -ah | grep -v "/$" | sort -rh Just to be on the safe side I'm using this version of du: $ du -version
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |