Title: bash shell notes
Author: Darin Brezeale
Date Created: 06-23-2004
Updated: Sunday, 06-Jul-2008 19:28:27 EDT


  1. Count the number of lines with the_word in each .txt file
  2. for file in *.txt; do 
    	grep the_word $file | wc -l
  3. This code creates directories (under the current directory, as written) for any groups whose names begin with dev-.
  4. for GRP in `getent group | cut -d: -f1 | grep ^dev-`; do
    		mkdir $GRP
  5. Create directories named dev1, dev2, ..., dev10
  6. for (( num=1; num <= 10; num++ )); do
    		mkdir "dev$num"


    for num in $(seq 1 10); do
    		mkdir dev$num
  7. If you need to have a special format, the Unix command seq supports some of the printf standards. For instance, to convert the image files named frame001.jpg, frame002.jpg,..., frame999.jpg to GIFs, I can use
  8. for num in $(seq -f %03g 1 999); do
    		convert frame$num.jpg frame$num.gif
  9. Change all files with the extension out to lowercase
  10. for file in *.out; do
    	mv $file `echo $file | tr '[A-Z]' '[a-z]'`
  11. Change filenames with the extension chm that consist of words separated by periods (e.g., this.is.a.long.name.chm) to use underscores instead except for the period that typically exists between the prefix and file extension. Here sed's '-e' option is used to combine expressions.
  12. for file in *.chm; do 
    	mv $file `echo $file | sed -e 's/\./_/g' -e 's/_chm/.chm/'`
  13. Find all directories which have files within 2 levels of the current location that have changed in the past day. Use sed to get just the directory name and tar it with the current date in the tar'ed filename.
  14. SRC="/windows_data/data"
    DATE=`date '+%y%m%d'`
    cd $SRC
    for file in `find . -maxdepth 2 -mtime -1 | sed 's/\.\///' | sed 's/\(.*\)\/.*/\1/' | sort|uniq`
      if [ ! -e "$file" ]       # Check if file exists.
        echo "$file does not exist."; echo
        continue                # On to next.
      if [ ! "$file" == "." ]
        tar -czf $DEST/$file$DATE.tgz $file 
  15. Count the number of lines in each file with the extension dct and divide it by 480.
  16. for file in *.dct; do
    	LC=`cat $file | wc -l`
    	echo $file
    	COUNT=$(echo "scale=3; $LC / 480" | bc)
    	echo "  lines = " $LC", ratio = " $COUNT
  17. For each line in test.txt, count the number of words.
  18. while read line; do  
    	echo $line | wc -w; 
    done < test.txt
  19. extract the files for user1010 from the compressed files
  20. for file in *sequence.csv.tgz; do 
    	tar tvzf $file | grep user1010 |sed 's/.*\(user1010.*\)/\1/' > tempvar
    	tar xzf $file `cat tempvar`
  21. combine the contents of all user files (which have names of the form user1234-sequence.csv), with each user's part preceded by the user name and separated by a blank line
  22. for file in user*sequence.csv; do 
    	prefix=`basename $file -sequence.csv`
    	echo $prefix >> allusers.txt
    	cat $file >> allusers.txt
    	echo "" >> allusers.txt
  23. rename files of the form test3-q01.c, test3-q02.c,... to test2-q01.c, test2-q02.c,...
  24. for file in test3*.c; do 
        suf=`echo $file | sed 's/test3-//'`
        mv $file test2-$suf


  1. SysAdmin to SysAdmin: Programming with bash
  2. Learning the Bash Shell, 2nd Ed.