UNIX: How to change all hidden files to visible in a multiple sub directories
Solution 1
With GNU find
:
find /some/path -type f -name '.*' -execdir sh -c 'mv -i "$0" "./${0#./.}"' {} \;
With Perl rename
:
find /some/path -type f -name '.*' -exec prename -i -n 's!.*/\K\.!!' {} +
(remove -n
when you're happy with the results).
Solution 2
this is the line that fixed it all finally found the answer
find -mindepth 1 -depth -exec rename -n 's{/\.([^\/]*$)}{/$1}' {} +
Solution 3
Just use find
together with a simple shell script for doing the renaming and checking that no existing file is overwritten:
find . -type f -name '.*' \
-execdir sh -c '[ ! -e "${1#.}" ] && mv "$1" "${1#.}"' sh {} ';'
The -execdir
option will execute its argument inside the parent directory of the found name, and {}
will be the base name (name without path) of the found name. This option is a widely implemented extension to standard find
.
The sh -c
script will simply make sure that the desired name is not already taken, and then it will rename the file.
The ${1#.}
parameter substitution will take the value of $1
(the first command line argument of the sh -c
script, which is a filename) and remove the initial dot.
Related videos on Youtube
Ortoch
Updated on September 18, 2022Comments
-
Ortoch almost 2 years
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/\.//;' .*
-
George Udosen over 6 yearsTry it like this
rename -n 's/\.//;' ../*
the-n
will see what happens without making any changes, then when your ok with it remove the-n
option -
Ortoch over 6 yearsI tried this command and its looking at the files above the directory I'm in not below? copied and pasted directly into putty
-
George Udosen over 6 yearsWhat does
from one directory up
mean? -
Kusalananda over 6 yearsIf it's just a matter of visibility:
alias ls='ls -a'
andshopt -s dotglob
(inbash
). -
Kusalananda over 6 yearsIf you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-)
-
-
Ortoch over 6 yearsCan't rename ./T2/.test3 /T2/.test3: No such file or directory got this error over and over in the test directory I'm playing with testing things out. Have 3 test directories with 3 test hidden files in each and all 9 files returned this string.
-
Ortoch over 6 yearssame error as the above solution Can't rename ./T2/.test3 /T2/.test3: No such file or directory almost looks like its trying to rename the Directory or something?
-
Ortoch over 6 yearssame error as all the other answers which means you are all probably right but I'm doing something wrong
-
Ortoch over 6 yearswith -n the command looks like it will run perfectly here is output...find . -type f -iname ".*" -exec basename {} \; | rename -n 's/\.//;' rename(.test3, test3) rename(.test1, test1) rename(.test2, test2) rename(.test3, test3) rename(.test1, test1)
-
George Udosen over 6 yearsremove the
-n
when your ready to rename -
Ortoch over 6 yearsyes without the -n I get this error for all 9 test files find . -type f -iname ".*" -exec basename {} \; | rename 's/\.//;' Can't rename .test3 test3: No such file or directory Can't rename .test1 test1: No such file or directory Can't rename .test2 test2: No such file or directory Can't rename .test3 test3: No such file or directory Can't rename .test1 test1: No such file or directory Can't rename .test2 test2: No such file or directory
-
Ortoch over 6 yearsI am in TEST running this command on TEST/T1/.test1 TEST/T1/.test2 TEST/T1/.test3 etc....
-
George Udosen over 6 yearsYes i get same error here let me fix that
-
Ortoch over 6 yearsthat would be great thanks I'll wait your reply
-
Satō Katsura over 6 yearsThat's because
rename
gets to see files as./path/foo
. -
Kusalananda over 6 yearsThis applies
rename
to files and directories alike, regardless of whether they actually need to be renamed or not. -
ADDB over 6 yearsI don't understand why that code sample shouldn't work, but just for reference: use
$()
instead ofback ticks
.