summarylogtreecommitdiffstats
path: root/fsed
blob: 4277cea85eb297e718cd1d630eb32e063823463c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Prerequisites:
#   Have any Unix/Linux environment with bash installed
# Install:
#   Copy this file to one of your $PATH directories, then `chmod a+x <path_to_this_file>`
# Usage:
#   fsed 's/.js/.min.js/g' *.js

if [ $# = 0 ]; then
  echo "Usage: fsed <PATTERN> [FILE]..."
  echo "'fsed --help' for more information."
  exit 0
fi

PATTERN=
MV_ARGS=
IN_PLACE=

for arg in "$@"; do
  if [ "$arg" = "--help" ] || [ "$arg" = "-h" ]; then
    echo "Usage: fsed [-iv] <PATTERN> [FILE]..."
    echo "Rename files in batch using sed syntax."
    echo ""
    echo "Example:"
    echo "  Dry run: fsed 's/.tgz/.tar.gz/' *.tgz"
    echo "  Replace: fsed -i 's/.tgz/.tar.gz/' *.tgz"
    echo ""
    echo "Arguments:"
    echo "  PATTERN is in sed syntax, will be applied on file names."
    echo "  -h, --help          print this help page"
    echo "  -i, --in-place      change the file names"
    echo "  -v, --verbose       rename files and print"
    exit 0
    exit 0
  elif [ "$arg" = "--verbose" ] || [ "$arg" = "-v" ]; then
    MV_ARGS="$MV_ARGS -v"
  elif [ "$arg" = "--in-place" ] || [ "$arg" = "-i" ]; then
    IN_PLACE=true
  elif [ "$PATTERN" = "" ]; then
    PATTERN=$arg
  else
    TO=$(echo $arg | sed "$PATTERN")
    if [ "$IN_PLACE" = "true" ]; then
      [ "$arg" != "$TO" ] && mv $MV_ARGS "$arg" "$TO"
    else
      echo $TO
    fi
  fi
done