folder manipulation tools
This commit is contained in:
28
folderFlatten
Executable file
28
folderFlatten
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
function help() {
|
||||
echo "Flatten the directory structure from the current folder downwards"
|
||||
echo "Files in subfolders will be renamed / -> _"
|
||||
echo "Empty folders are removed"
|
||||
echo -- " add -f to force action "
|
||||
}
|
||||
|
||||
function helpexit() {
|
||||
help
|
||||
exit
|
||||
}
|
||||
[[ -z "$1" ]] && {
|
||||
help
|
||||
echo "Are you sure? Break with ctrl-c"
|
||||
read i
|
||||
}
|
||||
[[ "$1" = "-h" ]] && helpexit
|
||||
|
||||
IFS=$'\n'
|
||||
|
||||
for f in $( find . -mindepth 2 -type f -path '*.*' -printf %P'\n' );
|
||||
do mv -iv "$f" "$( echo $f | sed s,[/],_,g )"
|
||||
done
|
||||
find . -depth -type d -empty -delete
|
||||
|
||||
|
||||
|
||||
68
folderSplitter
Executable file
68
folderSplitter
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
|
||||
function helpexit() {
|
||||
echo "FolderSplitter: copy files in this folder to subfolders in equal distribution"
|
||||
echo "usage: FolderSplitter [numberOfFolders] [rootFolder]"
|
||||
echo " If rootFolder omitted, current working directory used "
|
||||
exit
|
||||
}
|
||||
[[ -z "$1" ]] && helpexit
|
||||
[[ "$1" = "-h" ]] && helpexit
|
||||
|
||||
|
||||
if [ -z "$2" ]
|
||||
then indir=$( pwd )
|
||||
else
|
||||
if [ -d "$2" ]
|
||||
then indir="$2"
|
||||
else
|
||||
echo $2 does not exist.
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
if [ -z "$1" ]
|
||||
then echo need a number to how many folders to split.
|
||||
exit 1
|
||||
fi
|
||||
if [[ $1 == +([0-9]) ]]
|
||||
then n=$1
|
||||
else
|
||||
echo $1 is not integer
|
||||
exit
|
||||
fi
|
||||
|
||||
# make the list separator a rowchange
|
||||
IFS=$'\n'
|
||||
# list files
|
||||
pushd "$indir"
|
||||
infiles=( $( ls ) )
|
||||
fip=0
|
||||
# create directories
|
||||
for (( d=1; d<=$n; d++ ))
|
||||
do str="dir"$d
|
||||
mkdir "${str}" || echo error creating directory ${str}
|
||||
done
|
||||
|
||||
perdir=$(( ${#infiles[@]} / $n ))
|
||||
|
||||
dip=1
|
||||
fip=1
|
||||
for (( f=0; f<${#infiles[@]}; f++ ))
|
||||
do cp -lav "${infiles[$f]}" dir"$dip"/ || echo error copying file ${infiles[$f]}
|
||||
if (( $fip > $perdir ))
|
||||
then dip=$(($dip+1))
|
||||
fip=1
|
||||
else
|
||||
fip=$(($fip+1))
|
||||
fi
|
||||
if (( $dip > $n ))
|
||||
then dip=$n
|
||||
fi
|
||||
done
|
||||
|
||||
for (( d=1; d<=$n; d++ ))
|
||||
do echo "dir"$d with $( ls dir"$d" | wc --lines ) files
|
||||
done
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user