LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 02-15-2013, 11:22 AM   #1
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Rep: Reputation: 0
copy and move problem


Hi to all!!

I have some problems using copy and move comand. I need to move/copy many files from dir1 to dir2 but i must only move the file with 2 random characters in the name somting like this:
text@@.txt where @-random character, next problem that i have is how to move many files ant the same time somting like this mv {file1.f,text1.txt,db.db} and rename it at same time.

Last edited by ed88; 02-15-2013 at 11:51 AM.
 
Old 02-15-2013, 12:46 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
First thing, in order to move a file that having some special characters in it, use escape sequence (i.e. \) before special characters, like :
Code:
~$ mv text\@\@.txt /path/to/directory
Second, in order to rename and move multiple files to another directory, you can create a small script. Bur let's know what exaclty you want with files file1.f, text1.txt, db.db - just want to move them to some other directory, or rename them, or copy them, or rename and then move to some dir. or what? And did you try anything so far?

Last edited by shivaa; 02-15-2013 at 12:48 PM.
 
1 members found this post helpful.
Old 02-15-2013, 01:22 PM   #3
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by shivaa View Post
First thing, in order to move a file that having some special characters in it, use escape sequence (i.e. \) before special characters, like :
Code:
~$ mv text\@\@.txt /path/to/directory
Second, in order to rename and move multiple files to another directory, you can create a small script. Bur let's know what exaclty you want with files file1.f, text1.txt, db.db - just want to move them to some other directory, or rename them, or copy them, or rename and then move to some dir. or what? And did you try anything so far?
I wanna to move it to other dir and rename it at the same time somting like that move dir1/(file1.f, text1.txt, db.db) to dir2/(old_fole1.f, old_text1.txt, old_db.db)
 
Old 02-15-2013, 07:52 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
In that case, a script could do your job, like:
Code:
#!/bin/bash
find dir1 -type f -exec mv '{}' dir2 \;
cd dir2
for file in $(ls *)
do
mv $file old_$file
echo "$file renamed"
done
 
1 members found this post helpful.
Old 02-16-2013, 08:33 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Code:
#!/bin/bash
cd /dir1
for file in *
do
    mv $file /dir2/old_$file
done
 
2 members found this post helpful.
Old 02-17-2013, 05:20 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You just need to learn the power of shell globbing:

globbing
extended globbing


There's also the similar feature called brace expansion:

brace expansion


Do recognize, however, that globbing only matches patterns, while brace expansion generates lists of strings. So you usually do not want to use the latter when moving and copying, as you'll end up with errors on unmatched filenames.

Finally, when using globbing, I recommend using the "-t" option in cp/mv to explicitly set the target directory first, then follow it with the pattern.

Edit: I just saw the desire to rename at the same time. To do that you need to use a loop and perhaps some parameter substitution.

Code:
for fname in file?.f text??.txt *.db; do

    mv -t newdir "old_$fname"

done

(PS: @shiiva, you're still doing the DRLWF thing! Don't use ls to list the filenames, just loop directly over the globbing pattern. )

Last edited by David the H.; 02-17-2013 at 05:28 PM. Reason: as stated
 
  


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
FTP problem: can create n move file n folders but can't copy... Sayan Acharjee Linux - Server 8 03-04-2010 01:23 AM
When moving a file, how do I get it to ask me if I want to move or copy? Rockgod1969 Linux - Software 1 09-14-2008 04:22 PM
move/copy command Cyberian Linux - Newbie 1 11-23-2004 07:50 AM
how to move and copy files.... JMK Linux - Newbie 3 01-22-2004 05:57 PM
can move but not copy matt Linux - General 3 08-01-2001 08:08 AM


All times are GMT -5. The time now is 12:08 PM.

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