Sunday, May 5, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 475  / 1 Year ago, wed, february 8, 2023, 5:42:21

I was setting up a cloud server and saw that I accidentally added a folder named ^[[4~ (with escape code). I want to see what is inside and if that was just a mistake then remove that directory. However I cannot cd nor ls the contents. I tried finding out the escape code so that I could enter a command like cd x1b[30m; and hope that it would get me in the directory.



What if I was to cd or remove by searching for the first directory listed with ls. Would that be possible? Because when I do a ls |grep 4 two results come up and one of them is the one I'm trying to access.



Either finding out the folder name in escape string or changing the directory to the first directory ls finds might be a solution here. How can I approach this problem?


More From » command-line

 Answers
0

A funny way to deal with that, using Bash: cd into the directory (that's easy).



Then we'll check that a control character is in the file's name: if you issue



printf 'strange: %s
' *[[:cntrl:]]*


you should see your file there. If not, try to use another character class or modify the glob so that it matches your file name. (But very likely, you'll only have the bizarre one.) Then, three possiblities:




  • The previous command only issued your bizarre file. Then, easy, just rename it:



    mv -nv *[[:cntrl:]]* a_less_bizarre_name


    done!


  • The previous command issued several filenames with bizarre names. Then, rename them all as:



    i=0
    for biz in *[[:cntrl:]]*; do
    mv -nv "$biz" a_less_bizarre_name$((++i))
    done


    done!


  • The previous command output several names, but you only want to change one. In this case, we'll put them all in an array, print them all out with a number in front, and then we'll select only the one we want:



    bizarre=( *[[:cntrl:]]* )
    for index in "${!bizarre[@]}"; do
    printf "%s --> %s
    " "$index" "${bizarre[index]}"
    done
    # Then, look at the index of the one you want to rename
    # If its index is 42, issue:
    mv -nv "${bizarre[42]}" a_less_bizarre_name


    done!




Note. If you use these commands, make sure you use them with the proper quotings (i.e., the quotings I gave). Do not alter the quotings!



Remark. You will very likely want first to issue the mv I gave with an echo in front, just to see what it'll do, e.g.,



echo mv -nv *[[:cntrl:]]* a_less_bizarre_name


and remove the echo when you're happy with it! Anyways, I'm using the -n switch so as to not overwrite an otherwise existing file. All these commands are rather safe if you use them as I typed them.



Good luck!


[#29141] Thursday, February 9, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bleger

Total Points: 468
Total Questions: 108
Total Answers: 100

Location: Belarus
Member since Wed, Dec 7, 2022
1 Year ago
;