Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
becki:sources:file_utilities [2008-09-16 13:22] becki |
becki:sources:file_utilities [2009-04-24 09:57] (aktuell) becki |
||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| ====== File Utilities == | ====== File Utilities == | ||
| - | Hopefully useful scripts to convert file names, handle access rights, convert between charsets and indent size handling | + | Hopefully useful scripts to convert file names, handle access rights, convert between charsets and indent size handling. |
| + | |||
| + | Download latest version [[http://think-deep.com/becki/sources/pack.php?n=fileutils|here]] | ||
| ===== Filename Manipulation == | ===== Filename Manipulation == | ||
| Zeile 7: | Zeile 9: | ||
| ''sbsp2un'': Convert all spaces in passed filenames to underscores. Not recursive. | ''sbsp2un'': Convert all spaces in passed filenames to underscores. Not recursive. | ||
| - | |||
| - | <code bash> | ||
| - | #!/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 | ||
| - | </code> | ||
| ==== Make Filenames lowercase == | ==== Make Filenames lowercase == | ||
| - | ''sblowerc'': Make filenames and directory names lowercase recursively in whole directory trees: | + | ''sblowerc'': Make filenames and directory names lowercase recursively in whole directory trees. |
| - | + | ||
| - | <code bash> | + | |
| - | #!/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 | + | |
| - | </code> | + | |
| ===== Change Access Permissions == | ===== Change Access Permissions == | ||
| ==== Permissions for Files and Dirs == | ==== Permissions for Files and Dirs == | ||
| - | ''sbchmod'': Change access permissions recursively and separately for files and directories: | + | ''sbchmod'': Change access permissions recursively and separately for files and directories. |
| - | + | ||
| - | <code bash> | + | |
| - | #!/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 | + | |
| - | </code> | + | |
| ===== Character Set Conversion == | ===== Character Set Conversion == | ||
| Zeile 140: | Zeile 23: | ||
| ''sblatin2utf'': Convert text files from Latin1 (ISO-8859-1) to UTF-8 and vice versa: | ''sblatin2utf'': Convert text files from Latin1 (ISO-8859-1) to UTF-8 and vice versa: | ||
| - | |||
| - | <code bash> | ||
| - | #!/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 | ||
| - | </code> | ||
| ===== Spaces, Tabs and Indent == | ===== Spaces, Tabs and Indent == | ||
| Zeile 218: | Zeile 28: | ||
| ''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. | ''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. | ||
| - | |||
| - | <code bash> | ||
| - | #!/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 | ||
| - | </code> | ||
| ==== Cange indentation width == | ==== Cange indentation width == | ||
| ''sbindent'': Cange indentation width eg. of source code files | ''sbindent'': Cange indentation width eg. of source code files | ||
| - | |||
| - | <code bash> | ||
| - | #!/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 | ||
| - | </code> | ||