简体   繁体   English

将所有以相同前缀开头的文件放在linux的文件夹中

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

相关问题 如何重命名(通过添加前缀)从当前文件夹开始的所有文件和文件夹? - How to rename(by adding a prefix) all the files and folders starting from the current folder? 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 在单个命令中使用前缀重命名文件夹中的所有文件 - Rename all files in a folder with a prefix in a single command 为什么我的 Linux 服务器中的 bash 脚本无法将星号识别为具有相同前缀的所有文件? - How come my bash scripts in my Linux server doesn't recognize asterisk as all files with the same prefix? 如何使用SVN提交具有相同前缀的所有文件 - How to SVN commit all files with the same prefix 根据带有 Linux 的前缀号将文件从文件夹移动到子文件夹 - moving files from a folder into subfolders based on the prefix number with Linux 如何重命名 linux 目录中具有不同/随机扩展名的同一文件夹中的所有文件? - How to rename all files in the same folder with different/random extentions in a directory on linux? Linux:使用指定的数据覆盖文件夹中的所有文件吗? - Linux: Overwrite all files in folder with specified data? Linux昨天修改了所有文件夹的文件 - Linux all files of folder modified yesterday Linux-重命名文件夹中的所有文件 - Linux- rename all files in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM