OverTheWire/bandit

Bandit Level 2 → Level 3

보름달빵 2025. 1. 10. 17:26

📌 문제

홈 디렉터리 아래에 spaces in this filename 이라는 파일안에 bandit3의 비밀번호가 들어있다고 한다. 

해당 파일을 읽어서 비밀번호를 찾으면 된다. 

이 문제를 통해서 파일 이름에 공백이 있는 경우 파일을 읽는 방법에 대해 알아보자 

 

 

📌 풀이 

 

cat 명령어는 동시에 여러개의 파일을 읽을 수 있고, 각각의 파일 이름을 공백으로 구분한다.

cat [파일이름1] [파일이름2] .....

 

따라서 spaces in this filename 이라는 파일의 이름을 그대로 입력하게 되면, cat은 하나의 파일이 아닌 4개의 파일을 각각 읽으라는 의미로 인식하게 된다. 

bandit2@bandit:~$ ls
spaces in this filename
bandit2@bandit:~$ cat ./spaces in this filename
cat: ./spaces: No such file or directory
cat: in: No such file or directory
cat: this: No such file or directory
cat: filename: No such file or directory

 

그렇게 명령을 입력해보니 역시 space, in , this, filename이라는 파일들은 해당 디렉터리안에 존재하지 않는다고 나온다. 

 

공백이 있는 파일을 읽는 방법은 " " 나 ' ' 안에 파일 이름을 적어주면 된다.

또는 \ 를 사용하여 공백 문자를 무시하도록 해도 된다. 

bandit2@bandit:~$ cat "spaces in this filename"

bandit2@bandit:~$ cat 'spaces in this filename'

bandit2@bandit:~$ cat spaces\ in\ this\ filename

bandit2@bandit:~$ cat ./*

 

 

 

📌 정리

공백이 있는 파일의 이름은 "" , '' 안에 파일이름을 입력하거나 \를 이용하여 특수문자가 무시되도록한다. 

 

 

 

참고자료

https://appuals.com/how-to-handle-passing-filenames-with-spaces-in-bash/