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/\.//;' .*
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}' {} +
-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).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