How to rename a file using patterns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to rename a file using patterns?
# 1  
Old 05-02-2013
How to rename a file using patterns?

hi,

can somebody help me out if possible.

sone files are there in directory say

Code:
ppp_abc.txt
ppp_def.txt
ppp_ghi.txt

the above 3 files can be retrieved by this pattern

Code:
ppp_*.txt

i want to move these files to another directory and rename them as

Code:
qqq_abc.txt      (for ppp_abc.txt)
qqq_def.txt      (for ppp_def.txt) 
qqq_ghi.txt      (for ppp_ghi.txt)

for these i am using the pattern

Code:
qqq_*.txt

If i use this command

Code:
mv ppp_*.txt ../qqq_*.txt

it shows an error stating last argument should be a directory. (i think because i am using a * ).

is there any way to rename using patterns??
# 2  
Old 05-02-2013
Try
Code:
$ for file in ppp_*.txt
     do mv $file qqq_${file#*_}
     done

# 3  
Old 05-02-2013
Alternatively if your system supports the rename command you can use this:
Code:
rename 's/^ppp/qqq/' *.txt

# 4  
Old 05-02-2013
Quote:
Originally Posted by RudiC
Try
Code:
$ for file in ppp_*.txt
     do mv $file qqq_${file#*_}
     done

can you please explain me the above code? especially the 2nd line.

Code:
mv $file qqq_${file#*_}

Code:
for file in ppp_*.txt

Is ppp_*.txt being treated similar to an array?

---------- Post updated at 02:55 PM ---------- Previous update was at 02:33 PM ----------

Quote:
Originally Posted by Subbeh
Alternatively if your system supports the rename command you can use this:
Code:
rename 's/^ppp/qqq/' *.txt

I tried the above command. its not showing any errors but there is no change in the file names. can you check if you are missing something?
# 5  
Old 05-02-2013
It works for me:
Code:
$ ls -1
ppp_abc.txt
ppp_def.txt
ppp_ghi.txt
$ rename 's/^ppp/qqq/' *.txt
$ ls -1
qqq_abc.txt
qqq_def.txt
qqq_ghi.txt

# 6  
Old 05-02-2013
Quote:
Originally Posted by Subbeh
It works for me:
Code:
$ ls -1
ppp_abc.txt
ppp_def.txt
ppp_ghi.txt
$ rename 's/^ppp/qqq/' *.txt
$ ls -1
qqq_abc.txt
qqq_def.txt
qqq_ghi.txt

i got this

Code:
$ ls -1
ppp_abc.txt
ppp_def.txt
ppp_ghi.txt
$ rename 's/^ppp/qqq/' *.txt
$ ls -1
ppp_abc.txt
ppp_def.txt
ppp_ghi.txt


its not working for me.
# 7  
Old 05-04-2013
Quote:
Originally Posted by Little
can you please explain me the above code? especially the 2nd line.

Code:
mv $file qqq_${file#*_}

Code:
for file in ppp_*.txt

Is ppp_*.txt being treated similar to an array? . . .
cf man bash:
Quote:
for name [ [ in [ word ... ] ] ; ] do list ; done
The list of words following in is expanded, generating a list of items.
. . .
EXPANSION . . .
Parameter Expansion
${parameter#word}
${parameter##word}
Remove matching prefix pattern. . . .
You can follow the execution of the commands by setting the -x (xtrace) option.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Parsing a file with different patterns

I have a text file like this , where the same set of three lines repeat with different values. INFO: processing now 03/25/2015-00:06:05 03/25/2015-00:16:04 ------Invoking myexe table=table1 INFO 25-03 00:20:26,801 - Finished processing (I=0, O=57, R=57, W=57, U=0, E=0) INFO: processing... (4 Replies)
Discussion started by: Lakshmikumari
4 Replies

3. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Replace patterns in a file

Hi all, I here have a file which contains a list of files inside, all of which have the suffix ".log". And now I would wish to replace all the pattern ".log" with ".log.bz2" there. So how could I archive this? Thanks (4 Replies)
Discussion started by: isaacniu
4 Replies

5. Shell Programming and Scripting

Extract certain patterns from file.

Hi All, I tried extracting this pattern using grep but it did not work. What I have is a file which has contents like this: file:///channel/add-adhd.html file:///channel/allergies.html file:///channel/arthritis.html http://mail.yahoo.com/ http://messenger.yahoo.com/... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

6. Shell Programming and Scripting

Finding patterns in a file

Hi, I have a file with 3 columns and I want to find when the average number of rows on column 3 is a certain value. The output will be put into another file indicating the range. Here is what I mean (file is tab separated): hhm1 2 0 hhm1 4 0.5 hhm1 6 0.3 hhm1 8 -1.4... (2 Replies)
Discussion started by: kylle345
2 Replies

7. Shell Programming and Scripting

Replacing patterns in a file

Hi Everybody, Can we replace the contents of a file without using a temporary file. For ex: I need to relpace the value "old" with "new", I can do it using the sed replace pattern, but I do not want to use a temporary file. Also, from the questions posted in the past I could see a... (3 Replies)
Discussion started by: mr_manii
3 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

To find 3 patterns in a file

hy i have a requirement in which my script needs to find 3 patterns in a file and if any pattern is missing it should sent a mail Patterns Interval60min_Daily_readings$a.txt Interval_Daily_readings$a.txt Daily_readings$a.txt Basically i want to test for the above Patterns in the... (2 Replies)
Discussion started by: ali560045
2 Replies

10. Shell Programming and Scripting

grep patterns - File

Hi I have 3 patterns for example to be searched. These three patterns are available in file1. The patterns to be searched are in file2. I want to search the pattern of file1 to file2. Can any one help with example? Regards Dhana (1 Reply)
Discussion started by: dhanamurthy
1 Replies
Login or Register to Ask a Question