ricbaral

Bash script to prefix filenames with their containing folder

Oct 11th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. DRYRUN=false
  4.  
  5. while getopts 'd' flag; do
  6.   case "${flag}" in
  7.     d) DRYRUN=true ;;
  8.     *) error "Unexpected option ${flag}" ;;
  9.   esac
  10. done
  11.  
  12. for dir in *; do
  13.     if test -d "$dir"; then
  14.         (
  15.             cd $dir
  16.             for file in *; do
  17.                 if test -f "$file"; then
  18.                     (
  19.                         newfile=$dir.${file}
  20.                         if [ "$DRYRUN" = true ] ; then
  21.                             echo "$file" ../"$newfile"
  22.                         else
  23.                             mv "$file" ../"$newfile"
  24.                         fi
  25.                     )
  26.                 fi
  27.             done
  28.         )
  29.     fi
  30. done
Advertisement
Add Comment
Please, Sign In to add comment