Solved

cp files from mulitple directories into one directory LINUX (and rename files too)

Posted on 2010-08-26
4
466 Views
Last Modified: 2013-12-16
Hi--

I have a situation where i need to copy many files from many different subdirectories into another directory.
I am using the code below, BUT it overwrites files with the same name (obviously).

What would be a good work around so that I can rename the files something unique while copying them?

find <start directory>  -iname "*.html" -exec cp {} <target Directory> \;

Thanks!

0
Comment
Question by:b1xx1b
  • 2
4 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 33533962
try something like this

n=0
find <start directory>  -iname "*.html" | while read file
do
    tfile=`basename $file`
    cp $file  dir/$tfile.$n
    n=`expr $n + 1`
done
0
 

Author Comment

by:b1xx1b
ID: 33534081
I am an noob on linux, how would i execute that script?

0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 33534123
put in file using editor like vi or as below

echo 'n=0
find <start directory>  -iname "*.html" | while read file
do
    tfile=`basename $file`
    cp $file  dir/$tfile.$n
    n=`expr $n + 1`
done' > myscript

chmod +x myscipt

- run the script

./myscript
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 33534209
The following shell script should do the trick.  supposed the script is named 'my_move'  it would be invoked as follows "my_move <directory to look in> <directory to copy to>.   this particular script adds the date to the end of the file name so home.html would be renamed to home.html.082610 and then copied to the directory specified in $2, the second command line arguement.  Hope this helps.


Mike

m_date=`date +%m%d%y`
for i in `find $1 -print`
do
        rename .html .html.$m_date $i
        cp $i.$m_date $2
done
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now