LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-17-2005, 07:01 PM   #1
otheralex
LQ Newbie
 
Registered: Jul 2004
Posts: 6

Rep: Reputation: 0
bash script to sort files by extension


I'm trying to write a simple bash script to sort files based on their extensions. Mostly this is for sorting images. I would like to be able to go into a directory which may contain any combination of several filetypes and put them into separate directories.

Basically I'm thinking of this being a series of if/thens... sort of like this:

if [ there are any files ending with .tif ]
then
mkdir tif
mv *.tif tif/
fi

and so on for .jpg, .dng, etc...

thanks!
 
Old 08-17-2005, 07:24 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Re: bash script to sort files by extension

Quote:
Originally posted by otheralex
I'm trying to write a simple bash script to sort files based on their extensions. Mostly this is for sorting images. I would like to be able to go into a directory which may contain any combination of several filetypes and put them into separate directories.

Basically I'm thinking of this being a series of if/thens... sort of like this:

if [ there are any files ending with .tif ]
then
mkdir tif
mv *.tif tif/
fi

and so on for .jpg, .dng, etc...

thanks!
Did you have a specifc question about what you wanted to do?
 
Old 08-17-2005, 07:44 PM   #3
otheralex
LQ Newbie
 
Registered: Jul 2004
Posts: 6

Original Poster
Rep: Reputation: 0
yes, sorry for not making my original post more clear.

What I don't know how to do is write a script that will look for any files whose names end with a specific extension and if there are any such files, put them in a new directory.

So if the action of the script was written in plain English is would say "if there are any files ending with ".tif" then do the following..."

I hope that clears is up.

Thanks.
 
Old 08-17-2005, 09:52 PM   #4
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
It appears you have the concept now you just need to start producing some code. Post something.
 
Old 08-17-2005, 09:59 PM   #5
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally posted by otheralex
yes, sorry for not making my original post more clear.

What I don't know how to do is write a script that will look for any files whose names end with a specific extension and if there are any such files, put them in a new directory.

So if the action of the script was written in plain English is would say "if there are any files ending with ".tif" then do the following..."

I hope that clears is up.

Thanks.
Well, you can probably just use pattern matching in the shell. E.g.
Code:
#Move all .txt files to subdir txt.
#Only create txt subdir if at least 1 file with .txt extension exists.
for i in $(ls *.txt); do
   mkdir txt
   break
done
mv *.txt txt
 
Old 08-18-2005, 02:02 AM   #6
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 46
i did a script once that does the same thing

here it is

Code:
#!/bin/bash
#
#
#

################################################
#
# Functions
#
################################################

DIR=$(pwd)


Music()
{
if [ -e "$DIR/Music" ];then
       echo -n ""
else
       mkdir Music
fi

mv *.mp3 "$DIR/Music" 2>/dev/null
mv *.wav "$DIR/Music" 2>/dev/null
mv *.midi "$DIR/Music" 2>/dev/null
}

Movies()
{
if [ -e "$DIR/Movies" ];then
       echo -n ""
else
       mkdir Movies
fi

mv *.avi "$DIR/Movies" 2>/dev/null
mv *.mpeg "$DIR/Movies" 2>/dev/null
mv *.mpg "$DIR/Movies" 2>/dev/null
}


Pictures()
{
if [ -e "$DIR/Pictures" ];then
       echo -n ""
else
       mkdir Pictures
fi

mv *.jpeg "$DIR/Pictures" 2>/dev/null
mv *.jpg "$DIR/Pictures" 2>/dev/null
mv *.gif "$DIR/Pictures" 2>/dev/null
mv *.png "$DIR/Pictures" 2>/dev/null
}

Compressed()
{
if [ -e "$DIR/Compressed" ];then
       echo -n ""
else
       mkdir Compressed
fi

mv *.zip "$DIR/Compressed" 2>/dev/null
mv *.rar "$DIR/Compressed" 2>/dev/null
mv *.7z "$DIR/Compressed" 2>/dev/null
mv *.tar "$DIR/Compressed" 2>/dev/null
mv *.tar.bz2 "$DIR/Compressed" 2>/dev/null
mv *.tar.gz "$DIR/Compressed" 2>/dev/null
}


################################################
#
# Main Program
#
################################################

Music
Movies
Pictures
Compressed
 
Old 08-18-2005, 04:32 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,290

Rep: Reputation: 174Reputation: 174
Code:
EXTENSIONS="lsp tk tcl txt"

for ext in $EXTENSIONS ;do
  ls *.$ext | cpio -pd ./$ext
done
$
 
Old 08-19-2005, 02:40 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
Code:
EXTS="ext1 ext2 ext 3"
for ext in $EXTS
do
    mkdir $ext
    mv *.${ext} $ext
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 08:31 PM
bash script to set extension association vi0lat0r Programming 6 05-24-2004 07:04 PM
Identifying files in bash script Cruger Programming 7 03-15-2004 10:49 AM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 07:53 AM


All times are GMT -5. The time now is 01:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration