简体   繁体   English

将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中

[英]move all the folders and files to the folder with the same subject name in the directory Linux

I have for example 001, 001_ses-1, 001_ses-2 folders and files 001_lg1.lsf, 001_recon1.lsf, 001_trac1.lsf in the same directory.例如,我在同一目录中有 001、001_ses-1、001_ses-2 文件夹和文件 001_lg1.lsf、001_recon1.lsf、001_trac1.lsf。 I have hundreds of subjects.我有数百个科目。 I want to move folders: 001_ses-1, 001_ses-2, and files: 001_lg1.lsf , recon1_001.lsf and trac1_001.lsf to 001 main folder.我想将文件夹:001_ses-1、001_ses-2 和文件:001_lg1.lsf、recon1_001.lsf 和 trac1_001.lsf 移动到 001 主文件夹。 How do I do this?我该怎么做呢?

list={001, 011, 023, 059, ..... 102}
for i in list; do rm i* i; done 

so far, I have no clues.到目前为止,我没有任何线索。 Need some help figuring it out!需要一些帮助来解决它!

Given the information provided in the comments, there are two possibilities for identifying the destination folders:根据评论中提供的信息,识别目标文件夹有两种可能性:

A) Create a manual list. A) 创建一个手动列表。 This is how it is done in Bash and compatible Linux shells:这是在 Bash 和兼容的 Linux shell 中完成的方式:

DEST_LIST=( "001" "002" "003" "004" ) 

for DEST in ${DEST_LIST[@]}; do
    mv "${DEST}_"* "${DEST}"

B) Create a regular expression of the pattern and use find . B) 创建模式的正则表达式并使用find
I will use the pattern from your comment as an example:我将使用您评论中的模式作为示例:

PREFIX_PATTERN="[0-9]\{3\}_S_[0-9]\{3\}\.[0-9]"

for DEST in $(find . -type d -regex ".*/${PREFIX_PATTERN}"); do
    PREFIX=`expr "${DEST}" : ".*\(${PREFIX_PATTERN}\)"`
    mv "${PREFIX}_"* "${DEST}"

The extra line containing the expr command extracts the clean folder name from the full folder path returned by find command.包含expr命令的额外行从find命令返回的完整文件夹路径中提取干净的文件夹名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有文件和文件夹移动到同一文件夹的子文件夹中 - How to move all files and folders into a subfolder of the same folder Linux:查找所有具有特定名称的文件夹,将其删除,然后将一个文件夹复制到这些文件夹的父目录中 - Linux: Find all folders with a particular name, remove them and have a folder copied into the parent directory of those folders 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder Linux命令在目录中查找具有相同名称但扩展名不同的所有文件? - Linux command to find all the files with same name but different extension in a directory? 根据目录名称移动文件(Linux / Debian /#!) - Move files based on directory name (Linux / Debian / #!) 在同一目录中移动多个文件,而无需每次都输入目录名(Linux) - Move multiple files in same directory without typing directory name each time (Linux) Linux - 授予所有用户访问文件夹(以及下面的所有文件夹和文件)的权限 - Linux - Giving all users access to a folder (and all folders and files below) Linux:如何移动具有相同名称的文件diff ext。 放入自己的文件夹? - Linux: How to move files with same name, diff ext. into their own folder? 从Linux中包含文件夹名称的多个文件夹复制文件 - Copy files from multiple folders with including folder name in Linux 将同名文件夹移动到新目录Linux,Ubuntu - Moving folders with the same name to new directory Linux, Ubuntu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM