 |
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 |
| |
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
|
| |
12-22-2007
|
#3 (permalink)
| | Linux User
Join Date: Aug 2006
Posts: 353
| Quote:
Originally Posted by patch_linams 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
|
| |
12-22-2007
|
#4 (permalink)
| | Just Joined!
Join Date: Nov 2004
Posts: 18
| Quote:
Originally Posted by ghostdog74 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 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
|
| |
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. |
| |
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.
|
| |
12-22-2007
|
#7 (permalink)
| | Linux Enthusiast
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 ) |
| |
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.
|
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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. |
| |