Skip to main content
edited tags
Source Link
Gilles 'SO- stop being evil'
  • 865.4k
  • 205
  • 1.8k
  • 2.3k

sorts Sort files according to their extensions

Suggestion need to overcome some limitations for this script or totally new scirpt if it works more smartly:
II have made a script that will sort files according to their extension and place them in the proper folder. For example, place for example, place abc.jpg in folder jpg
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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

sorts files according to their extensions

Suggestion need to overcome some limitations for this script or totally new scirpt if it works more smartly:
I have made a script that will sort files according to their extension and place them in proper folder. for example, place abc.jpg in folder 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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

Sort files according to their extensions

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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

added 47 characters in body
Source Link
Alex Jones
  • 6.5k
  • 18
  • 54
  • 85

Suggestion need to overcome some limitations for this script or totally new scirpt if it works more smartly:
I have made a script that will sort files according to their extension and place them in proper folder. for example, place abc.jpg in folder 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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

Suggestion need to overcome some limitations for this script:
I have made a script that will sort files according to their extension and place them in proper folder. for example, place abc.jpg in folder 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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

Suggestion need to overcome some limitations for this script or totally new scirpt if it works more smartly:
I have made a script that will sort files according to their extension and place them in proper folder. for example, place abc.jpg in folder 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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.

Source Link
Alex Jones
  • 6.5k
  • 18
  • 54
  • 85

sorts files according to their extensions

Suggestion need to overcome some limitations for this script:
I have made a script that will sort files according to their extension and place them in proper folder. for example, place abc.jpg in folder 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:

  1. it involves use of IFS, it is said to avoid use of IFS, as much as possible
  2. it does not sorts file without file extensions
  3. it will sort files like abc.tar.bz in folder named bz, but however such a file should go in tar.bz folder
  4. 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 named i.am.live.in.india.and.i.study.computer.science.txt will be placed in folder named study.computer.science.txt

you may also suggest any tweaks to make this script more smaller and neat.