简体   繁体   English

Linux中当前目录下所有子目录添加后缀

[英]Add suffix to all subdirectories in current directory in Linux

In current directory: folders: 001, 002, 003, 004......etc.当前目录下:文件夹:001、002、003、004……等。 I want to add the suffix '_ses-1' to all folders and it makes for example 001_ses-1我想将后缀“_ses-1”添加到所有文件夹,例如 001_ses-1

Tried尝试过

for d in *; do mv "$d" "${d}_ses-1"; done


find ./ -type d -exec bash -c mv "$folder" "${folder}_ses-1"

All failed and would like some help to figure this out.一切都失败了,希望得到一些帮助来解决这个问题。

The first command ( for ) won't match only directories.第一个命令 ( for ) 不会只匹配目录。

The second ( find ) is problematic if you have nested directories.如果您有嵌套目录,则第二个 ( find ) 是有问题的。

If you don't have nested folders, you can use:如果您没有嵌套文件夹,您可以使用:

find . -maxdepth 1 -type d -exec mv {} {}_ext \;

Assuming this is a one-off task, and it doesn't need to recurse, one trick is to pipe the output of ls into a text file, run a macro (eg in vim) to transform them to be the correct mv command, then run the whole thing as a script.假设这是一次性任务,不需要递归,一个技巧是将ls的 pipe output 转换成文本文件,运行宏(例如在 vim 中)将它们转换为正确的mv命令,然后将整个事情作为脚本运行。

Another option is to use bash to generate each command (similar to what you have already tried) as a separate mv command and pipe into a file which you then run as a script.另一种选择是使用 bash 将每个命令(类似于您已经尝试过的命令)生成为单独的mv命令,并将 pipe 生成到一个文件中,然后您可以将其作为脚本运行。

I like the find./ approach, but it likely finds .我喜欢find./方法,但它可能会找到. and .. then fails on moving them.并且..然后无法移动它们。

find./ -type d. -name. . -name .. -exec bash -c mv "$folder" "${folder}_ses-1"

For the for d in *;对于for d in *; method, you'd want to do this to iterate over the list:方法,你会想要这样做来遍历列表:

for d in `ls -1`; do mv "$d" "${d}_ses-1"; done

Use rename command, which can be used like so: find... | xargs rename...使用rename命令,可以像这样使用: find... | xargs rename... find... | xargs rename... . find... | xargs rename... The rename command has several implementations, and is very powerful. rename命令有多种实现方式,非常强大。 It can be used for much more complex operations than a simple mv .它可用于比简单的mv复杂得多的操作。

Example:例子:


# Create a tiny test with 4 input directories:

$ mkdir 001 002 003 004

$ ls -d 00[1-4]*
001  002  003  004

# Do a dry run first using -n option:

$ find . -type d -name '00[1-4]' | xargs rename -n 's/$/_ses-1/'
'./001' would be renamed to './001_ses-1'
'./003' would be renamed to './003_ses-1'
'./004' would be renamed to './004_ses-1'
'./002' would be renamed to './002_ses-1'

# Actually rename for real:

$ find . -type d -name '00[1-4]' | xargs rename 's/$/_ses-1/'

# Confirm the results:

$ ls -d 00[1-4]*
001_ses-1  002_ses-1  003_ses-1  004_ses-1

Note that rename can be installed easily, for example with conda :请注意, rename可以很容易地安装,例如使用conda

conda install rename

SEE ALSO:也可以看看:

rename manual (very helpful): rename手册(非常有帮助):

rename --man

For example:例如:

-n, --dry-run, --just-print
    Show how the files would be renamed, but don't actually do anything.

暂无
暂无

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

相关问题 重命名Linux目录和子目录中的所有文件 - Rename All Files in directory and subdirectories Linux 删除linux中目录的所有子目录中的特定文件 - Delete particular files in all subdirectories of a directory in linux 将目录权限应用于Linux中的所有新子目录 - Apply directory permissions to all new subdirectories in linux 列出具有特定模式文件名的linux目录和子目录中的所有文件 - list all files in directory and subdirectories in linux with specific pattern filename 列出当前目录和所有子目录中特定大小的文件 - List files over a specific size in current directory and all subdirectories 如何在Linux中重命名当前目录中的所有文件 - How to rename all the files in current directory in linux 在gedit中打开当前目录下所有子目录下所有名为generator.yml的文件 - Open All files named generator.yml in all subdirectories of current directory in gedit 如何在Linux中递归重命名目录和子目录? - How to rename directory and subdirectories recursively in linux? bash脚本linux - 使用目录作为用户输入参数,并将所有子目录复制到/ tmp / folder,其名称与输入目录相同 - bash script linux - use directory as user input parameter and copy all the subdirectories to /tmp/ folder with the same name as the input directory 在Linux脚本中,如何删除当前目录中的所有文件和目录? - In a Linux script, how to remove all files & directories but one, in current directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM