LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-24-2011, 04:13 PM   #1
valunthar
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Rep: Reputation: Disabled
Script to sort files based on part of file name


I'm trying to write a script that would organize files in a directory based on part of a file name, but I'm running into a few snags in figuring out some of the logic. So far the script creates a new folder based on the name of the file in question and move the file into the new directory. However the current solution would only exacerbate the problem as it would put each file of a series into it's own folder rather than grouping the series into one directory. Nor does it take into account folders that have already been created. Also the filenames in this folder don't use a specific naming convention and usually hold a lot of extraneous information so tools such as sed would be of limited use.

Here's what I've come up with so far


Code:
#!/bin/bash

for f in $1; do
  dir=$1/$f
  mkdir -p "$dir"
  mv "$f" "$dir"
done
Any help with ironing out these bugs would be greatly appreciated.
 
Old 06-24-2011, 06:28 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I don't quite understand what you mean by organizing files within a directory. What does that mean?

You can display names differently according to different attributes, using various options in ls, or the sort command, or such. But you can't change their true alphanumeric sorting order unless you rename everything.

So are you trying to rename all the files? If so, show us an actual example of the existing file and directory structure, and what you want it to look like afterwards.

If you have some other purpose in mind, please elaborate.

In other words, what is your real goal?
 
Old 06-24-2011, 06:33 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,647
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Why do it in bash?

In a very short Perl script you can slurp in a list of files, sort it using a cmp function that considers only part of the name and ... presto.

In a variety of other high-level programming tools, all of which are available on your Linux box, you can do the same.
 
Old 06-24-2011, 07:53 PM   #4
valunthar
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Bash is the only language I'm familiar with, though I am open to suggestions for other languages if it will do the job. Would you happen to have any code suggestions?
 
Old 06-24-2011, 09:16 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How about you add some examples of your file-names, and how they
are meant to relate to the directories you want to move them in?


Cheers,
Tink
 
Old 06-24-2011, 11:25 PM   #6
valunthar
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Some examples of file names would be

[releasegroup]series name-episode number.extension
[releasegroup]series name-episode number[md5 hash].extension
[releasegroup]series name-episode number[codec].extension
[releasegroup]series name-episode number[resolution].extension

I'm pretty sure that covers all of the permutations in that folder. Most file names contain spaces, while a few use underscores instead. Pretty much all of them uses dashes to separate the series name and episode number, but there are a couple that don't use them at all.

As far as the folder structure is concerned I would like to move all of the files pertaining to a specific series into a folder that corresponds to the name of the series in question.
 
Old 06-25-2011, 06:22 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The way this would normally be done is to loop through the filenames, extract the title part from the name, use that to create a directory, and then move the file into it.

But you can only automate this if there's some way to clearly and reliably differentiate the text you want from the text that you don't want. If there's no regular pattern to the filenames, then it's likely to be difficult to impossible to do this.

Perhaps if you showed us some actual examples of the variations in filenames that could be encountered, we could come up with something that would work in the majority of cases. For the above file patterns, for example, this might work:

Code:
IFS=$'\n'   # sets field separator to newline; makes the script ignore
	    # spaces when word-splitting

for file ; do    #no need for "in", for loops default to reading "$@"

	dir="${file#*]}"    #strip everything up to the first "]" in the name
	dir="${dir%%-*}"    #strip everything after the first "-" in the name
	dir="${dir// /_}"   #replaces spaces with underscores in the dirname.
				 
	mkdir -p "$dir"
	mv -t "$dir" "$file"

done
This is not a very robust extraction though. It will only work on that very specific pattern where the title comes between "]" and "-".

Frankly, if I were you I'd first work to get all the files renamed in a consistent pattern. Then sorting them would be a cinch.
 
Old 06-25-2011, 07:02 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,647
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Quote:
Originally Posted by valunthar View Post
Bash is the only language I'm familiar with, though I am open to suggestions for other languages if it will do the job. Would you happen to have any code suggestions?
It definitely should not be, because on your Linux system you will find (or can easily install for free):There are so many "tools for the job" out there, which are specifically intended to do that job, it makes little sense IMHO to use Bash.

The most significant element of these languages, in practice, is the very large library of contributed code that accompanies it. For instance, look at http://search.cpan.org for Perl. (Go ahead... type something... type anything...) All of these languages have similar.

And if you really love to do "programming in the half-shell," the Korn shell is just your ticket. (Minus the red sauce and good wine, unfortunately.)

When you start "digging deep" into the Windows system, well, you discover that it's not actually that deep. On the other hand, when you start poking around on a Linux box, even with what's installed there by default, you see that it's an Energizer® Bunny ... "it just keeps going, and going, and going."

Last edited by sundialsvcs; 06-25-2011 at 07:10 AM.
 
Old 06-26-2011, 03:56 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I have to disagree with the above. This is exactly the type of situation that shell scripting is designed for. After all, it only requires a simple loop and text pattern matching, along with directory creation and file renaming. Bash can do these just as well as any other language (assuming that there are actually usable patterns to match, as I talked about earlier).

Sure, you may be able to do it slightly better (read faster or more efficiently) if you already know perl or another language, but if you don't already know it, then why take hours or even days out of your time to study and write your own (likely poor) first attempts, when you can use a language you're already familiar with to do the same thing?

Suggesting learning other languages is a good thing, but suggesting learning one just to solve the problem at hand is not very helpful. Particularly when it can be accomplished without taking that extra step.

Last edited by David the H.; 06-26-2011 at 03:58 AM.
 
  


Reply



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
[SOLVED] Bash Script; Sort files into directory based on data in the file name MTAS Programming 31 10-06-2010 11:47 AM
Way to sort a files based on the creation time thangappan Linux - Software 6 07-09-2010 02:18 AM
Sort files in directories based on the files date... CharlieMike73 Programming 5 09-09-2009 10:04 PM
Automation Script !! Create automated archive of files based on name of file! yoshima Programming 5 09-28-2007 05:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:57 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
Open Source Consulting | Domain Registration