Benutzer-Werkzeuge

Webseiten-Werkzeuge


becki:sources:file_utilities

Dies ist eine alte Version des Dokuments!


File Utilities

Hopefully useful scripts to convert file names, handle access rights, convert between charsets and indent size handling

Filename Manipulation

Get rid of Spaces

sbsp2un: Convert all spaces in passed filenames to underscores. Not recursive.

#!/bin/bash
# File:    sbsp2un
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Todo:    Make script recursive with -R flag
# Description:
#   see usage
 
scna=$(basename $0)
 
usage="Usage:\n\
  $scna file1 [file2 ...] - to convert spaces in filenames to underscores"
 
# Test number of command line arguments:
if [ $# -lt 1 ]; then
    echo "Error: Argument is missing!"
    echo -e $usage
    exit 1
fi
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
    mv "$arg" "$(echo "$arg" | tr ' ' '_')"; #rename would also be possible
done

Make Filenames lowercase

sblowerc: Make filenames and directory names lowercase recursively in whole directory trees:

#!/bin/bash
# File:    sblowerc
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Todo:    Recursion only on demand with -R flag
# Description:
#   see usage
 
scna=$(basename $0)
 
usage="Usage:\n\
  $scna file1 [file2 ...] - to recursively make the filenames lowercase"
 
# Test number of command line arguments:
if [ $# -lt 1 ]; then
    echo "Error: Argument is missing!"
    echo -e $usage
    exit 1
fi
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
 
    # If file exists and is a directory:
    if [ -d $arg ]; then
        pushd $arg > /dev/null
        if [ $? -eq 0 ]; then
            #echo "recursing in" *
            $0 * # this starts recursion
            #echo "recursing ready" *
            popd > /dev/null
        #else
            #echo "Error: Couldn't pushd $arg"
        fi
    fi
 
    buf=$(echo $arg | tr '[:upper:]' '[:lower:]')
    #buf=$(echo $arg | tr '[:lower:]' '[:upper:]')
    if [ "$arg" != "$buf" ]; then
        mv "$arg" "$buf"
    fi
done

Change Access Permissions

Permissions for Files and Dirs

sbchmod: Change access permissions recursively and separately for files and directories:

#!/bin/bash
# File:    sbchmod
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Description:
#   see usage
 
scna=$(basename $0)
 
usage="Usage:\n\
  $scna dirmod filemod file1 [file2 ...] - change access permissions recursively and separately for files and directories"
 
# Test number of command line arguments:
if [ $# -lt 3 ]; then
    echo "Error: Too few arguments!"
    echo -e $usage
    exit 1
fi
dirmod=$1
filemod=$2
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
 
    # If file exists and is a directory:
    if [ -d $arg ]; then
        chmod $dirmod $arg
        pushd $arg > /dev/null
        if [ $? -eq 0 ]; then
            $0 $dirmod $filemod * # this starts recursion
            popd > /dev/null
        fi
 
     # If file exists and is a regular file:
    elif [ -f $arg ]; then
        chmod $filemod $arg
    fi
done

Character Set Conversion

Convert Latin1 to UTF-8

sblatin2utf: Convert text files from Latin1 (ISO-8859-1) to UTF-8 and vice versa:

#!/bin/bash
# File:    sblatin2utf
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Description:
#   see usage
# Todo:
#   Test script with filenames containing spaces!
# Installation:
#   Save this script to /usr/local/bin/sblatin2utf
#   Crate a symlink: /usr/local/bin/sbutf2latin -> sblatin2utf
 
 
lati='ISO-8859-1'
utf8='UTF-8'
l2uf='sblatin2utf'
u2lf='sbutf2latin'
scna=$(basename $0)
tmpf=/tmp/$scna.$$
 
usage="$scna Usage:\n\
  $l2uf file1 [file2 ...] - to convert files from $lati to $utf8\n\
  $u2lf file1 [file2 ...] - to convert files from $utf8 to $lati"
 
 
printError() {
    echo "$scna Error: $1" >&2
}
 
printErrorAndExit() {
    printError "$1"
    echo -e $usage >&2
    exit $2
}
 
 
# Get direction of conversion:
case "$scna" in
    $l2uf ) fenc=$lati; tenc=$utf8;;
    $u2lf ) fenc=$utf8; tenc=$lati;;
    *     )
        printErrorAndExit "Rename this script to $l2uf or $u2lf!" 2
    ;;
esac
 
