Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, January 5, 2012

print urls in a css file

Print the URLs in a css file:

sed -rn 's/^.*url\((.+\.[a-z]+)\).*$/\1/p' file.css

I had a css file that referenced a some images in a directory that contained lots of images and wanted to move just the images referenced in the css file to a new directory, so ended doing something along the lines of:

for p in $(sed -rn 's/^.*url\((.+\.[a-z]+)\).*$/\1/p' file.css); do cp ~/old_path/css/$p new_path/$p; done

Thursday, December 22, 2011

download jquery

Once or twice a month I'll find myself working on some random little project with a web UI and so of course I need jquery at which point I wonder if I have the latest version.. hence, a little script to get the latest and greatest:

#!/bin/bash

wget $(\
 wget -qO - http://docs.jquery.com/Downloading_jQuery |\
 sed -rn 's#^.*(http://code.jquery.com/jquery-[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.min.js).*$#\1#p' |\
 head -n 1
 )

todo: check local jquery copy to see if there actually is a new version we need to download

Monday, December 5, 2011

string find and replace in files

The -i option to sed does in place file editing, however it "modifies" each file it touches whether it actually does any replacing or not (so the file mod time is updated). By filtering through grep first, we can be sure we are only messing with files we actually want to. Replace "happy" with "blammo":
find . -type f -print0 | xargs -0 grep -lZ "happy" | xargs -0 sed -i 's/happy/blammo/g'
The -l option to grep prints file names only and stops with the first match (so no duplicates). -Z uses null byte for separator instead of newline.

Monday, November 14, 2011

import sql files

import all files matching *.mysql in current directory into database "db"
for f in *.mysql; do echo $f && mysql -u username -psecretpw db < $f ; done
echos the file name as it goes

Saturday, November 12, 2011

recursive file list sorted by mod time

I end up bouncing from one project to another pretty often, and I don't like saving my workspace in my editor so that I can start with a nice, clean, blank slate. But it's nice to know what exactly I've been working on recently, so..
find . -type f | xargs ls -l 2>/dev/null | sort -r -k6,6 -k7,7 | most

Friday, November 4, 2011

hexdump

Quick little one liner for hexdump with some nice, versatile output. 1 byte per line, with
byte #[space]octal value[space]decimal value[space]hex value[space]default character set
hexdump -v -e '"%07_ad" 1/1 " %03o"' -e '1/1 " %03u"' -e '1/1 " %03x"' -e '1/1 " %_c \n"'
And in a bash script taking a file:
#!/bin/bash

hexdump -v -e '"%07_ad" 1/1 " %03o"' -e '1/1 " %03u"' -e '1/1 " %03x"' -e '1/1 " %_c \n"' $1
Example output when called on itself:
0000000 043 035 023 # 
0000001 041 033 021 ! 
0000002 057 047 02f / 
0000003 142 098 062 b 
0000004 151 105 069 i 
0000005 156 110 06e n 
0000006 057 047 02f / 
0000007 142 098 062 b 
0000008 141 097 061 a 
0000009 163 115 073 s 
0000010 150 104 068 h 
0000011 012 010 00a \n 
0000012 012 010 00a \n 
0000013 150 104 068 h 
0000014 145 101 065 e 
0000015 170 120 078 x 
0000016 144 100 064 d 
0000017 165 117 075 u 
0000018 155 109 06d m 
0000019 160 112 070 p 
0000020 040 032 020   
0000021 055 045 02d - 
0000022 166 118 076 v 
0000023 040 032 020   
0000024 055 045 02d - 
0000025 145 101 065 e 
0000026 040 032 020   
0000027 047 039 027 ' 
0000028 042 034 022 " 
0000045 063 051 033 3 
0000046 157 111 06f o 
0000047 042 034 022 " 
0000048 047 039 027 ' 
0000049 040 032 020   
0000050 055 045 02d - 
0000051 145 101 065 e 
0000052 040 032 020   
0000053 047 039 027 ' 
0000054 061 049 031 1 
0000055 057 047 02f / 
0000056 061 049 031 1 
0000057 040 032 020   
0000058 042 034 022 " 
0000059 040 032 020   
0000060 045 037 025 % 
0000061 060 048 030 0 
0000062 063 051 033 3 
0000063 165 117 075 u 
0000064 042 034 022 " 
0000065 047 039 027 ' 
0000066 040 032 020   
0000067 055 045 02d - 
0000068 145 101 065 e 
0000069 040 032 020   
0000070 047 039 027 ' 
0000071 061 049 031 1 
0000072 057 047 02f / 
0000073 061 049 031 1 
0000074 040 032 020   
0000075 042 034 022 " 
0000076 040 032 020   
0000077 045 037 025 % 
0000078 060 048 030 0 
0000079 063 051 033 3 
0000080 170 120 078 x 
0000081 042 034 022 " 
0000082 047 039 027 ' 
0000083 040 032 020   
0000084 055 045 02d - 
0000085 145 101 065 e 
0000086 040 032 020   
0000087 047 039 027 ' 
0000088 061 049 031 1 
0000089 057 047 02f / 
0000090 061 049 031 1 
0000091 040 032 020   
0000092 042 034 022 " 
0000093 040 032 020   
0000094 045 037 025 % 
0000095 137 095 05f _ 
0000096 143 099 063 c 
0000097 040 032 020   
0000098 134 092 05c \ 
0000099 156 110 06e n 
0000100 042 034 022 " 
0000101 047 039 027 ' 
0000102 040 032 020   
0000103 044 036 024 $ 
0000104 061 049 031 1 
0000105 012 010 00a \n 

