[英]How to change all hidden folders/files to visible in a multiple sub directories
I have hundreds of sub directories in a directory that all have hidden files in them that I need to remove the period at the beginning of them to make them visible. 我在目录中有数百个子目录,所有子目录中都有隐藏文件,我需要删除它们开头的句点以使其可见。 I found a command to go into each directory and change them to make them visible but I need to know how to make this command work from one directory up.
我找到了一个进入每个目录并更改它们以使其可见的命令,但是我需要知道如何使该命令从一个目录开始工作。
rename 's/\.//;' .*
I have tried about an hour to modify this to work one level up but don't understand the Perl string enough to do it. 我已经尝试了大约一个小时来修改它以使其向上升级,但是对Perl字符串的理解却不够。 If someone could help out I am sure it's simple and I just can't land on the right answer.
如果有人可以提供帮助,我相信这很简单,而且我无法找到正确的答案。
This requires a find that supports the +
(can use \\;
instead, which will call rename
multiple times), but even POSIX find specifies it: 这需要一个支持
+
的查找(可以使用\\;
相反,它将多次调用rename
),但是即使POSIX查找也可以指定它:
find -mindepth 1 -depth -exec rename -n 's{/\.([^\/]*$)}{/$1}' {} +
-depth
option prevents directories from being renamed before all the files in them are renamed -depth
选项可防止在重命名目录中的所有文件之前重命名目录 -mindepth 1
prevents find from trying to rename the current directory, .
-mindepth 1
阻止find尝试重命名当前目录.
. -n
is to just print what would be renamed instead of actually renaming (has to be removed to do the renaming). -n
仅打印要重命名的内容,而不是实际重命名(必须删除以进行重命名)。 rename
doesn't overwrite existing files, unless the -f
("force") option is used. rename
不会覆盖现有文件,除非使用-f
(“强制”)选项。
For a test directory structure like this: 对于这样的测试目录结构:
.
├── .dir1
│ ├── .dir2
│ │ ├── .dir3
│ │ │ └── .file2
│ │ └── .file1
│ ├── file3
│ └── .file6
├── dir5
│ └── .file5
├── .file4
├── test1.bar
└── test1.foo
the output is 输出是
rename(./dir5/.file5, ./dir5/file5)
rename(./.file4, ./file4)
rename(./.dir1/.file6, ./.dir1/file6)
rename(./.dir1/.dir2/.file1, ./.dir1/.dir2/file1)
rename(./.dir1/.dir2/.dir3/.file2, ./.dir1/.dir2/.dir3/file2)
rename(./.dir1/.dir2/.dir3, ./.dir1/.dir2/dir3)
rename(./.dir1/.dir2, ./.dir1/dir2)
rename(./.dir1, ./dir1)
and the result after removing -n
is 并且删除
-n
后的结果是
.
├── dir1
│ ├── dir2
│ │ ├── dir3
│ │ │ └── file2
│ │ └── file1
│ ├── file3
│ └── file6
├── dir5
│ └── file5
├── file4
├── test1.bar
└── test1.foo
safely_unhide
: safely_unhide
:
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw( fileparse );
for (@ARGV) {
my $o = $_;
my ($fn, $dir_qfn) = fileparse($_);
$fn =~ s/^\.//
or next;
my $n = "$dir_qfn/$fn";
if (stat($n)) {
warn("Skipping \"$o\": \"$n\" already exists\n");
next;
}
elsif (!$!{ENOENT}) {
warn("Skipping \"$o\": Can't stat \"$n\": $!\n");
next;
}
rename($n, $o)
or warn("Skipping \"$o\": Can't rename to \"$n\": $!\n");
}
Usage: 用法:
find -type f -exec safely_unhide {} + # Supports all file names. Requires GNU find
find -type f | xargs safely_unhide # Doesn't support newlines in file names.
find -type f -print0 | xargs -0 safely_unhide # Supports all file names.
Drop -type f
and add -depth
if you want to rename hidden dirs too. 如果您也想重命名隐藏的目录,请
-depth
-type f
并添加-depth
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.