Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
11 total  XML / RSS feed 

Progress indicator snippet for man find


find -x "$HOME" -type f -print0 2>/dev/null | while read -d $'\0' filename; do 
   if [[ ${#filename} -lt 85 ]]; then
      printf -- "\r\e[0K$(printf -- "$filename" | tr -d '\n\r')"; 
   else
      printf -- "\r\e[0K$(printf -- "$filename" | tr -d '\n\r' | sed -En 's/^(.{40}).*(.{40})$/\1.....\2/p')"; 
   fi
done; echo



find -x "$HOME" -type f -print0 2>/dev/null | while read -d $'\0' filename; do 
   i=$[i+1]
   if [[ ${#filename} -lt 85 ]]; then
      printf -- "\r\e[0K\e[1;32m$i\e[0m $(printf -- "$filename" | tr -d '\n\r')"; 
   else
      printf -- "\r\e[0K\e[1;32m$i\e[0m $(printf -- "$filename" | tr -d '\n\r' | sed -En 's/^(.{40}).*(.{40})$/\1.....\2/p')"; 
   fi
done; echo

Finding large files

// Hi all, just a cheap and easy way to find a bunch of large files on your server etc..

#!/bin/bash
for file in `find / -type f -size +100000`; do
        ls -lh $file
done

recursively find (and delete) files and directories in linux (and svn)

This example looks in the current directory for directories named .svn

That isn't the best way to do it. It will break if you have lots of .svn directories. Then the command line will be too long and you will get 'argument list too long'. Recent bash have enormous buffers so this is less of a problem. xargs solves this problem.

The other problem is any file with spaces in the name will cause bash to treat it as two arguments. Which could give an error or could delete the wrong file. find -print0 and xargs -0 solve this problem.

find . -type d -name .svn

find . -name .svn -print0 | xargs -0 rm -rf



OR make an alias to an egrep command and go nuts

alias g='egrep --color=auto -i -r'


from here on out, just type g "" *

case insensitive
recursive
colored results

Find last article

Add this to your Article model (or whatever you name it) to get the last published article on your site.

def Article.find_last
  Article.find(:first, :order => "created_at DESC")
end

Mass find-replace using sed

Replace some widespread nasty hardcoded strings. 'sed $file > $file' doesn't work so hot so we use a temp file, and also make a backup of the old file.

for i in $(find . -type f); do sed 's/oldstring/newstring/g' $i > $i-tmp; mv $i $i-backup; mv $i-tmp $i; done

Make Find.find return an array

Ruby's find module usually forces you to pass it a block. This snippet makes it return an array if there is no block. Note: the returning method is from Rails, but this can easily be removed.

require 'find'

module Find
  class << self
    alias_method :find_old, :find
    
    def find(*paths)
      if block_given?
        find_old(*paths, &yield)
      else
        returning ary = [] do
          find_old(*paths) { |p| ary << p }
        end
      end
    end
  end
end

Find flagged message subjects in maildir

Simple, perhaps slightly cryptic oneliner to get the subjects of all flagged messages in a Maildir. It assumes that 'F' is the flag for flagged messages (as it almost always is) and that your message files end with ":2,".

find ~/Maildir -type f -name "*:2,*F*" -exec egrep -h "^Subject:" "{}" ";" | cut -c 10-

find and delete Thumbs directory

If you want to delete all the Thumbs directories in your iPhoto Library for some reason:

for file in $( find . -name "Thumbs" ) ; do rm -r $file; done


Edit: a better solution by bcrow:
find . -name "Thumbs" -exec rm -r {} \;

Find and change a users uid

Most useful for providing a consistent uid on machines accessing an NFS mount:

$ sudo find . -xdev -user <old-uid> -print -exec chown <new-uid> {} \;

delete a filename starting with a dash

Have you ever created a file by mistakenly adding a flag where you shouldn't? Find the inode number of the file and delete it using find:

[one:~/src/oops] ryanschwartz$ cp test -p
[one:~/src/oops] ryanschwartz$ ls
-p test
[one:~/src/oops] ryanschwartz$ rm -p
rm: illegal option -- p
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
[one:~/src/oops] ryanschwartz$ \rm -p
rm: illegal option -- p
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
[one:~/src/oops] ryanschwartz$ ls -li
total 0
5937393 -rw-r--r-- 1 ryanschwartz textdrive - 0B May 27 18:04 -p
5937392 -rw-r--r-- 1 ryanschwartz textdrive - 0B May 27 18:04 test
[one:~/src/oops] ryanschwartz$ find . -inum 5937393
./-p
[one:~/src/oops] ryanschwartz$ find . -inum 5937393 -exec rm {} \;
[one:~/src/oops] ryanschwartz$ ls -li
total 0
5937392 -rw-r--r-- 1 ryanschwartz textdrive - 0B May 27 18:04 test

Clearing out a bunch of spam with spoofed emails that were bounced back to some poor guy with a catchall email

We don't really want to delete them all just in case.

cd /usr/local/scratch/
mkdir junk
find /var/spool/postfix -exec grep "somediscernible-feature.com" '{}' \; | awk '{print($3)}' | xargs -J X mv X ./junk/


The "find" produces

Binary file /var/spool/postfix/active/D/D8832E38 matches
Binary file /var/spool/postfix/active/D/D78EC1C72 matches
Binary file /var/spool/postfix/active/D/D593D279D matches
Binary file /var/spool/postfix/active/D/D0EB32833 matches


The awk

/var/spool/postfix/active/D/D8832E38
/var/spool/postfix/active/D/D78EC1C72
/var/spool/postfix/active/D/D593D279D
/var/spool/postfix/active/D/D0EB32833


And then the mv, moves it.
« Newer Snippets
Older Snippets »
11 total  XML / RSS feed