LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Processing multiple files in a directory and moving them. (https://www.linuxquestions.org/questions/linux-newbie-8/processing-multiple-files-in-a-directory-and-moving-them-609038/)

keysorsoze 12-25-2007 08:58 AM

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.

tommytomthms5 12-25-2007 09:01 AM

im no expert but you might wanna try commands such as "dd" or "cp"


edit: my book says use "mv"

keysorsoze 12-25-2007 09:10 AM

Ok I tried the following command with no luck as either?

find *.db | sed -e 's/.db//' |xargs cp -R /var/named/new/

pwc101 12-25-2007 09:18 AM

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.

jschiwal 12-25-2007 09:29 AM

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.

tommytomthms5 12-25-2007 09:52 AM

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?

pwc101 12-25-2007 11:01 AM

Code:

for suffix in {0..100}; do
  cp file /your/destination/directory/"file"$suffix;
done

is one way to do it.

tommytomthms5 12-25-2007 11:10 AM

that did a whole lot of nothing anything else that might make say 50 copies of one file

Uncle_Theodore 12-25-2007 11:33 AM

My two cents. :)
Code:

for file in *.db; do mv $file new/${file%.db}; done

pwc101 12-25-2007 11:39 AM

Quote:

Originally Posted by tommytomthms5 (Post 3001221)
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.

keysorsoze 12-25-2007 02:50 PM

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

Uncle_Theodore 12-25-2007 02:54 PM

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! :)

keysorsoze 12-25-2007 02:56 PM

Thanks Uncle_Theodore, I just gave that a shot. I didn't know you could run for loops at the terminal.

tommytomthms5 12-25-2007 07:09 PM

pwc101 yours worked after i figured out how to use it.... thank you

pwc101 12-26-2007 05:27 AM

Quote:

Originally Posted by tommytomthms5 (Post 3001445)
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:45 AM.