LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 12-25-2007, 09:58 AM   #1
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Rep: Reputation: 30
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.
 
Old 12-25-2007, 10:01 AM   #2
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Rep: Reputation: 30
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 10:07 AM.
 
Old 12-25-2007, 10:10 AM   #3
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Ok I tried the following command with no luck as either?

find *.db | sed -e 's/.db//' |xargs cp -R /var/named/new/
 
Old 12-25-2007, 10:18 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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.
 
Old 12-25-2007, 10:29 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.
 
Old 12-25-2007, 10:52 AM   #6
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Rep: Reputation: 30
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?
 
Old 12-25-2007, 12:01 PM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Code:
for suffix in {0..100}; do
   cp file /your/destination/directory/"file"$suffix;
done
is one way to do it.
 
Old 12-25-2007, 12:10 PM   #8
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Rep: Reputation: 30
that did a whole lot of nothing anything else that might make say 50 copies of one file
 
Old 12-25-2007, 12:33 PM   #9
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
My two cents.
Code:
for file in *.db; do mv $file new/${file%.db}; done
 
Old 12-25-2007, 12:39 PM   #10
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by tommytomthms5 View Post
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 12:46 PM.
 
Old 12-25-2007, 03:50 PM   #11
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
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 03:55 PM.
 
Old 12-25-2007, 03:54 PM   #12
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 60
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!
 
Old 12-25-2007, 03:56 PM   #13
keysorsoze
Member
 
Registered: Apr 2004
Location: Queens, NY
Distribution: Red Hat, Solaris
Posts: 295

Original Poster
Rep: Reputation: 30
Thanks Uncle_Theodore, I just gave that a shot. I didn't know you could run for loops at the terminal.
 
Old 12-25-2007, 08:09 PM   #14
tommytomthms5
Member
 
Registered: Sep 2007
Distribution: debian based
Posts: 305

Rep: Reputation: 30
pwc101 yours worked after i figured out how to use it.... thank you
 
Old 12-26-2007, 06:27 AM   #15
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by tommytomthms5 View Post
pwc101 yours worked after i figured out how to use it.... thank you
No problem
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 08:56 PM
How do I serch for files on multiple directory levels? vasco-t Ubuntu 5 03-15-2007 04:51 PM
Moving or copying a directory into multiple directories oadvantage Linux - Newbie 3 03-07-2006 04:47 AM
symlink multiple files to new directory with new extension jmanjohn61 Linux - General 1 01-06-2005 01:55 PM
Moving multiple files krazykow Linux - Software 3 09-03-2004 06:20 PM


All times are GMT -5. The time now is 01:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration