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

Posted on 2010-08-26
Last Modified: 2013-12-16

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> \;


Question by:b1xx1b
  • 2
LVL 40

Expert Comment

ID: 33533962
try something like this

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

Author Comment

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

LVL 40

Accepted Solution

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
    tfile=`basename $file`
    cp $file  dir/$tfile.$n
    n=`expr $n + 1`
done' > myscript

chmod +x myscipt

- run the script


Expert Comment

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.


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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 113
expand ext4 on centos 6 5 57
How to use IFS to get output of range in comma separator in bash? 2 48
Shrink Linux Swap File Size CentOS 10 33
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

696 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