[英]Put all the files starting with same prefix in a folder in linux
I have different files in some old data. 我在一些旧数据中有不同的文件。
They are in the same folder. 它们在同一文件夹中。
They are grouped by the prefix. 它们按前缀分组。
I want to move all the files starting with the same prefix in a folder named with the same prefix. 我想将所有以相同前缀开头的文件移动到以相同前缀命名的文件夹中。
Looking for a script which does it automatically. 寻找一个自动执行的脚本。
EDIT 1 The script has to look by itself that how many prefixes are there and then group 编辑1脚本必须自己查看有多少个前缀,然后分组
Thanks 谢谢
As a starting point, how about an ls -1
listing of all the files, piping to an awk
command to get all the unique prefixes: 首先,以
ls -1
列出所有文件,然后将其传递给awk
命令以获取所有唯一的前缀:
xxx
yyy
zzz
www
...
Then, use the output of the awk
commmand and loop through to create the prefix directory and move all files into it as @Noufal Ibrahim suggested: 然后,使用
awk
的输出并循环遍历以创建前缀目录,然后按照@Noufal Ibrahim的建议将所有文件移动到该目录中:
mkdir ${prefix} && mv ${prefix}* prefix
I'm no awk expert, but off the top of my head, I'm thinking the awk script would be something along the lines of: 我不是awk专家,但是我想不起的是,awk脚本可能类似于以下内容:
BEGIN {
// initialize associative array $x
}
{ $x[prefix($0)] = 1; }
END {
// foreach key in $x:
print $key
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.