# Test number of command line arguments:
if [ $# -lt 1 ]; then
    printErrorAndExit "Argument is missing!" 1
fi
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
    if [ -w $arg ]; then
        iconv -f $fenc -t $tenc $arg > $tmpf
        if [ $? -eq 0 ]; then
            cat  $tmpf > $arg # no mv, because mv resets the file permissions
            #mv $tmpf $arg
        else
            printError "iconv failed to convert $arg"
        fi
    else
        printError "$arg is not a file or is write protected"
    fi
done
 
if [ -e $tmpf ]; then
    rm $tmpf
fi

Spaces, Tabs and Indent

Tabs to Spaces

sbexpand: Convert tabs to spaces and vice versa. Works like expand and unexpand commands, but opeates directly on the file instead of writing to stdout.

#!/bin/bash
# File:    sbexpand
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Description:
#   see usage
# Installation:
#   Save this script to /usr/local/bin/sbexpand
#   Crate a symlink: /usr/local/bin/sbunexpand -> sbexpand
 
expa='sbexpand'
unex='sbunexpand'
scna=$(basename $0)
tmpf=/tmp/$scna.$$
tabsize=8
 
usage="$scna Usage:\n\
  same as (un)expand commands (see 'man expand'),\n\
  but the file(s) are convertetd directly instead of sending it to stdout"
 
printError() {
    echo "$scna Error: $1" >&2
}
 
printErrorAndExit() {
    printError "$1"
    echo -e $usage >&2
    exit $2
}
 
 
# Get direction of conversion:
case "$scna" in
    $expa ) command='expand';;
    $unex ) command='unexpand';; # not tested yet!
    *     )
        printErrorAndExit "Rename this script to $expa or $unex!" 2
    ;;
esac
 
# Get Tabsize:
while getopts "t:" Option; do
    case $Option in
        t ) tabsize=$OPTARG;;
        * ) echo "hallo"; exit 1 ;;
    esac
done
shift $(($OPTIND - 1)) # Decrements the argument pointer
 
# Test number of command line arguments:
if [ $# -lt 1 ]; then
    printErrorAndExit "Filenames are missing!" 1
fi
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
    if [ -w $arg ]; then
        $command -t $tabsize $arg > $tmpf
        if [ $? -eq 0 ]; then
            cat  $tmpf > $arg # no mv, because mv resets the file permissions
        else
            printError "$command failed to convert $arg"
        fi
    else
        printError "$arg is not a file or is write protected"
    fi
done
 
 
if [ -e $tmpf ]; then
    rm $tmpf
fi

Cange indentation width

sbindent: Cange indentation width eg. of source code files

#!/bin/bash
# File:    sbindent
# Version: 2008-09-16
# Author:  Stefan Beckert  http://wiki.think-deep.com/becki:start
# License: Public Domain
#          No warranty expressed or implied. Use at your own risk.
# Description:
#   see usage
 
scna=$(basename $0)
tmpf=/tmp/$scna.$$
oldindent=''
newindent=''
 
usage="$scna Usage:\n\
  $scna -o oldIndentSize -n newIndentSize file1 [file2 [...]]\n\
  Note that there must not be any hard tabs in the source file(s).\n\
  Use bfuexpand to convert them to soft tabs first."
 
printError() {
    echo "$scna Error: $1" >&2
}
 
printErrorAndExit() {
    printError "$1"
    echo -e $usage >&2
    exit $2
}
 
# Get Tabsize:
while getopts "o:n:" Option; do
    case $Option in
        o ) oldindent=$OPTARG;;
        n ) newindent=$OPTARG;;
        * ) exit 1 ;;
    esac
done
shift $(($OPTIND - 1)) # Decrements the argument pointer
 
# do we have everything that we need?
if [ ! "$oldindent" ]; then
    printErrorAndExit "old indent size is missing! (-o)" 1
fi
if [ ! "$newindent" ]; then
    printErrorAndExit "new indent size is missing! (-n)" 1
fi
if [ $# -lt 1 ]; then
    printErrorAndExit "filenames are missing!" 1
fi
 
# do the work:
for arg in "$@"; do  # $@ sees arguments as separate words.
    if [ -w $arg ]; then
        unexpand -t $oldindent --first-only $arg > $tmpf # spaces 2 tabs
        expand   -t $newindent $tmpf > $arg # tabs 2 spaces
    else
        printError "$arg is not a file or is write protected"
    fi
done
 
# clean up
if [ -e $tmpf ]; then
    rm $tmpf
fi
Cookies helfen bei der Bereitstellung von Inhalten. Diese Website verwendet Cookies. Mit der Nutzung der Website erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Computer gespeichert werden. Außerdem bestätigen Sie, dass Sie unsere Datenschutzerklärung gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website. Weitere Information
becki/sources/file_utilities.1221571334.txt.gz · Zuletzt geändert: 2009-04-24 09:57 (Externe Bearbeitung)

Impressum - Datenschutzerklärung