I have made a script that will sort files according to their extension and place them in the proper folder. For example, place abc.jpg
in the directory jpg
.
#!/bin/bash
#this script sorts files according to their extensions
oldIFS=$IFS
IFS=$'\n'
(find . -type f) > /tmp/temp
for var in `cat /tmp/temp`
do
name=`basename "$var"`
ext=`echo $name | cut -d'.' -f2- | cut -d'.' -f2- | cut -d'.' -f2- | cut -d'.' -f2- | cut -d'.' -f2- | cut -d'.' -f2- | cut -d'.' -f2-`
mkdir -p $ext
mv "$var" $ext/ 2> /dev/null
done
IFS=$oldIFS
problem with this script:
- it involves use of IFS, it is said to avoid use of IFS, as much as possible
- it does not sorts file without file extensions
- it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
- see line 9 of my script; if any file contain more no. of dots(in its name) than no. of
cut -d'.' -f2-
in the script than if will result in file name taken in extension part.
for example, a file namedi.am.live.in.india.and.i.study.computer.science.txt
will be placed in folder namedstudy.computer.science.txt
you may also suggest any tweaks to make this script more smaller and neat.
abc.tar.bz2
should go into thetar.bz2
folder, why shouldn'ti.am.live.in.india.and.i.study.computer.science.txt
go into a folder namedam.live.in.india.and.i.study.computer.science.txt
?