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 »
8 total  XML / RSS feed 

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

Add all new files to Subversion

This solution is shell-agnostic, unlike http://textsnippets.com/posts/show/450, and cleaner than to do a --force. I recommend adding it to a alias in your shell, i.e "sall".

svn st | grep "^?" | awk -F "      " '{print $2}' | xargs svn add


Can be modified to do a svn delete on files you accidentally rm -rf'ed:

svn st | grep "^!" | awk -F "      " '{print $2}' | xargs svn delete

Add all new files to Subversion

Bash script to add all new files to Subversion

for i in `svn st | grep ? | awk -F "      " '{print $2}'`; do svn add $i; done

opening in a textmate project all files that match a pattern

This will pass all matched filenames to textmate, generating a project with all files flatly in the drawer (no dirs)

egrep -lr pattern * | grep -v .svn | xargs mate

Searching with grep & Spotlight's kMDItemDisplayName


# search for a file or folder (path) name that contains the specified search string
mdfind 'kMDItemDisplayName == "*apple*"wc'
mdfind -0 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename
mdfind -0 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename | grep Apple
mdfind -0 -onlyin ~/Desktop 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename
mdfind '*==*' | grep -i apple
mdfind '*==*' | grep -i -E '/[^/]*apple[^/]*$'
mdfind '*==*' | grep -i -E '/[^/]*apple[^/]*$' | while read filename; do basename "$filename"; done



# the following is an example of bash process substitution
# http://wooledge.org:8000/BashFAQ (FAQ 20 & 24)
# http://wooledge.org:8000/ProcessSubstitution

# read matched file names into an array

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
mdfind_search="\"*search_string*\"wc"   # alternative

while read -d $'\0' filename; do 
   ar[i++]="$filename"
done < <(mdfind -0 -onlyin ${dir} "kMDItemDisplayName == ${mdfind_search}")

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



# let mdfind & kMDItemDisplayName search for the fixed part of the search string, and grep for the variable one
# read the matched file names into an array

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then     # match entire path name with grep
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then     # match last part of path name with grep
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemDisplayName == ${mdfind_search}") 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



# To search only for files add: 'kMDItemKind != "Folder" && ...'

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

item1="'*text*'wc"
item2="'*document*'wc"
item3="'*source*'wc"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemKind != 'Folder' && kMDItemDisplayName == ${mdfind_search}"); 
#done < <(mdfind -0 -onlyin ${dir} "( kMDItemKind != 'Folder' && ( kMDItemKind == ${item1} || kMDItemKind == ${item2} || kMDItemKind == ${item3}) ) && kMDItemDisplayName == ${mdfind_search}"); 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



# To search only for folders add: 'kMDItemKind == "Folder" && ...'

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemKind == 'Folder' && kMDItemDisplayName == ${mdfind_search}"); 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo

grep-based phrase search with Spotlight

Some examples:


# for mdfind "phrase* " and "phrase! " also work, but not "phrase ! "
mdfind -0 -onlyin ~ "phrase This is a" | xargs -0 grep -ails -E 'This is a phrase!'

mdfind -0 "It's just $8.99!" | xargs -0 grep -ails -E "It's just \\\$8\\.99\\!"

mdfind -0 'Fr\303\251d\303\251ric Chopin' | xargs -0 grep -ails -Z -E 'Fr.d.ric Chopin' | xargs -0 basename

Regex-based file/folder search with Spotlight

To get more Spotlight search options use the commands mdimport -A and mdls file.txt in Terminal.app.


mdfind -onlyin ~ "*==*" | grep -i -E 'part.*of.*name'

mdfind -onlyin ~ 'kMDItemContentType == "text"wc' | grep -i -E '.*\.txt$'
#mdfind -onlyin ~ '*==* && kMDItemContentType == "text"wc' | grep -i -E '.*\.txt$'

mdfind -onlyin ~ 'kMDItemKind == "Folder"' | grep -i -E '.*photos.*[0-5]'


View processes, grep out by user and then kill all their PIDs

ps axu | grep user | kill -9 `awk{print $2}�`
« Newer Snippets
Older Snippets »
8 total  XML / RSS feed