July #TMIL - Some command line favorites

Getting close to the end of the year.. will I make all twelve TMIL posts? Let's see how far we can get as things might start to take on a "best of 2014" flavor. Here are some command line favorites I've noted and usually forget, mostly around searching:

List the first ten items in a directory
ls | head -10

Show only the human-readable sizes of all xml files in a directory
ls -lSah *.xml | awk '{print$5}'

Sum the sizes of all zip files in a directory (notice there's no -h flag to ls, this way you sum only bytes, then later convert to kilobytes then to megabytes)
ls -lSa *.zip | awk '{ total += $5}; END {print total/1024/1024 "Megabytes"}'

Find all files recursively, count them
find . -type f | wc -l

Find files in app/views with case-insensitive 'important' somewhere in the name
find app/views -iname "*important*" -type f

Find visible or hidden files in the current directory with case-insensitive 'important' anywhere in the title and move them to an existing directory named 'temp'
find . -iname '*important*' -type f -maxdepth 1 | while read FILE; do mv $FILE ./temp; done

Find xml files that don't contain 'beer' in the current directory
grep -v 'beer' *.xml

Find files in config/locales that have been modified in the last day
find config/locales -mtime -1

Find the word 'beer' in files within the app/models directory and show the match in color
grep --color -Rin beer app/models

Find directories starting with the path app/views/external_*
find app/views/external_* -type d

Show only the matching parts of a line (-o) from a regular expression search for ten consecutive numbers in xml files in the currently directory (add -h to hide the matching filenames)
egrep -o '[0-9]{10}' *.xml

Popular posts from this blog

Verifying Doubles with ActionMailer in Rails 4

Drupal Workflow with Drush, Git and git-ftp

January #TMIL