user2908134
user2908134

Reputation:

How to find,copy and rename files in linux?

I am trying to find all files in a directory and sub-directories and then copy them to a different directory. However some of them have the same name, so I need to copy the files over and then if there are two files have the same name, rename one of those files.

So far I have managed to copy all found files with a unique name over using:

#!/bin/bash
if [ ! -e $2 ] ; then
    mkdir $2
    echo "Directory created"
fi
if [ ! -e $1 ] ; then
    echo "image source does not exists"
fi
find $1 -name IMG_****.JPG -exec cp {} $2 \;

However, I now need some sort of if statement to figure out if a file has the same name as another file that has been copied.

Upvotes: 1

Views: 5229

Answers (4)

Litmus
Litmus

Reputation: 10986

You can use rsync with the following switches for more control

 rsync --backup --backup-dir=DIR --suffix=SUFFIX -az <source dire> <destination dir>

Here (from man page)

-b, --backup

With this option, preexisting destination files are renamed as each file is transferred or deleted. You can control where the backup file goes and what (if any) suffix gets appended using the --backup-dir and --suffix options.

--backup-dir=DIR

In combination with the --backup option, this tells rsync to store all backups in the specified directory on the receiving side. This can be used for incremental backups. You can additionally specify a backup suffix using the --suffix option (otherwise the files backed up in the specified directory will keep their original filenames).

--suffix=SUFFIX

This option allows you to override the default backup suffix used with the --backup (-b) option. The default suffix is a ~ if no --backup-dir was specified, otherwise it is an empty string.

You can use rsycn to either sync two folders on local file system or on a remote file system. You can even do syncing over ssh connection.

rsync is amazingly powerful. See the man page for all the options.

Upvotes: 0

damienfrancois
damienfrancois

Reputation: 59070

You can do:

for file in $1/**/IMG_*.jpg ; do  
  target=$2/$(basename "$file") 
  SUFF=0 
  while [[ -f "$target$SUFF" ]] ; do 
    (( SUFF++ )) 
  done
  cp "$file" "$target$SUFF"
done

in your script in place of the find command to append integer suffixes to identically-named files

Upvotes: 0

Claudio
Claudio

Reputation: 10947

Try this approach: put the list of files in a variable and copy each file looking if the copy operation succeeds. If not, try a different name.

In code:

FILES=`find $1 -name IMG_****.JPG | xargs -r`
for FILE in $FILES; do
    cp -n $FILE destination
    # Check return error of latest command (i.e. cp)
    # through the $? variable and, in case
    # choose a different name for the destination
done

Inside the for statement, you can also put some incremental integer to try different names incrementally (e.g., name_1, name_2 and so on, until the cp command succeeds).

Upvotes: 1

William Pursell
William Pursell

Reputation: 212208

Since you are on linux, you are probably using cp from coreutils. If that is the case, let it do the backup for you by using cp --backup=t

Upvotes: 2

Related Questions