LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-15-2009, 08:42 PM   #1
JackieBrown
Member
 
Registered: Dec 2004
Location: San Antonio, TX
Distribution: Debian-AMD64 Sid
Posts: 481

Rep: Reputation: 31
Solved - Trying to create directories based on file names


I have everything in one large directory currently.

Example

movie.mkv
movie.tbn
movie.jpg

I want each to have it's own directory but there is a lot to manually do this.

I know I have done this before (actually, I used to do the opposite and delete based on parts of file names) but I am drawing a blank.

I am googling now, but wanted to post in case some one is quicker

Last edited by JackieBrown; 05-15-2009 at 09:44 PM.
 
Old 05-15-2009, 08:49 PM   #2
rayfordj
Member
 
Registered: Feb 2008
Location: Texas
Distribution: Fedora, RHEL, CentOS
Posts: 488

Rep: Reputation: 78
there are various ways to implement. if all of your files are of the *.??? structure you could do something like:

Code:
for i in $(ls *.mkv); do BNAME=$(basename ${i} .mkv); mkdir ${BNAME} && mv ${BNAME}.* ${BNAME}/; done
from your example i suspect something like this might do it for you, but it really does depend on your file name structure, ...


 
Old 05-15-2009, 09:04 PM   #3
JackieBrown
Member
 
Registered: Dec 2004
Location: San Antonio, TX
Distribution: Debian-AMD64 Sid
Posts: 481

Original Poster
Rep: Reputation: 31
That looks good (I tried it on a jpg folder.)

basename is what I was missing (and I never would have thought of it. I was too focused on making grep work for this.)

Thanks
 
Old 05-15-2009, 09:13 PM   #4
JackieBrown
Member
 
Registered: Dec 2004
Location: San Antonio, TX
Distribution: Debian-AMD64 Sid
Posts: 481

Original Poster
Rep: Reputation: 31
Actually, it doesn't work with files with spaces in them.

I guess this makes sense since mkdir Hello World would create a directory Hello and a directory World.

What I would need is the equivalent of mkdir "Hello World"

I just rename the " " to "_"

Last edited by JackieBrown; 05-15-2009 at 09:44 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script that create a blank file in all sub-directories. samengr Programming 8 02-18-2009 05:00 PM
Automation Script !! Create automated archive of files based on name of file! yoshima Programming 5 09-28-2007 05:17 AM
copying multiple different files in different directories with identical file names nickleus Linux - Software 4 03-09-2006 01:25 PM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM
Create an archive file which includes files in sub-directories? amaze Linux - General 1 09-26-2003 08:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:18 PM.

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