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