[英]need help utilizing find command and xargs command
I'm trying to write a simple scripts that can mv every file within a folder to a folder generated from the current date. 我正在尝试编写一个简单的脚本,可以将文件夹中的每个文件转换为从当前日期生成的文件夹。 This is my initiatives. 这是我的倡议。
#!/bin/bash
storage_folder=`date +%F` # date is generated to name the folder
mkdir "$storage_folder" #createing a folder to store data
find "$PWD" | xargs -E mv "$storage_folder" # mv everyfile to the folder
xargs is not needed. 不需要xargs。 Try: 尝试:
find . -exec mv -t "$storage_folder" {} +
Notes: 笔记:
Find's -exec
feature eliminates most needs for xargs
. Find的-exec
功能消除了对xargs
大多数需求。
Because .
因为.
refers to the current working directoy, find "$PWD"
is the same as the simpler find .
指当前工作的目录, find "$PWD"
与简单find .
相同find .
. 。
The -t target
option to mv
tells mv
to move all files to the target
directory. mv
的-t target
选项告诉mv
将所有文件移动到target
目录。 This is handy here because it allows us to fit the mv
command into the natural format for a find -exec
command. 这在这里很方便,因为它使我们可以将mv
命令适合于find -exec
命令的自然格式。
If you do not have GNU tools, then your mv
may not have the -t
option. 如果您没有GNU工具,则您的mv
可能没有-t
选项。 In that case: 在这种情况下:
find . -exec sh -c 'mv -- "$1" "$storage_folder"' Move {} \;
The above creates one shell process for each move. 上面为每一步创建一个shell过程。 A more efficient approach, as suggested by Charles Duffy in the comments, passes in the target directory using $0
: 如Charles Duffy在评论中所建议的,一种更有效的方法是使用$0
传递到目标目录:
find . -exec sh -c 'mv -- "$@" "$0"' "$storage_folder" {} +
As Gordon Davisson points out in the comments, for safety, you may want to use the -i
or -n
options to mv
so that files at the destination are not overwritten without your explicit approval. 正如戈登·戴维森(Gordon Davisson)在评论中指出的那样,为了安全起见,您可能希望使用-i
或-n
选项来mv
这样,未经您的明确许可就不会覆盖目标文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.