LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-30-2006, 08:15 AM   #1
New2Linux06
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, 08:24 AM   #2
timmeke
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, 08:29 AM   #3
huibert.alblas
Member
 
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:

<code>
ls | while read FILE;
do
EXT=`echo $FILE | cut -d\. -f2`
echo $FILE has extension $EXT;
done
</code>

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, 08:58 AM   #4
New2Linux06
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:
1-kylie.mp3
2-madonna.mp3

Contents of graphics directory:
1-logo.png
2-textureone.png

Contents of executables directory:
1-footasy.exe
2-nolife.exe
3-woos.exe

Last edited by New2Linux06; 10-30-2006 at 09:58 AM.
 
Old 10-30-2006, 09:02 AM   #5
pwc101
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:

http://www.linuxquestions.org/linux/rules.html

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


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


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration