#!/bin/sh # # The reason for this prog was that I had brought in a bunch of my old # stuff from a vfat partition and as a result everything was marked # rwxrwxrwx # # Some potential file exte message="\ # This program is derived from recurse_fname_print # It uses recursion to traverse a directory tree and test each file # name for a matching extension, like .txt or .cpp, at which point # it will change permissions to those specified. # # Usage: # $0 dir layers ext mode # # Where: # dir - is the starting directory (layer 1) # layers - is how many layers of subdirectory to recurse into. # ext - is the file extensions to check for. # Use double quotes on multiple extensions. # eg: \".txt .c .cpp .bmp\" # mode - is the new mode to set in octal form. eg: 444 # # \".c .cpp .h .txt .dat .jpg .bmp\" #\n" if [ $# -lt 3 ]; then echo -e "\n Error: Too few arguments: \n$message \n"; exit; fi; function list_dir() { local last_d=$(pwd); echo "---- In list_dir () $1 (from $last_d) ----"; cd $1; local i=0; local fn; for fn in $(ls); do ((i++)); if [ -f $fn ]; then ((filect++)) echo "$i fn= $fn"; for ea in $ext; do if [[ $fn =~ $ea$ ]]; then echo " .ext IS MATCH: $fn =~ $ea$ "; echo $(ls -go $fn); chmod $mode $fn; echo $(ls -go $fn); ((changect++)); fi; done; elif [ -d $fn ]; then if [[ $layerct -lt $layers ]]; then echo "Recursing to \"$fn\" "; ((dirct++)); ((layerct++)); list_dir $fn; ((layerct--)); else echo "lcount Prevented from entry to layer: $lcount $fn"; fi; else echo "Undetermined file type"; fi; done; cd $last_d; echo "---- changing back to $last_d ----\n"; } echo "##### Begin: ##### "; tfiles=0; old_d=$(pwd); start_d=$1; layers=$2; ext=$3; mode=$4; layerct=1; filect=0; dirct=1; changect=0; if [ $layers -le 0 ]; then echo "invalid value entered for layers: $layers"; exit 1; fi; ext=${ext//\\} # removes all escape characters ext=${ext//./\\.} # replaces all . with \. if [ -d $start_d ]; then list_dir $start_d; fi; echo; echo "Stats:"; echo " filect = $filect dirct = $dirct changect = $changect "; echo; echo "##### End ##### "; cd $old_d; exit 0; ##################################################################### # ~/xdir> chmod -R 755 ~/xdir # ~/xdir> ls -goR ~/xdir/warns/* # -rwxr-xr-x. 1 5839 Apr 9 13:03 warns/global_scope-1cpp # -rwxr-xr-x. 1 1552 Apr 9 13:03 warns/global_scope-1cpp.cpp # -rwxr-xr-x. 1 4640 Apr 9 13:03 warns/top_level-1c # -rwxr-xr-x. 1 1282 Apr 9 13:03 warns/top_level-1c.c # # ~/xdir> recurse_chmod_on.ext ~/xdir 3 ".c .cpp .h .txt .dat .jpg .bmp .png .html .htm .xhtml" 664 > xfile # ~/xdir> tail xfile # -rw-rw-r--. 1 1282 Apr 9 13:03 top_level-1c.c # ---- changing back to /home/hlenderk/xdir ----\n # 120 fn= xfile # 121 fn= xfile2 # ---- changing back to /home/hlenderk/xdir ----\n # # Stats: # filect = 533 dirct = 57 changect = 288 # # ##### End ##### # ~/xdir> ls -goR warns/* # -rwxr-xr-x. 1 5839 Apr 9 13:03 warns/global_scope-1cpp # -rw-rw-r--. 1 1552 Apr 9 13:03 warns/global_scope-1cpp.cpp # -rwxr-xr-x. 1 4640 Apr 9 13:03 warns/top_level-1c # -rw-rw-r--. 1 1282 Apr 9 13:03 warns/top_level-1c.c # # ~/xdir> ls -goR ~/xdir > xfile # cool dude! # and for the big test # ~> du -sh ~/ap # 2.2G /home/hlenderk/ap # ~> time { recurse_chmod_on.ext ~/ap 10 ".c .cpp .h .txt .dat .jpg .bmp .png .html .htm .xhtmli .csv" 664 > xfile; } # real 1m48.823s # user 0m29.103s # sys 1m2.358s # ~> tail xfile # .ext IS MATCH: test_upload_file.txt =~ \.txt$ # -rw-rw-r--. 1 42 Oct 23 2008 test_upload_file.txt # -rw-rw-r--. 1 42 Oct 23 2008 test_upload_file.txt # ---- changing back to /home/hlenderk/ap ----\n # ---- changing back to /home/hlenderk ----\n # Stats: # filect = 17612 dirct = 1411 changect = 7823 ##### End ##### # -go xfile # -rw-rw-r--. 1 1609716 Apr 11 00:02 xfile # wow # mv xfile ap/chmod_notes.txt