Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files recursively, rename them but keep the same extension in Bash?

Tags:

find

bash

cp

I have a folder with tens of thousands of different file types. Id like to copy them all to a new folder (Copy1) but also rename them all to $RANDOM but keep the extension intact. I realize I can write a line specifying which extension to find and how to name it, but there is got to be a way to do it dynamically, because there are at least 100 file types and may be more in the future.

I have the following so far:

find ./ -name '*.*' -type f -exec bash -c 'cp "$1" "${1/\/123_//_$RANDOM}"' -- {} \;

but that puts the random number after the extension, and also it puts the all in the same folder. I cant figure out how to do the following 2 things:

1 - Keep all paths intact, but in a new root folder (Copy1)

2 - How to have name be $RANDOM.extension, instead of .extension.$RANDOM

PS - by $RANDOM i mean actual randomly generated number. I am interested in keeping folder structure, so we are dealing with a few hundred files at most per directory, but all directories/files need to be renamed to $RANDOM. Another way to look at what I need to do. Copy all contents or Folder1 with all subdirectories and files to Folder2 (where Fodler2 is a $RANDOM name), then rename all folders and files to random names but keep all extensions.

EDIT: Ok i figured out how to rename and keep extension. But I have a problem where its dumping all of the files into the root directory where script is run from. How do I keep them in their respective folders? Command Im using is:

find ./ -name '*.*' -type f -exec bash -c 'mv "$1" $RANDOM.${1##*.}' -- {} \;

Thanks!

like image 833
Duxa Avatar asked Dec 17 '14 01:12

Duxa


People also ask

How do I copy and rename a file in Linux at the same time?

An obvious way to do this is to use a command like “cp file1 file1-orig.” The command is named cp from the short name of copy, which means copy. Linux system users can copy folders, directories, and files using the cp command.

How do I copy multiple files with the same name in Linux?

Copy Multiple Files Using CP CommandYou must provide both the file name and the destination directory when using the cp command to copy multiple files. First, open the specific directory in the terminal and execute the tree command. If you don't know about the tree command, then please check out this blog.


2 Answers

Ok i figured out how to rename and keep extension. But I have a problem where its dumping all of the files into the root directory where script is run from. How do I keep them in their respective folders? Command Im using is:

find ./ -name '*.*' -type f -exec bash -c 'mv "$1" $RANDOM.${1##*.}' -- {} \;

Change your command to:

PATH=/bin:/usr/bin find . -name '*.*' -type f -execdir bash -c 'mv "$1" $RANDOM.${1##*.}' -- {} \;

Or alternatively using uuids instead of random numbers:

PATH=/bin:/usr/bin find . -name '*.*' -type f -execdir bash -c 'mv "$1" $(uuidgen).${1##*.}' -- {} \;
like image 61
Armali Avatar answered Sep 28 '22 05:09

Armali


Here's what I came up with :

i=1
random="whatever"
find . -name "*.*" -type f | while read f
do
    newbase=${f/*./$random$i.} //added counter to filename
    cp $f /Path/Name/"$newbase"
    ((i++))
done

I had to add a counter to random (i), otherwise, if the extensions are similar, your files would overwrite themselves when copied.

In your new folder, your files should look like this :

whatever1.txt
whatever2.txt
etc etc

I hope this is what you were looking for.

like image 27
buydadip Avatar answered Sep 28 '22 06:09

buydadip