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