Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
#!/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
You can’t perform that action at this time.