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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-25-2007, 08:58 AM
|
#1
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Rep:
|
Processing multiple files in a directory and moving them.
Hi! I am trying to rename a large number of files in a particular directory and move them into a new directory.
Ex: /var/named
file1.db, file2.db file3.db - rename to file (strip off .db) and move to new directory /var/named/new/file, file2, file3
Here is my attempt to complete this but its not functioning:
cd /var/named
echo *.db | sed -e 's/.db//' |xargs `mv /var/named/new`
What am I doing wrong?
Thanks.
|
|
|
12-25-2007, 09:01 AM
|
#2
|
Member
Registered: Sep 2007
Distribution: debian based
Posts: 308
Rep: 
|
im no expert but you might wanna try commands such as "dd" or "cp"
edit: my book says use "mv"
Last edited by tommytomthms5; 12-25-2007 at 09:07 AM.
|
|
|
12-25-2007, 09:10 AM
|
#3
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Original Poster
Rep:
|
Ok I tried the following command with no luck as either?
find *.db | sed -e 's/.db//' |xargs cp -R /var/named/new/
|
|
|
12-25-2007, 09:18 AM
|
#4
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep: 
|
Code:
for original in ./*.db; do
cp $original /var/named/new/$(basename $original .db)
done
Substitute mv for cp if you want to move the files.
|
|
|
12-25-2007, 09:29 AM
|
#5
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You could use variable substitution:
Code:
for file in *.db; do
mv "$file" $DESTDIR/"${file%.db}"
done
The %.db will remove .db from the end of the variable.
|
|
|
12-25-2007, 09:52 AM
|
#6
|
Member
Registered: Sep 2007
Distribution: debian based
Posts: 308
Rep: 
|
if i wanted to take say "file" and copy it multiple times to a directory like file0,file1,file2,file3,file4 etc. how would i do it?
|
|
|
12-25-2007, 11:01 AM
|
#7
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep: 
|
Code:
for suffix in {0..100}; do
cp file /your/destination/directory/"file"$suffix;
done
is one way to do it.
|
|
|
12-25-2007, 11:10 AM
|
#8
|
Member
Registered: Sep 2007
Distribution: debian based
Posts: 308
Rep: 
|
that did a whole lot of nothing anything else that might make say 50 copies of one file
|
|
|
12-25-2007, 11:33 AM
|
#9
|
Member
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896
Rep:
|
My two cents.
Code:
for file in *.db; do mv $file new/${file%.db}; done
|
|
|
12-25-2007, 11:39 AM
|
#10
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep: 
|
Quote:
Originally Posted by tommytomthms5
that did a whole lot of nothing anything else that might make say 50 copies of one file
|
Your original request was:
Quote:
Originally Posted by tommytomthms5
take say "file" and copy it multiple times to a directory
|
which I took to mean copy a file (called "file" in the example I gave) multiple times.
Code:
for suffix in {0..100}; do
input_file=/some/file/you/want/to/copy
cp $input_file /some/destination/directory/$(basename $input_file)"$suffix"; done
done
That will take a file as defined in the variable input_file and output it to /some/destination/directory named whatever $input_file was called, but with a number added as a suffix.
Please clarify if that's not what you meant.
Some pleases and thankyous (plus some commas) wouldn't go amiss either.
Last edited by pwc101; 12-25-2007 at 11:46 AM.
|
|
|
12-25-2007, 02:50 PM
|
#11
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Original Poster
Rep:
|
Guys using the script below works perfectly, but can't we run this as a command in conjunction with xargs? Just curious if not I'll just use the script. I would ideally like to run this as a command for a quick job rather than scripting out a file. I never new %.db was even possible. Need to do some reading on variable substitution.
for original in ./*.db; do
cp $original /var/named/new/$(basename $original .db)
done
Thanks for the help once again guys.
Edit Sorry I did not try at the CLI that did the job without a script. Sweet!!!!
for file in *.db; do mv $file new/${file%.db}; done
Last edited by keysorsoze; 12-25-2007 at 02:55 PM.
|
|
|
12-25-2007, 02:54 PM
|
#12
|
Member
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896
Rep:
|
What prevents you from running this as a command? Just type this in a terminal
for original in ./*.db; do cp $original /var/named/new/$(basename $original .db); done
and voila! 
|
|
|
12-25-2007, 02:56 PM
|
#13
|
Member
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295
Original Poster
Rep:
|
Thanks Uncle_Theodore, I just gave that a shot. I didn't know you could run for loops at the terminal.
|
|
|
12-25-2007, 07:09 PM
|
#14
|
Member
Registered: Sep 2007
Distribution: debian based
Posts: 308
Rep: 
|
pwc101 yours worked after i figured out how to use it.... thank you
|
|
|
12-26-2007, 05:27 AM
|
#15
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep: 
|
Quote:
Originally Posted by tommytomthms5
pwc101 yours worked after i figured out how to use it.... thank you
|
No problem 
|
|
|
All times are GMT -5. The time now is 08:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|