Created
August 14, 2016 15:09
-
-
Save uriel1998/6c57a5b6a1346301532c6766624b730d to your computer and use it in GitHub Desktop.
Small shell script to copy each file in a directory into a subdirectory named the same as the filename (but not extension!) and then move the file into it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dir=$(echo "$PWD") | |
for f in * | |
do | |
if [ -f "$f" ];then | |
# this is redundant due to how I'm searching, but why not? | |
filename=$(basename "$f") | |
extension="${filename##*.}" | |
file_only="${filename%.*}" | |
if [ "$file_only" == "$f" ];then | |
mv "$f" "$f.tmp" | |
fullfile=$(echo "$dir/$f.tmp") | |
else | |
fullfile=$(echo "$dir/$f") | |
fi | |
if [ -d "$file_only" ];then | |
echo "Subdirectory $file_only exists" | |
else | |
newdir="$dir/$file_only" | |
mkdir "$newdir" | |
newfile="$newdir/$f" | |
mv "$fullfile" "$newfile" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment