简体   繁体   English

如何在linux中查找,复制和重命名文件?

[英]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. 但是,我现在需要某种if语句来确定文件是否与另一个已复制的文件具有相同的名称。

Since you are on linux, you are probably using cp from coreutils. 由于您使用的是linux,因此您可能正在使用coreutils中的cp If that is the case, let it do the backup for you by using cp --backup=t 如果是这种情况,请使用cp --backup=t为您进行备份

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 (eg, name_1, name_2 and so on, until the cp command succeeds). 在for语句中,您还可以使用一些增量整数来逐步尝试不同的名称(例如,name_1,name_2等等,直到cp命令成功)。

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 在您的脚本中代替find命令将整数后缀附加到具有相同名称的文件

You can use rsync with the following switches for more control 您可以将rsync与以下开关一起使用以获得更多控制

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

Here (from man page ) 这里(来自手册页

-b, --backup -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和--suffix选项控制备份文件的位置以及附加的后缀(如果有)。

--backup-dir=DIR --backup-DIR = DIR

In combination with the --backup option, this tells rsync to store all backups in the specified directory on the receiving side. 结合--backup选项,这告诉rsync将所有备份存储在接收端的指定目录中。 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=SUFFIX --suffix = SUFFIX

This option allows you to override the default backup suffix used with the --backup (-b) option. 此选项允许您覆盖与--backup(-b)选项一起使用的默认备份后缀。 The default suffix is a ~ if no --backup-dir was specified, otherwise it is an empty string. 如果没有指定--backup-dir,则默认后缀为〜,否则为空字符串。

You can use rsycn to either sync two folders on local file system or on a remote file system. 您可以使用rsycn同步本地文件系统或远程文件系统上的两个文件夹。 You can even do syncing over ssh connection. 您甚至可以通过ssh连接进行同步。

rsync is amazingly powerful. rsync非常强大。 See the man page for all the options. 有关所有选项,请参见手册页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM