[英]Append directory name to the end of the files and move them
I am finding some files in a directory using this command: 我正在使用此命令在目录中找到一些文件:
find /Users/myname -type f
output is: 输出为:
/Users/myname/test01/logs1/err.log
/Users/myname/test01/logs1/std
/Users/myname/test01/logs2/std
/Users/myname/test02/logs2/velocity.log
/Users/myname/test03/logs3/err.log
/Users/myname/test03/logs3/invalid-arg
I need to move this files to a different directory by appending the test directory name to the end of the files. 我需要通过将测试目录名称附加到文件末尾来将此文件移动到其他目录。 Like below: 如下所示:
err.log-test01
std-test01
std-test01
velocity.log-test02
err.log-test03
invalid-arg-test03
I am trying with the cut command but not getting the desired output. 我正在尝试使用cut命令,但未获得所需的输出。
find /Users/myname -type f | cut -d'/' -f6,4
plus, I also need to move the files to a different directory. 另外,我还需要将文件移动到其他目录。 I guess a suitable way could be there using sed command, but I am not proficient with sed. 我想可以使用sed命令找到合适的方法,但是我对sed不熟练。 How this can be achieved in an efficient way? 如何有效地做到这一点?
You can let find
create the mv
command, use sed to modify it and then have it run by the shell: 您可以find
create mv
命令,使用sed对其进行修改,然后由外壳运行它:
find /Users/myname -type f -printf "mv %p /other/dir/%f\n" |
sed 's,/\(test[0-9]*\)/\(.*\),/\1/\2-\1,' | sh
This assumes there are no spaces in any argument, otherwise liberally add '
or "
. Also run it without the final | sh
to see what it actually wants to do. If you need to anchor the test[0-9]*
pattern better you can include part of the left or right string to match: 假定任何参数中都没有空格,否则自由地添加'
或"
。也可以在不带最终| sh
情况下运行它,以查看其实际功能。如果您需要更好地固定test[0-9]*
模式,则可以可以包含左或右字符串的一部分以进行匹配:
's,myname/\\(test[0-9]*\\)/\\(.*\\),myname/\\1/\\2-\\1,'
You can move it from the dst to the dst_dir
appending the directory, using awk
, and the target name would be awk -F/ '{print $5 "-" $4}'
. 您可以使用awk
将其从dst dst_dir
附加目录的dst_dir
,目标名称为awk -F/ '{print $5 "-" $4}'
。 The full command could be as simple as: 完整的命令可能很简单:
for i in `find . -type f`
do mv $i /dst_dir/`echo $i| awk -F/ '{print $5 "-" $4}' `
done
There are a number of things going on that you may want to use a helper script with find
to insure you can validate the existence of the directory to move the files to, etc.. A script might take the form of: 您可能需要使用find
的辅助脚本来确保您可以验证目录的存在以将文件移至其中,等等。脚本可能采取以下形式:
#!/bin/bash
[ -z $1 -o -z $2 ] && { # validate at least 2 arguments
printf "error: insufficient input\n"
exit 1
}
ffn="$1" # full file name provided by find
newdir="$2" # the target directory
# validate existence of 'newdir' or create/exit on failure
[ -d "$newdir" ] || mkdir -p "$newdir"
[ -d "$newdir" ] || { printf "error: uname to create '$newdir'\n"; exit 1; }
tmp="${ffn##*test}" # get the test## number
num="${tmp%%/*}"
fn="${ffn##*/}" # remove existing path from ffn
mv "$ffn" "${newdir}/${fn}-test${num}" # move to new location
exit 0
Save it in a location where it is accessible under a name like myscript
and make it executable (eg chmod 0755 myscript
) You may also choose to put it in a directory within your path. 将其保存在可通过myscript
类的名称访问的位置,并使其可执行(例如chmod 0755 myscript
)。您也可以选择将其放置在路径中的目录中。 You can then call the script for every file returned by find
with: 然后,您可以使用以下命令为find
返回的每个文件调用脚本:
find /Users/myname -type f -exec ./path/to/myscript '{}' somedir \;
Where somedir
is the target directory for the renamed file. 其中somedir
是重命名文件的目标目录。 Helper scripts generally provide the ability to do required validation that would otherwise not be done in one-liners. 辅助脚本通常提供执行所需验证的能力,否则将无法在单行代码中完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.