简体   繁体   English

创建与父文件夹同名的子文件夹并将文件移动到其中

[英]Create subfolder with same name as parent and move files into it

I have folders as below. 我有以下文件夹。 I want to create subdirectory with same name and move only few of the files into sub directory 我想创建一个具有相同名称的子目录,并将仅几个文件移到子目录中

Input 输入

 Parent

   folder1/a.txt
   folder1/b.txt
   folder2/a.txt
   folder2/b.txt
   folder3/a.txt
   folder3/b.txt 


Output

  Parent

  folder1/folder1/a.txt
  folder1/b.txt
  folder2/folder2/a.txt
  folder2/b.txt
  folder3/folder3/a.txt
  folder3/b.txt 

I tried this , but this is working only for files not folders 我试过了,但这仅适用于文件而不是文件夹

       for file in *; do dir=$(echo $file | cut -d. -f1); mkdir -p $dir; mv $file $dir; done

If your shell is bash, you can run the following: 如果您的外壳是bash,则可以运行以下命令:

for file in */a.txt ; do 
    dir=${file%/a.txt}
    mkdir "$dir/$dir"
    mv "$file" "$dir/$dir"
done

It uses the parameter expansion to remove the /a.txt from the file name which only leaves the directory name in $dir . 它使用参数扩展从文件名中删除/a.txt ,仅将目录名保留在$dir

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

相关问题 如何将所有文件和文件夹移动到同一文件夹的子文件夹中 - How to move all files and folders into a subfolder of the same folder 移动与其包含的文件名称相同的文件 - move files that have the same name of the file they contain 如果父文件夹与bash同名,则移动子文件夹的内容 - Move contents of a child folder if parent has the same name with bash 在查找和移动Bash脚本中包含相同名称的不同文件 - Including different files with the same name in a Find and Move Bash Script 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 复制/移动不同文件夹中同名的多个文件 - copy/move same name multiple files in different folder Unix 在一个目录下创建多个同名文件 - Unix create multiple files with same name in a directory 使用wget镜像具有相同名称的路径和子文件夹的网站 - using wget to mirror a website with path and subfolder that have the same name 在同一目录中移动多个文件,而无需每次都输入目录名(Linux) - Move multiple files in same directory without typing directory name each time (Linux) Linux:如何移动具有相同名称的文件diff ext。 放入自己的文件夹? - Linux: How to move files with same name, diff ext. into their own folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM