Renaming hidden files to their filename sans .. (double dot)

5,526

Solution 1

for i in ..*; do
    echo "mv \"$i\" \"`echo "$i" | sed 's/^\.\.//g'`\""
done

Once it works, change echo to eval.

Solution 2

rename .. "" ..* did the trick for me.

pinguinson at desktop in ~ 
$ mkdir test && cd test && touch ..aa ..bb ..cc

pinguinson at desktop in ~/test 
$ ls -a
.  ..  ..aa  ..bb  ..cc

pinguinson at desktop in ~/test 
$ rename .. "" ..*

pinguinson at desktop in ~/test 
$ ls -a
.  ..  aa  bb  cc
Share:
5,526

Related videos on Youtube

Justin
Author by

Justin

Updated on September 18, 2022

Comments

  • Justin
    Justin over 1 year

    I accidentally ran a script that added a ".." infront of a variety of files.

    ie. originalfile ==> ..originalfile

    Now I have a variety of files that are hidden. Being new to Linux commands, is there a script that can reverse the above? Searching for files with a .. and knowing to rename the file to the filename after the ..?

    ie. ..originalfile ==> originalfile

    I have run "find .. -type f print0" which finds all the files that I accidentally hid, but I don't know how to write a script that can parse the characters after the .. to make it renamed to those characters.

    Any help would be greatly appreciated!

  • Justin
    Justin almost 9 years
    Thanks theoden for the absolutely quick reply! When I replace echo to eval I get the following error "mv: cannot move ‘..’ to ‘’: No such file or directory". But when I use the original code with echo, the output looks exactly like what I want it do.
  • Justin
    Justin almost 9 years
    Sorry I think I am an idiot, it basically double checks to see if it worked or not. Sorry! New to linux, but enjoying it (when I don't screw up)!
  • theoden8
    theoden8 almost 9 years
    @Justin eval does the same as echo, but instead performs the task. With eval, you can make shell language object-oriented. If it doesn't make the proper result achieved, return it to echo and add | zsh at the end of the line. Don't worry, noone thinks you are idiot. It's very pleasant to see when people respect your effort.
  • Justin
    Justin almost 9 years
    I see! Thank you so much! You saved me hours of manually changing the filenames back by hand!
  • theoden8
    theoden8 almost 9 years
    @Justin In case I really did it, you could tick the answer as accepted. As I see you're new. It gives you 2 reputation, and gives 15 to me. Moreover, it doesn't make people distracted on the unanswered question and all further readers will see that the answer worked. If you expect a more appropriate answer, you shouldn't tick.
  • Justin
    Justin almost 9 years
    thank you! Did not know about that. Your answer has been ticked!
  • Justin
    Justin almost 9 years
    thanks! I was able to use theoden solution as he replied first. But out of curiosity, why do you use rename and not mv? Also, does this command go through subdirectories as well?
  • Nikita Gousak
    Nikita Gousak almost 9 years
    @Justin i find this way a bit less cumbersome. It doesn't go through subdirectories, though.
  • theoden8
    theoden8 almost 9 years
    @Justin if you're interested in doing subdirectories, use find . -type f -name "..*" | awk '{print "mv "$0" "$0}'
  • Justin
    Justin almost 9 years
    can I append that 'find' command to the above code that you provided (ie. before the mv statement)? Or will this only search for all files (including sub directories) that are hidden?
  • theoden8
    theoden8 almost 9 years
    @Justin, it will search all files in subdirectories of the name (not path) matching ..* and stream that to awk, which will print them like mv FILE FILE. By the way, I made a mistake... It will not actually rename them. This needs to be completed..
  • Justin
    Justin almost 9 years
    Gotcha! Thank you! I have a feeling the script I ran accessed sub directories as it was supposed to. All started as a renaming script which became a hiding-everything-script because I made the search term too broad!
  • Justin
    Justin almost 9 years
    sorry to keep bugging you, I have a 2 questions regarding the find command that you suggested for subdirectories. (1) When I run that command it comes up with only a handful of samples, but when I fun the following find .. -type f -print0 I get a whole lot more. Is there a reason for this? (2) How come your original answer does not search subdirectories? Is it because mv only works in the current directory while find searches everything? Thus, is the answer to the subdirectory problem to use find save the answers as $i and then use your mv / eval command to rename?
  • theoden8
    theoden8 almost 9 years
    @Justin, I will reorganize your questions, if you don't mind. Type in the following commands in any order: man mv, man find. Also check out this and this. You can use for i in ..*; do echo $i; done-like construction in your Shell loops to iterate through files of a mask.
  • Justin
    Justin over 8 years
    Thanks again! I think I am starting to understand the gist of how I can go about fixing the rest of the files. Thanks again for your help!