Ortoch Ortoch - 3 years ago 163
Perl Question

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. If someone could help out I am sure it's simple and I just can't land on the right answer.

Answer Source

This requires a find that supports the + (can use \; instead, which will call rename multiple times), but even POSIX find specifies it:

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
  • -mindepth 1 prevents find from trying to rename the current directory, ..
  • -n is to just print what would be renamed instead of actually renaming (has to be removed to do the renaming).
  • 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.

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

├── dir1
│   ├── dir2
│   │   ├── dir3
│   │   │   └── file2
│   │   └── file1
│   ├── file3
│   └── file6
├── dir5
│   └── file5
├── file4
├── test1.bar
└── test1.foo
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download