Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-30-2006, 07:15 AM   #1
LQ Newbie
Registered: Apr 2006
Posts: 7

Rep: Reputation: 0
Post how to use shell scripts for sorting files

How to write a shell script that will move all the files from the /unsorted directory into the proper sub-directories in the /gameassets directory. Files with the .mp3 extension should be moved into the music directory, files with the .png extension should be moved into the graphics directory, and files with the .exe extension should be moved into the executables directory. Furthermore, each file must be sorted alphabetically and renamed with a prefix number.

Old 10-30-2006, 07:24 AM   #2
Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Please post further details/examples.
Moving files to subdirs is easy:
mv /unsorted/*.[Mm][Pp]3 /gameassets/music
and similarly for the png's.

For alphabetical sorting, try looking into the "sort" utility.
Renaming can be done via simple scripting (ie "for" loops & "if" statements) and "mv" commands.
Old 10-30-2006, 07:29 AM   #3
Registered: Jan 2004
Location: Duesseldorf /Germany
Distribution: Gentoo amd64 / Debian
Posts: 226

Rep: Reputation: 30
Hmmm, smells like a homework assignment to me.....

Maybe you should first splitup your problems.
I hope it is not required to write a one liner.

In bash you could start with the following:

ls | while read FILE;
EXT=`echo $FILE | cut -d\. -f2`
echo $FILE has extension $EXT;

see; first "ls" all files,
then for each filename, try to find the correct file extension.
Then do somthing with it.

The code to find the extension is not really good, it assumes there is only 1 DOT in the filename.
You probably should use some sed/awk regex stuff.
Old 10-30-2006, 07:58 AM   #4
LQ Newbie
Registered: Apr 2006
Posts: 7

Original Poster
Rep: Reputation: 0
More details.

Before running the shell script:
Contents of /unsorted folder:
kylie.mp3, madonna.mp3, textureone.png, logo.png, nolife.exe, footasy.exe, woos.exe

Contents of music, graphics and executables directories:
Empty, no files inside.

After running the shell script:
Contents of /unsorted folder:
Empty, no more files inside.

Contents of music directory:

Contents of graphics directory:

Contents of executables directory:

Last edited by New2Linux06; 10-30-2006 at 08:58 AM.
Old 10-30-2006, 08:02 AM   #5
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
I'm afraid it's against the forum rules for us to do your homework for you:

  • Do not expect LQ members to do your homework - you will learn much more by doing it yourself.


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
handling multiple files in shell scripts. wilsonsamm Linux - General 7 07-05-2006 06:36 PM
sorting a file in shell script shmuel_orgad Programming 11 05-20-2006 08:16 PM
Sorting files in BASH deleted/ Linux - Newbie 16 01-26-2006 06:03 AM
I'm terrible at writing shell scripts. A little help fetching files? ashibaka Linux - General 6 04-10-2004 12:41 AM
Editing files from shell scripts? SirRobbin Linux - Newbie 6 03-23-2004 10:27 AM > Forums > Linux Forums > Linux - Newbie

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

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