简体   繁体   English

如何将所有隐藏的文件夹/文件更改为在多个子目录中可见

[英]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}' {} +
  • The -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仅打印要重命名的内容,而不是实际重命名(必须删除以进行重命名)。
  • The regular expression removes the last period after which there are no forward slashes, if it is preceded by a forward slash. 如果正则表达式前面带有正斜杠,则它会删除没有正斜杠的最后一个句点。

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.

相关问题 如何在不chmod文件夹的情况下chmod子目录中的所有文件? - How to chmod all files in sub-directories without chmod the folders? 如何将多个子文件夹中的多个目录中存在的文件复制两级? - How to copy files which are present in multiple directories within multiple sub folders two levels up? 如何解压缩子目录中的所有文件? - How to unzip all files in sub-directories? 如何gzip bash所有子目录下的所有文件 - How to gzip all files in all sub-directories in bash 如何重命名包含在多个子目录中的多个文件 - How to rename multiple files, contained in multiple sub-directories 如何从所有子目录中递归删除具有特定名称的所有文件夹? - How to remove all folders with a specific name recursively from all sub directories? 如何查找所有目录和子目录的文件总和(分别),并按大小对文件进行排序 - How to find sum of files for all directories and sub directories (separately) and them sort them by size 如何更改 linux 上包含特定字符串的所有目录/文件的名称 - how to change names of all directories / files containing a specific string on linux 如何将所有子目录中的所有文件gzip压缩为bash中的一个压缩文件 - How to gzip all files in all sub-directories into one compressed file in bash Bash:如何在所有子目录中的所有名为output.log的文件中搜索特定短语 - Bash: How to search for a specific phrase in all files named output.log across all sub directories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM