Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Help with make directory and move files to that directory
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-20-2007   #1 (permalink)
Just Joined!
 
Join Date: Dec 2007
Posts: 2
Help with make directory and move files to that directory

I am looking for some help in making directories and moving files to them. I am doing it manually and since I have tens of thousands of these files to move, I figure there must be an easier way to do it.
Here is what I have and want to do.

In one directory I have tens of thousands of .txt files named as such:

rdb-001-001.txt
rdb-001-002.txt
rdb-002-001.txt
rdb-002-002.txt
and so on.
What I want to do( and not manually ) is
mkdir rdb-001
mv rdb-001-* rdb-001
I want a script that can grab a file name, make the directory and move all corresponding files to that directory.
Any help? Pointers as to what program to use? Or is a script the best?
Thanks
eldergod is offline   Reply With Quote
Old 12-20-2007   #2 (permalink)
Just Joined!
 
Join Date: Nov 2004
Posts: 18
Something like this should do the trick:
Code:
#!/bin/sh

DIR=""
for F in $(ls -1 *.txt); do
    D="${F%-*}"
    if [ "$DIR" != "$D" ]; then
	DIR="$D"
	mkdir -p $DIR
    fi
    mv -f "$F" "$DIR"    
done
patch_linams is offline   Reply With Quote
Old 12-22-2007   #3 (permalink)
Linux User
 
Join Date: Aug 2006
Posts: 353
Quote:
Originally Posted by patch_linams View Post
Something like this should do the trick:
Code:
#!/bin/sh

DIR=""
for F in $(ls -1 *.txt); do
    D="${F%-*}"
    if [ "$DIR" != "$D" ]; then
	DIR="$D"
	mkdir -p $DIR
    fi
    mv -f "$F" "$DIR"    
done
actually, there's no need to check for existing directory since you have used -p for mkdir. Also, its better to use shell expansion, instead of ls -1 *txt when passing it to the for loop. If files has spaces in them, using ls -1 will break the for loop.

so the whole thing might be
Code:
for F in *.txt; do
    mkdir -p ${F%-*.txt}
    mv -f "$F" ${F%-*.txt}
done
ghostdog74 is offline   Reply With Quote
Old 12-22-2007   #4 (permalink)
Just Joined!
 
Join Date: Nov 2004
Posts: 18
Quote:
Originally Posted by ghostdog74 View Post
its better to use shell expansion, instead of ls -1 *txt when passing it to the for loop. If files has spaces in them, using ls -1 will break the for loop.
Since the exact naming scheme of all files is known it won't be necessary to check for spaces in filenames. But I agree that "ls" is not needed.
Quote:
Originally Posted by ghostdog74 View Post
Well, since the exact format of all files is known
so the whole thing might be
Code:
for F in *.txt; do
    mkdir -p ${F%-*.txt}
    mv -f "$F" ${F%-*.txt}
done
well, your mkdir statement will create several directories if there are spaces in filenames. You might want to add double quotation marks
This would improve both versions:
Code:
#!/bin/sh
for F in *.txt; do
    D="${F%-*.txt}"
    mkdir -p "$D"
    mv -f "$F" "$D"
done
patch_linams is offline   Reply With Quote
Old 12-22-2007   #5 (permalink)
Just Joined!
 
Join Date: Dec 2007
Posts: 2
Thanks for all the great help!

I've kind of added/tweaked a little from each reply and its working fantastic!! Actually I'm completely finished moving the files!! It sure saved me tons of work. I knew there was a reason I left Windows
Thanks to everyone who replied, I can't thank you enough for all the help.
eldergod is offline   Reply With Quote
Old 12-22-2007   #6 (permalink)
Just Joined!
 
Join Date: Dec 2007
Location: Ceredigion, Wales,UK.
Posts: 10
Help with make directory and move files to that directory

As a very new bee in the scripting hive I prefer to google my way out of trouble whenever possible, but one line of the proposed script has me completely lost. What does:-

"${F%-*.txt}" mean?

I know what it is doing in this context, but can find no reference to "F%", or anything like it in the man pages, the numerous tutorials I have studied, nor from googling "mkdir -p ${".

Enlighten me, someone, please.
__________________
When push comes to shove, you're on your own.
Hatrick is offline   Reply With Quote
Old 12-22-2007   #7 (permalink)
drl
Linux Enthusiast
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Solaris, SuSE, Xandros
Posts: 688
Hi, Hatrick.

A few things are deep within man bash. Look for the heading Parameter Expansion in that man page. You'll find some interesting descriptions of % and # in such expressions.

If you wish to see some examples and references, look through http://www.tldp.org/LDP/abs/html/index.html -- it's long, but valuable.

And, of course, there is nothing quite like experimentation.

And a pat on the back for doing your own searching first ... cheers, drl
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Old 12-22-2007   #8 (permalink)
Just Joined!
 
Join Date: Dec 2007
Location: Ceredigion, Wales,UK.
Posts: 10
Thanks drl. Heading for man bash, right now.
__________________
When push comes to shove, you're on your own.
Hatrick is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't display JPEG files in CGI-BIN metalx1000 Servers 0 12-08-2007 06:36 PM
Cinelerra 2.1 Niche25 Redhat / Fedora Linux Help 2 11-16-2007 05:29 PM
Linux File Permissions GoldenEye Linux Tutorials, HOWTO's & Reference Material 0 07-16-2004 01:50 PM
Suddenlly broken virtual hosting mooman_fl Servers 1 07-13-2004 09:09 PM
KDE basics with some Tips and Tricks flw Linux Tutorials, HOWTO's & Reference Material 0 07-17-2003 02:14 AM

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 04:28 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0