Skip to content

Instantly share code, notes, and snippets.

@uriel1998
Created August 14, 2016 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uriel1998/6c57a5b6a1346301532c6766624b730d to your computer and use it in GitHub Desktop.
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.
#!/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