Sunday, October 23, 2011

ubuntu oneiric ocelot write desktop background image file

here is a script which takes in path to one or more directories containing images and writes a gnome desktop background file to show those images

#!/bin/bash

usage()
{
cat << EOF
usage: $0 -t transition_time -s static_time (-d dirs|-r recursive_dirs)

OPTIONS:
 -h      Show this message
 -t      required. time of transition between images, in seconds
 -s      required. time each image is show
 -d      comma separated list of directories containing images.
 -r      comma separated list of directories that contain images and
         other directories with more images

Either dirs, recursive_dirs, or both is required
EOF
if [[ -z $1 ]]; then exit 1; else exit $1; fi
}

set_img_paths()
{
 dir_path=$1
 find_opts=$2
 
 for file_path in $(find ${dir_path} ${find_opts} -type f)
 do
  mime_type=$(file --mime-type ${file_path})
  if [[ $mime_type =~ ^.*:[[:space:]]+image/(.*)$ ]]
  then
   img_paths[${#img_paths[*]}]=$file_path
  fi
 done
}

while getopts ":hd:r:t:s:" x
do
 case $x in
  h) usage 0 ;;
  t) transition_time=$OPTARG ;;
  s) static_time=$OPTARG ;;
  d) dirs[${#dirs[*]}]=$OPTARG ;;
  r) recursive_dirs[${#recursive_dirs[*]}]=$OPTARG ;;
 esac
done

if [[ -z $transition_time ]]; then usage; fi
if [[ -z $static_time ]]; then usage; fi
if [[ ${#dirs[*]} -eq 0 && ${#recursive_dirs[*]} -eq 0 ]]; then usage; fi

# array for our image paths
declare -a img_paths

# loop over directories to search non-recursively for images
for ((i=0; i<${#dirs[*]}; i++))
do
 set_img_paths "${dirs[$i]}" "-maxdepth 1"
done

# loop over directories to search recursively for images
for ((i=0; i<${#recursive_dirs[*]}; i++))
do
 set_img_paths "${recursive_dirs[$i]}" ""
done

# make sure we have some images
if [[ ${#img_paths[*]} -eq 0 ]]
then
 echo "Error: could not find any images"
 exit 2
fi

# randomize our array
random_order=$(seq ${#img_paths[*]} | shuf)
i=0
for r in $random_order
do
 randomized[$i]=${img_paths[$r-1]}
 i=$(( $i + 1 ))
done

# write our background xml file to stdout
echo ""
for ((i=0; i<${#randomized[*]}; i++))
do
 img_path=${randomized[$i]}
 echo -n "
 
  ${static_time}
  ${img_path}
 
"
 
 # transition to next image, or, if we are showing the last image, transition back to first image
 [[ $(( $i + 1 )) -lt ${#randomized[*]} ]] && next_img=${randomized[$i+1]} || next_img=${randomized[0]}
 echo -n "
 
  ${transition_time}
  ${img_path}
  ${next_img}
 
"
done
echo ""

Tuesday, October 18, 2011

oneiric ocelot 11.10 set background to xml file

gsettings set org.gnome.desktop.background picture-uri 'file:///path/to/file/background.xml' 
 
http://askubuntu.com/questions/67218/how-do-i-create-a-desktop-wallpaper-slideshow-in-oneiric


as best I can tell this property is stored in ~/.gconf/desktop/gnome/background/%gconf.xml

more to come on this in a bit

Thursday, September 29, 2011

remove svn dirs

I'm sure this is out there on the internet already somewhere, but

find . -name ".svn" -type d | xargs rm -rf

Monday, September 26, 2011

add unversioned files to svn

svn st | sed -rn 's/^\?\s+(.*)$/\1/p' | xargs svn add