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
Related videos on Youtube
Author by
Justin
Updated on September 18, 2022Comments
-
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 almost 9 yearsThanks 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 almost 9 yearsSorry 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 almost 9 years@Justin
eval
does the same as echo, but instead performs the task. Witheval
, you can make shell language object-oriented. If it doesn't make the proper result achieved, return it toecho
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 almost 9 yearsI see! Thank you so much! You saved me hours of manually changing the filenames back by hand!
-
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 almost 9 yearsthank you! Did not know about that. Your answer has been ticked!
-
Justin almost 9 yearsthanks! 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 almost 9 years@Justin i find this way a bit less cumbersome. It doesn't go through subdirectories, though.
-
theoden8 almost 9 years@Justin if you're interested in doing subdirectories, use
find . -type f -name "..*" | awk '{print "mv "$0" "$0}'
-
Justin almost 9 yearscan 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 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 likemv FILE FILE
. By the way, I made a mistake... It will not actually rename them. This needs to be completed.. -
Justin almost 9 yearsGotcha! 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 almost 9 yearssorry 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 followingfind .. -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 becausemv
only works in the current directory whilefind
searches everything? Thus, is the answer to the subdirectory problem to usefind
save the answers as $i and then use yourmv
/eval
command to rename? -
theoden8 almost 9 years
-
Justin over 8 yearsThanks 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!