简体   繁体   English

将目录名称附加到文件末尾并将其移动

[英]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.

相关问题 根据目录名称移动文件(Linux / Debian /#!) - Move files based on directory name (Linux / Debian / #!) 将具有唯一名称的多个文件移动到新文件夹并追加到文件名 - Move multiple files with unique name to new folder and append to file name 在目录中搜索文本文件,并在每个文本文件后附加(静态)行 - Search for text files in a directory and append a (static) line to each of them 如何将主目录列表附加到文本文件以及如何移动文件? - How to append list of home directory to a text file and how to move files? Shell脚本 - 查找今天修改的文件,创建目录并将其移动到那里 - Shell script - Find files modified today, create directory, and move them there Shell:如何将多个文件移动到一个目录并以不同的名称压缩该目录? - Shell: How to move multiple files to a directory and zip that directory with different name? 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 如何将所有文件按扩展名移动到指定DIR目录中的子目录? - How to move all files to subdirectories by extension name in the specified DIR directory? 如何根据 linux 中的名称将文件移动到新目录? - How do I move files to a new directory based on their name in linux? 创建 Unix shell 脚本以将非空文件从源目录移动到目标目录并为其添加时间戳 - Create Unix shell script to move non empty files from Source directory to Target directory and add timestamp to them
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM