Help in UNIX shell to copy part of file name to new file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in UNIX shell to copy part of file name to new file name
# 1  
Old 01-11-2014
Facebook Help in UNIX shell to copy part of file name to new file name

Hi,
I want to do the following in a Unix shell script and wonder if someone could assist me?

I want to take files in a specific directory that start with the name pxpur012 and copy them to the same directory with the file name not containg pxpur012. For example, I have files like pxpur012_1.html, pxpur012_10.html, pxpur012_20.html, etc. . I want to take these file and copy each to the same directory but minus the pxpur012. The end result will be new files named as follows:
1.html, 10.html, 20.html.


I may also want to add a character to the name, but not sure yet.

Any help would be greatly appreciated.

Thanks,

Linda
# 2  
Old 01-11-2014
You can try something like this:

Code:
for x in *.html;do mv -vv $x ${x/px*_/};done

# 3  
Old 01-11-2014
HI,

I tried that but replaced the mv with cp and errored with the message:

Code:
${x/px*_/}: The specified substitution is not valid for this command.



Here is the command I put in a shell script and ran the shell script:

Code:
for x in *.html;do cp -vv $x ${x/px*_/};done





thanks,

Linda

Last edited by Scrutinizer; 01-12-2014 at 03:58 AM.. Reason: code tags
# 4  
Old 01-11-2014
Or
Code:
for f in *.html; do cp "$f" "${f#*_}"; done

# 5  
Old 01-11-2014
Hi yoda,

this works. If I wanted to replace the file if it already exists what do I add?


thanks to all for the help!!


LindaSmilie

---------- Post updated at 09:26 PM ---------- Previous update was at 09:05 PM ----------

Hi,
forget about replacing the file , I wasn't thinking straight and know what to do.

Thanks again for everyone's help! Greatly appreciated!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy part of file between two strings to another

I am a newbie to shell scripting I have a large log file , i need to work on the part of the log file for a particular date. Is there a way to find the first occurance of the date string and last occurance of the next day date date string and move this section to a new file. to explain it... (3 Replies)
Discussion started by: swayam123
3 Replies

2. Shell Programming and Scripting

To display the selected part in text file of unix

0400903071220312 20120322 20:21 1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363 12122011AUS 182644 000C2 8122011 0000 000 1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363 12122011AUS ... (6 Replies)
Discussion started by: rammm
6 Replies

3. UNIX for Dummies Questions & Answers

Copy the last part since the file has been updated

Input File1 constatntly running and growing in size. My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 123 terminated **ID PIN 12345 Comamnd Successful Command Terminated Command Successful Command Terminated **My Program Erorr ddmmyy hh:mm:ss My Program Error **Port 345... (3 Replies)
Discussion started by: eurouno
3 Replies

4. Programming

how to copy downloaded file into my source file folder (putty/unix)

I need to "Ensure that when you download libchat.a from the VLE you have copied it to the same folder on ius as your source files. You then refer to the library (and the libraries it needs) with: gcc -o outputfile sourcefile.c -L. -lchat -lsocket -lnsl" But I have no idea what this means! (I... (2 Replies)
Discussion started by: fakuse
2 Replies

5. UNIX for Dummies Questions & Answers

Copy a part of file

Hi, I want to copy text between expressions ">bcr1" and ">bcr2" to another file. Any simple solutions? Thanks (4 Replies)
Discussion started by: alpesh
4 Replies

6. UNIX for Dummies Questions & Answers

Copy a windows CVS file to the unix server as a svs file

I so desperately need a script to copy a windows csv file to my unix server and i know these should be at dummies but i have no bits. it is life & no job situation help please. thanks (1 Reply)
Discussion started by: zhegal
1 Replies

7. Shell Programming and Scripting

Perl or Awk script to copy a part of text file.

Hi Gurus, I'm a total newbie to Perl and Awk scripting. Let me explain the scenario, there is a DB2 table with 5 columns and one of the column is a CLOB datatype containing XML. We need all the 4 columns but only a portion of string from the XML column. We decided to export DB2 table to a .del... (26 Replies)
Discussion started by: asandy1234
26 Replies

8. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

9. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

10. UNIX for Dummies Questions & Answers

Can I copy only part of my file to a new file?

Hi, I am looking for command to allow me only copying certain lines of records in my file to a new file. Can I do that? (3 Replies)
Discussion started by: whatisthis
3 Replies
Login or Register to Ask a Question