2

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/\.//;' .*
  • 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 – George Udosen Oct 1 '17 at 23:05
  • I tried this command and its looking at the files above the directory I'm in not below? copied and pasted directly into putty – Ortoch Oct 1 '17 at 23:23
  • What does from one directory up mean? – George Udosen Oct 1 '17 at 23:25
  • If it's just a matter of visibility: alias ls='ls -a' and shopt -s dotglob (in bash). – Kusalananda Oct 2 '17 at 5:45
  • 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!" :-) – Kusalananda Oct 8 '17 at 15:40
4

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).

2

this is the line that fixed it all finally found the answer

find -mindepth 1 -depth -exec rename -n 's{/\.([^\/]*$)}{/$1}' {} +
  • This applies rename to files and directories alike, regardless of whether they actually need to be renamed or not. – Kusalananda Oct 2 '17 at 6:32
2

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.

0

This will do what you want:

find . -iname ".*" -exec realpath {} \; | rename -n 's/\.(.*)/$1/'

File structure:

.
├── game
│   ├── .keie
│   ├── .kjae
│   ├── .ndhe
│   └── shame
│       ├── .alwo
│       ├── .asdjd
│       └── .kajd
├── .jsdsd
├── .lewe
└── .skdsd

Test run:

rename(/home/george/Documents/askubuntu/rename/down/.lewe, /home/george/Documents/askubuntu/rename/down/lewe)
rename(/home/george/Documents/askubuntu/rename/down/.jsdsd, /home/george/Documents/askubuntu/rename/down/jsdsd)
rename(/home/george/Documents/askubuntu/rename/down/game/shame/.asdjd, /home/george/Documents/askubuntu/rename/down/game/shame/asdjd)
rename(/home/george/Documents/askubuntu/rename/down/game/shame/.kajd, /home/george/Documents/askubuntu/rename/down/game/shame/kajd)
rename(/home/george/Documents/askubuntu/rename/down/game/shame/.alwo, /home/george/Documents/askubuntu/rename/down/game/shame/alwo)
rename(/home/george/Documents/askubuntu/rename/down/game/.ndhe, /home/george/Documents/askubuntu/rename/down/game/ndhe)
rename(/home/george/Documents/askubuntu/rename/down/game/.keie, /home/george/Documents/askubuntu/rename/down/game/keie)
rename(/home/george/Documents/askubuntu/rename/down/game/.kjae, /home/george/Documents/askubuntu/rename/down/game/kjae)
rename(/home/george/Documents/askubuntu/rename/down/.skdsd, /home/george/Documents/askubuntu/rename/down/skdsd)

Results:

.
├── game
│   ├── keie
│   ├── kjae
│   ├── ndhe
│   └── shame
│       ├── alwo
│       ├── asdjd
│       └── kajd
├── jsdsd
├── lewe
└── skdsd

Information:

realpath {}: get the real path to file of interest

-n: used to test run, remove when you're ready to rename the files.

  • same error as all the other answers which means you are all probably right but I'm doing something wrong – Ortoch Oct 1 '17 at 23:33
  • 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) – Ortoch Oct 1 '17 at 23:34
  • remove the -n when your ready to rename – George Udosen Oct 1 '17 at 23:34
  • 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 Oct 1 '17 at 23:36
  • I am in TEST running this command on TEST/T1/.test1 TEST/T1/.test2 TEST/T1/.test3 etc.... – Ortoch Oct 1 '17 at 23:37
-1
find . -type f -name .\* -exec rename -n 's/\.//;' {} +
  • 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 Oct 1 '17 at 23:22
  • 1
    That's because rename gets to see files as ./path/foo. – Satō Katsura Oct 2 '17 at 3:25
-1

rename doesn't have a recursive option. If you want to rename the files in the current directory and all its subdirectories, put the following script in the parent directory and execute it.

#!/bin/bash
for i in `find . -name ".*"`
  do
    rename 's/\.//' $i
  done
  • 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 Oct 1 '17 at 23:21
  • I don't understand why that code sample shouldn't work, but just for reference: use $() instead of back ticks. – ADDB Oct 2 '17 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.