UNIX: How to change all hidden files to visible in a multiple sub directories

7,887

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.

Share:
7,887

Related videos on Youtube

Ortoch
Author by

Ortoch

Updated on September 18, 2022

Comments

  • Ortoch
    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
      George Udosen over 6 years
      Try 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
      Ortoch over 6 years
      I 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
      George Udosen over 6 years
      What does from one directory up mean?
    • Kusalananda
      Kusalananda over 6 years
      If it's just a matter of visibility: alias ls='ls -a' and shopt -s dotglob (in bash).
    • Kusalananda
      Kusalananda over 6 years
      If 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
    Ortoch over 6 years
    Can'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
    Ortoch over 6 years
    same 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
    Ortoch over 6 years
    same error as all the other answers which means you are all probably right but I'm doing something wrong
  • Ortoch
    Ortoch over 6 years
    with -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
    George Udosen over 6 years
    remove the -n when your ready to rename
  • Ortoch
    Ortoch over 6 years
    yes 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
    Ortoch over 6 years
    I am in TEST running this command on TEST/T1/.test1 TEST/T1/.test2 TEST/T1/.test3 etc....
  • George Udosen
    George Udosen over 6 years
    Yes i get same error here let me fix that
  • Ortoch
    Ortoch over 6 years
    that would be great thanks I'll wait your reply
  • Satō Katsura
    Satō Katsura over 6 years
    That's because rename gets to see files as ./path/foo.
  • Kusalananda
    Kusalananda over 6 years
    This applies rename to files and directories alike, regardless of whether they actually need to be renamed or not.
  • ADDB
    ADDB over 6 years
    I don't understand why that code sample shouldn't work, but just for reference: use $() instead of back ticks.