Why Linux Says “No Such File or Directory” When It Exists
You run ls -la, confirm the file is sitting right in front of you with execute permissions, run ./deploy.sh, and Linux returns No such file or directory (or cannot execute: required file not found on Bash 5.2). Linux is not lying, and this error does not necessarily mean your filesystem is corrupted. When you ask Linux to open or execute a file, ENOENT can mean that the requested file, a component of its pathname, or a required program interpreter does not exist—the missing file is not necessarily the one you typed.
This hands-on companion lab walks through five real-world troubleshooting scenarios behind the “ghost file” paradox on Ubuntu 24.04 LTS. You will reproduce every failure safely as a regular user, inspect each root cause without guessing or running chmod 777, and apply clean, reproducible fixes. If you missed Video #1 in this series, review Why chmod 777 Still Gives “Permission Denied” in Linux to cover the permission side of file troubleshooting.
Lab Environment, Prerequisites & Workflow
Every command in this tutorial runs as a regular user (alex) inside ~/ghost-file-lab on a disposable Ubuntu VM. None of the five troubleshooting cases requires root privileges once the prerequisite packages are installed.
| Component | Tested Value |
|---|---|
| Operating System | Ubuntu 24.04 LTS |
| Architecture | x86_64 |
| Shell | Bash 5.2 (Bash 5.1 wording noted for comparison) |
| User Account | alex (regular user in both reproduction and diagnostic terminals) |
| Locale | C.UTF-8 |
Verify Your Environment & Install Prerequisites
Check your OS release, shell version, architecture, and locale first:
cat /etc/os-release
bash --version
uname -m
localeOn a minimal Ubuntu VM, install the inspection and build utilities needed for the lab:
sudo apt update
sudo apt install build-essential binutils file python3Disposable VM Notice: Run the lab scripts as a regular user on a disposable x86-64 Ubuntu VM. The lab expects /lib/ld-musl-x86_64.so.1 and /usr/bin/python to be absent. If either path already exists (including as a symlink), use a clean VM snapshot instead of removing system files or packages. The setup script refuses to overwrite an existing ~/ghost-file-lab directory.
Quick Lab Workflow
Save the three scripts from the Complete Bash Lab Scripts section below as LF-formatted files, then run setup and verification before starting Case 1:
bash setup-ghost-file-lab.sh
bash verify-ghost-file-lab.shThe verification script confirms all eight intentional failure conditions reproduce before you apply any fixes (a non-zero exit status is expected in individual failing demos, not from the verification script itself).
Case 1: Hidden Windows Line Endings (CRLF)
A common cause of this paradox is a shell script saved with Windows line endings (\r\n, CRLF) and executed on Linux. This happens when an editor saves a file with CRLF, Git checks it out with CRLF conversion enabled, or a file transfer preserves existing Windows line endings.
1. Reproduce the Failure
cd ~/ghost-file-lab/case1-crlf
ls -la
# -rwxr-xr-x 1 alex alex 51 ... deploy.sh
# -rwxr-xr-x 1 alex alex 58 ... deploy-direct.sh
cat deploy.sh
# #!/usr/bin/env bash
# echo "Deployment succeeded!"
./deploy.sh
# /usr/bin/env: 'bash\r': No such file or directoryGNU env Hint Line: Depending on your installed version of GNU coreutils, ./deploy.sh may print an additional line such as /usr/bin/env: use -[v]S to pass options in shebang lines, and the prefix may read env or /usr/bin/env.
Now run deploy-direct.sh, which uses a direct #!/bin/bash shebang instead of env:
./deploy-direct.sh
# Bash 5.2 (Ubuntu 24.04):
# bash: ./deploy-direct.sh: cannot execute: required file not found
#
# Bash 5.1 (version comparison):
# bash: ./deploy-direct.sh: /bin/bash^M: bad interpreter: No such file or directoryThe exact phrasing depends on your shell version. Bash 5.1 commonly reports a bad interpreter error, while Bash 5.2 may report cannot execute: required file not found. Both messages can point to a missing shebang interpreter.
2. Diagnose the Hidden Carriage Return
Inspect both scripts with file and cat -A:
file deploy.sh deploy-direct.sh
# deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
# deploy-direct.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
cat -A deploy.sh
# #!/usr/bin/env bash^M$
# echo "Deployment succeeded!"^M$
cat -A deploy-direct.sh
# #!/bin/bash^M$
# echo "Direct shebang deployment succeeded!"^M$cat -A makes hidden control characters, tabs, and line endings visible; the ^M before $ represents a carriage return (\r). Linux shebang parsing does not automatically strip the carriage return from Windows line endings, so that extra byte becomes part of the interpreter path or its argument:
| Shebang Style | What Happens at Execution Time | Result |
|---|---|---|
#!/usr/bin/env bash\r\n | Kernel starts /usr/bin/env successfully, then passes bash\r. env searches PATH for a program literally named bash\r. | env reports No such file or directory (exit 127). |
#!/bin/bash\r\n | Kernel attempts to open an interpreter path literally named /bin/bash\r. | Kernel returns ENOENT; Bash 5.2 prints cannot execute: required file not found. |
#!/usr/bin/env bash\n (LF) | Correct interpreter name with no trailing \r byte. | Interpreter starts, if present and executable. |
3. Fix and Prevent
Strip trailing carriage returns natively on Ubuntu with sed -i (alternatively, the dos2unix utility can convert Windows line endings to Unix line endings for these scripts):
sed -i 's/\r$//' deploy.sh deploy-direct.sh
file deploy.sh deploy-direct.sh
# deploy.sh: Bourne-Again shell script, ASCII text executable
# deploy-direct.sh: Bourne-Again shell script, ASCII text executable
./deploy.sh
# Deployment succeeded!
./deploy-direct.sh
# Direct shebang deployment succeeded!Prevent Regressions in Git & Editors: Set line endings to LF in your editor, and add explicit rules to your repository’s .gitattributes file to help prevent line-ending conversion problems for matching files managed by Git:
*.sh text eol=lf
*.py text eol=lfCase 2: Missing ELF Dynamic Linker (PT_INTERP)
The same error also hits compiled executables—for example, when you copy a binary built for Alpine Linux (musl) onto an Ubuntu system (glibc), or attempt to run a 32-bit x86 binary without 32-bit runtime libraries installed.
Lab Implementation Note: To keep this lab self-contained with zero external binary downloads, setup-ghost-file-lab.sh compiles app.c as a glibc-linked executable with a deliberately nonstandard ELF interpreter path (/lib/ld-musl-x86_64.so.1) at link time. It is a deliberately modified ELF executable rather than a genuine Alpine/musl build, reproducing the exact missing-loader failure.
1. Reproduce the Failure
cd ~/ghost-file-lab/case2-elf-loader
ls -la
# -rwxr-xr-x 1 alex alex ... app
# -rw------- 1 alex alex ... app.c
./app
# Bash 5.2 (Ubuntu 24.04):
# bash: ./app: cannot execute: required file not found
#
# Bash 5.1 (version comparison):
# bash: ./app: No such file or directory2. Inspect Statically (Never Run ldd on Untrusted Binaries)
Security Warning: For an unknown executable, always start with file and readelf. These tools inspect the binary statically without intentionally executing it. Never use ldd on an untrusted executable—under some implementations or conditions, ldd can execute code from the target binary.
Compare the host architecture against the executable, inspect its requested ELF program interpreter (PT_INTERP), and verify whether that loader path exists:
uname -m
# x86_64
file ./app
# ./app: ELF 64-bit LSB pie executable, x86-64, ..., dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, ...
LC_ALL=C readelf -l ./app | grep interpreter
# [Requesting program interpreter: /lib/ld-musl-x86_64.so.1]
ls -lL /lib/ld-musl-x86_64.so.1
# ls: cannot access '/lib/ld-musl-x86_64.so.1': No such file or directoryBoth the system and executable use x86_64, ruling out an obvious architecture mismatch (an unsupported executable architecture commonly produces an Exec format error instead). A normal dynamically linked ELF executable specifies its program interpreter in the PT_INTERP segment. Ubuntu uses glibc by default, and on this demonstration VM the requested musl loader is not installed.
To inspect direct shared library dependencies safely without running the executable, use readelf -d (or objdump -p):
readelf -d ./app | grep NEEDED
# (Shows direct dependencies only; alternative: objdump -p ./app | grep NEEDED)Do Not Symlink glibc to musl: Never work around a missing loader by symlinking Ubuntu’s glibc loader (/lib64/ld-linux-x86-64.so.2) to a musl path. glibc and musl are different runtimes; a symlink does not make them interchangeable. For our deliberately mislinked lab executable, rebuild it normally without touching system loader paths.
3. Fix Safely
For a real third-party binary, use a compatible glibc build, a compatible static executable, or a runtime/container matching the binary (a genuine musl build may need a compatible musl runtime). When the source code and required build dependencies are available, compiling natively is a clean fix:
gcc app.c -o app-fixed
LC_ALL=C readelf -l ./app-fixed | grep interpreter
# [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
ls -lL /lib64/ld-linux-x86-64.so.2
test -f /lib64/ld-linux-x86-64.so.2 && test -x /lib64/ld-linux-x86-64.so.2 && echo "OK"
# OK
./app-fixed
# Binary executed successfully!What About 32-Bit x86 Executables? If file reports a 32-bit x86 ELF executable requesting /lib/ld-linux.so.2 on a compatible 64-bit x86 Ubuntu system, you may need to enable i386 multiarch support and install the required 32-bit runtime libraries, starting with libc6:i386 (additional 32-bit libraries may also be required depending on the binary):
sudo dpkg --add-architecture i386
sudo apt update
sudo apt install libc6:i386Case 3: Broken & Relative Symbolic Links
Ordinary read commands such as cat or cp can also return No such file or directory for a file that appears right in your directory listing when that entry is a dangling symbolic link.
1. Reproduce & Diagnose the Dangling Symlink
cd ~/ghost-file-lab/case3-symlinks
ls
# config.conf real-app.conf sub
cat config.conf
# cat: config.conf: No such file or directory
ls -la config.conf
# lrwxrwxrwx 1 alex alex ... config.conf -> /home/alex/ghost-file-lab/case3-symlinks/old-location/app.conf
file config.conf
# config.conf: broken symbolic link to /home/alex/ghost-file-lab/case3-symlinks/old-location/app.conf
readlink -e config.conf
echo $?
# 1readlink -e resolves the complete path and succeeds only when the required path components exist and can be resolved. Here, exit code 1 confirms the path could not be resolved because the symlink target is missing.
2. Diagnose the Relative Symlink Trap
Now check the symlink inside sub/:
ls -la sub/
# lrwxrwxrwx 1 alex alex ... app-link.conf -> real-app.conf
cat sub/app-link.conf
# cat: sub/app-link.conf: No such file or directoryThis link was created from the parent directory with ln -s real-app.conf sub/app-link.conf. Relative symbolic links are always resolved relative to the directory containing the symlink itself, not your current working directory when you ran ln. As a result, sub/app-link.conf looks for sub/real-app.conf, which does not exist.
3. Repair Both Symlinks Safely
Verify Before Using -f: The force option (-f) can overwrite an existing regular file, not just a symbolic link. Always confirm that each destination is the symbolic link you intend to replace before running ln -sfT.
Verify both destinations with ls -ld, guard each replacement with [[ -L ... ]] (assuming no concurrent modifications in the directory), and use GNU ln with -T so the destination is never treated as a target directory:
ls -ld config.conf sub/app-link.conf
[[ -L config.conf ]] && ln -sfT -- "$PWD/real-app.conf" config.conf
[[ -L sub/app-link.conf ]] && ln -sfrT -- real-app.conf sub/app-link.conf
ls -la config.conf sub/app-link.conf
readlink -e config.conf sub/app-link.conf
cat config.conf
# port=8080
cat sub/app-link.conf
# port=8080Using ln -sfrT tells GNU ln to compute the correct relative path (../real-app.conf) automatically.
Case 4: Ghost Filenames (Trailing Spaces & Lookalike Unicode)
Files arriving from web downloads, chat copy-pastes, or shared folders can contain trailing spaces or non-ASCII lookalike characters (homoglyphs) that look identical to plain ASCII names in unquoted output.
1. Reproduce the Illusion
Modern GNU ls automatically quotes unusual filenames in interactive terminals. Using --quoting-style=literal reproduces how filenames appear in raw logs, scripts, or GUIs:
cd ~/ghost-file-lab/case4-filenames
LC_ALL=C.UTF-8 ls -1 --quoting-style=literal
# repоrt.txt
# summary.txt
cat summary.txt
# cat: summary.txt: No such file or directory
cat report.txt
# cat: report.txt: No such file or directory2. Expose the Raw Filename Bytes
In a UTF-8 locale, tools such as ls display many Unicode characters as ordinary printable letters. Combining the C locale with --quoting-style=shell-escape forces non-ASCII bytes into visible escape sequences (changing how names display without modifying the files themselves). Follow with a null-delimited od dump—null bytes (\0) make a reliable separator because Linux filenames can contain newline characters, whereas a null byte can never appear inside a filename:
LC_ALL=C ls -la --quoting-style=shell-escape
# ... 'rep'$'\320\276''rt.txt'
# ... 'summary.txt '
printf '%s\0' * | od -An -tx1c| Character | Unicode Code Point | UTF-8 Hex Bytes | Octal Escape |
|---|---|---|---|
Latin lowercase o (expected) | U+006F | 6f | — (ASCII) |
Cyrillic lowercase о (actual in repоrt.txt) | U+043E | d0 be | \320\276 |
Trailing ASCII space (actual in 'summary.txt ') | U+0020 | 20 | space inside quotes |
3. Rename Safely (Direct Escaped Rename)
Because the diagnostic output already provides the exact escaped filenames, rename both directly using mv -iT --. Interactive mode (-i) prompts for confirmation before overwriting an existing destination file, -T treats the destination as a normal file, and -- ends option parsing:
mv -iT -- 'summary.txt ' summary.txt
mv -iT -- $'rep\320\276rt.txt' report.txt
cat summary.txt
# Q4 server audit passed.
cat report.txt
# Incident report: all nodes healthy.Alternative Technique: Rename by Inode Number
If you encounter a filename that is awkward to copy or escape, you can target it by inode number instead of the second mv command above (run this before renaming the Cyrillic file, or after resetting the lab). Because multiple hard links can share the same inode number, always run find ... -print first to confirm that exactly one intended file matches:
# Step A: Find the inode number (substitute your actual inode for 12345)
ls -i
find . -maxdepth 1 -type f -inum 12345 -print
# Step B: Rename only after confirming a single intended match
find . -maxdepth 1 -type f -inum 12345 -exec mv -iT -- {} report.txt \;
cat report.txtCase 5: Stale Shebang Paths (/usr/bin/python)
Older scripts copied from documentation or repositories often hardcode #!/usr/bin/python. Modern Ubuntu installations commonly provide Python 3 without an unversioned /usr/bin/python command (unless an optional compatibility package such as python-is-python3 is installed).
1. Reproduce & Diagnose
cd ~/ghost-file-lab/case5-shebang
ls -la
# -rwxr-xr-x 1 alex alex ... check_health.py
./check_health.py
# Bash 5.2 (Ubuntu 24.04):
# bash: ./check_health.py: cannot execute: required file not found
#
# Bash 5.1 (version comparison):
# bash: ./check_health.py: /usr/bin/python: bad interpreter: No such file or directoryInspect the first line with head -n 1, then check the exact absolute path with ls -lL rather than command -v python, because an absolute shebang bypasses PATH completely. Use Bash’s type -P python3 to locate the external python3 executable in PATH, and confirm what /usr/bin/env python3 launches:
head -n 1 check_health.py
# #!/usr/bin/python
ls -lL /usr/bin/python
# ls: cannot access '/usr/bin/python': No such file or directory
type -P python3
# /usr/bin/python3
/usr/bin/env python3 --version
# Python 3.12.x2. Fix with an Anchored Replacement
Use a tightly anchored sed substitution so only an exact #!/usr/bin/python line is changed without accidentally modifying versioned paths or interpreter arguments:
sed -i '1s|^#!/usr/bin/python$|#!/usr/bin/env python3|' check_health.py
./check_health.py
# Health check passed on Python 3!Python 2 Compatibility Caution: Changing the shebang does not convert Python 2 code to Python 3. Only switch to #!/usr/bin/env python3 after confirming the script is compatible with Python 3. Using env python3 locates Python 3 through PATH, making the script more portable across systems that provide /usr/bin/env and python3.
Four-Step Ghost File Troubleshooting Checklist
Next time Linux says a file right in front of you does not exist, run these targeted checks before changing permissions or recreating the file. Steps 3 and 4 apply according to file type—they are alternatives, not a fixed pipeline.
| Step | Command | What It Reveals |
|---|---|---|
| 1 | LC_ALL=C ls -la --quoting-style=shell-escape | Reveals symbolic links, target paths, trailing spaces, and non-ASCII filename bytes. |
| 2 | file TARGET | Identifies file type, Windows CRLF line endings, broken symlinks, and ELF architecture (compare against uname -m). |
| 3 Scripts | head -n 1 SCRIPT | cat -A | Inspects the shebang line and exposes hidden ^M carriage returns. |
| 4 ELF Binaries | LC_ALL=C readelf -l BINARY | grep interpreter | Inspects the requested PT_INTERP dynamic linker path (no PT_INTERP may be normal for static builds). |
Verify the Discovered Target or Interpreter
Inspection commands tell you what a file references; always verify whether that target or interpreter actually resolves on disk:
- Symbolic link:
readlink -e LINK - Absolute interpreter or ELF loader:
ls -lL PATHNAMEfollowed bytest -f PATHNAME && test -x PATHNAME && echo "OK" env-based shebang: Verify/usr/bin/envis an executable regular file, then runtype -P PROGRAMin Bash to confirm an external executable exists inPATH.
These four checks quickly isolate many of the most common ghost file problems without altering permissions or recreating files unnecessarily. Remember that these checks are diagnostic clues—other less common causes can involve mount namespaces, containers, chroot environments, or pathname-resolution permissions.
Complete Bash Lab Scripts (Setup, Verify & Cleanup)
Save each script below as its own LF-formatted text file on your disposable Ubuntu VM and run them with bash as a regular user (never with sudo). Setup deliberately refuses to delete an existing ~/ghost-file-lab directory, and cleanup validates ownership and the ghost-file-lab-v2 marker file before requiring interactive DELETE confirmation.
1. setup-ghost-file-lab.sh — Build All Five Broken Lab Fixtures
#!/usr/bin/env bash
set -euo pipefail
umask 077
export LC_ALL=C.UTF-8
die() { printf '[!] %s\n' "$*" >&2; exit 1; }
(( EUID != 0 )) || die 'Run as a regular user, not root or sudo.'
[[ -n ${HOME:-} && $HOME = /* && $HOME != / && -d $HOME ]] || die 'Invalid HOME.'
LAB_DIR="$HOME/ghost-file-lab"
FAKE_MUSL_LOADER=/lib/ld-musl-x86_64.so.1
STALE_PY=/usr/bin/python
[[ $(uname -m) == x86_64 ]] || die 'This recording lab requires x86-64 Linux.'
[[ $(uname -s) == Linux ]] || die 'Linux required.'
[[ ! -e $LAB_DIR && ! -L $LAB_DIR ]] || die "Path already exists: $LAB_DIR. Inspect it; use the companion cleanup only if it is your lab."
for tool in gcc readelf file python3; do
type -P "$tool" >/dev/null || die "Missing $tool; install the prerequisites first."
done
for path in "$FAKE_MUSL_LOADER" "$STALE_PY" $'/bin/bash\r'; do
[[ ! -e $path && ! -L $path ]] || die "Expected absent path already exists: $path. Use a clean VM snapshot. Do not remove system files."
done
for path in /bin/bash /usr/bin/env /lib64/ld-linux-x86-64.so.2; do
[[ -f $path && -x $path ]] || die "Required interpreter unavailable: $path"
done
/usr/bin/env bash -c 'exit 0' || die 'Bash is not available through env.'
/usr/bin/env python3 -c 'import sys; assert sys.version_info >= (3, 6)' || die 'A compatible Python 3 is required.'
if type -P -- $'bash\r' >/dev/null; then die 'Unexpected CR-suffixed bash command in PATH.'; fi
# Refuse existing paths; never recursively delete during setup.
mkdir -- "$LAB_DIR"
printf '%s\n' 'ghost-file-lab-v2' > "$LAB_DIR/.ghost-file-lab-owner"
trap 'printf "[!] Setup failed. Inspect the partial lab before running cleanup.\n" >&2' ERR
printf '[*] Creating lab at: %s\n' "$LAB_DIR"
mkdir -p \
"$LAB_DIR/case1-crlf" \
"$LAB_DIR/case2-elf-loader" \
"$LAB_DIR/case3-symlinks/sub" \
"$LAB_DIR/case4-filenames" \
"$LAB_DIR/case5-shebang"
# Case 1: Hidden Windows CRLF line endings (deploy.sh = 51 bytes)
printf '#!/usr/bin/env bash\r\necho "Deployment succeeded!"\r\n' \
> "$LAB_DIR/case1-crlf/deploy.sh"
printf '#!/bin/bash\r\necho "Direct shebang deployment succeeded!"\r\n' \
> "$LAB_DIR/case1-crlf/deploy-direct.sh"
chmod 755 "$LAB_DIR/case1-crlf/deploy.sh" "$LAB_DIR/case1-crlf/deploy-direct.sh"
# Case 2: Deliberately modified ELF executable requesting musl loader
cat <<'EOF' > "$LAB_DIR/case2-elf-loader/app.c"
#include <stdio.h>
int main(void) {
printf("Binary executed successfully!\n");
return 0;
}
EOF
gcc -Wl,--dynamic-linker="$FAKE_MUSL_LOADER" \
"$LAB_DIR/case2-elf-loader/app.c" \
-o "$LAB_DIR/case2-elf-loader/app"
chmod 755 "$LAB_DIR/case2-elf-loader/app"
# Case 3: Dangling symlink + relative symlink mistake
printf 'port=8080\n' > "$LAB_DIR/case3-symlinks/real-app.conf"
ln -s "$LAB_DIR/case3-symlinks/old-location/app.conf" \
"$LAB_DIR/case3-symlinks/config.conf"
ln -s real-app.conf "$LAB_DIR/case3-symlinks/sub/app-link.conf"
# Case 4: Trailing ASCII space (0x20) + Cyrillic 'о' (U+043E = 0xD0 0xBE)
printf 'Q4 server audit passed.\n' > "$LAB_DIR/case4-filenames/summary.txt "
LOOKALIKE_NAME="$(printf 'rep\xd0\xbe%s' 'rt.txt')"
printf 'Incident report: all nodes healthy.\n' \
> "$LAB_DIR/case4-filenames/$LOOKALIKE_NAME"
# Case 5: Stale absolute shebang (valid Python 3 body)
cat <<'EOF' > "$LAB_DIR/case5-shebang/check_health.py"
#!/usr/bin/python
import sys
print(f"Health check passed on Python {sys.version_info.major}!")
EOF
chmod 755 "$LAB_DIR/case5-shebang/check_health.py"
# Verify the deliberately selected interpreter before reporting success.
readelf -l "$LAB_DIR/case2-elf-loader/app" | grep -F "[Requesting program interpreter: $FAKE_MUSL_LOADER]" >/dev/null
printf '[+] Lab created at: %s\n' "$LAB_DIR"
printf '[i] Run the verification script before recording.\n'
2. verify-ghost-file-lab.sh — Confirm All Eight Intended Failures Reproduce
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C.UTF-8
LAB_DIR="${HOME:?}/ghost-file-lab"
die() { printf '[FAIL] %s\n' "$*" >&2; exit 1; }
(( EUID != 0 )) || die 'Run as the regular lab user.'
[[ -d $LAB_DIR && ! -L $LAB_DIR && -f $LAB_DIR/.ghost-file-lab-owner ]] || die 'Create a fresh lab first.'
# This verifies the intentionally broken fixtures; run before making repairs.
expect_failure() {
local expected=$1 label=$2 output status=0
shift 2
output=$(LC_ALL=C "$@" 2>&1) || status=$?
[[ $status == "$expected" ]] || die "$label: expected exit $expected, got $status: $output"
case $output in
*'No such file or directory'*|*'cannot execute: required file not found'*) ;;
*) die "$label: unexpected diagnostic: $output";;
esac
printf '[PASS] %s (exit %s)\n%s\n' "$label" "$status" "$output"
}
cd "$LAB_DIR/case1-crlf"
[[ $(stat -c %s deploy.sh) == 51 ]] || die 'Unexpected CRLF script size.'
file deploy.sh deploy-direct.sh
expect_failure 127 'Case 1: env CRLF' bash -c './deploy.sh'
expect_failure 127 'Case 1: direct CRLF' bash -c './deploy-direct.sh'
cd "$LAB_DIR/case2-elf-loader"
[[ ! -e /lib/ld-musl-x86_64.so.1 ]] || die 'Musl loader is present.'
LC_ALL=C readelf -l ./app | grep -F '[Requesting program interpreter: /lib/ld-musl-x86_64.so.1]'
expect_failure 127 'Case 2: missing ELF loader' bash -c './app'
cd "$LAB_DIR/case3-symlinks"
[[ -L config.conf && ! -e config.conf && -L sub/app-link.conf && ! -e sub/app-link.conf ]] || die 'Expected two dangling symlinks.'
expect_failure 1 'Case 3: dangling absolute symlink' cat config.conf
expect_failure 1 'Case 3: dangling relative symlink' cat sub/app-link.conf
cd "$LAB_DIR/case4-filenames"
[[ -f 'summary.txt ' && -f $'rep\320\276rt.txt' ]] || die 'Expected filename bytes missing.'
LC_ALL=C ls -la --quoting-style=shell-escape
expect_failure 1 'Case 4: trailing space' cat summary.txt
expect_failure 1 'Case 4: Cyrillic lookalike' cat report.txt
cd "$LAB_DIR/case5-shebang"
[[ ! -e /usr/bin/python ]] || die 'Unversioned Python interpreter is present.'
expect_failure 127 'Case 5: stale Python shebang' bash -c './check_health.py'
printf '[+] All eight intended failure demonstrations reproduced.\n'
3. cleanup-ghost-file-lab.sh — Safely Remove the Lab Directory
#!/usr/bin/env bash
set -euo pipefail
die() { printf '[!] %s\n' "$*" >&2; exit 1; }
(( EUID != 0 )) || die 'Run as a regular user, not root or sudo.'
[[ -n ${HOME:-} && $HOME = /* && $HOME != / && -d $HOME ]] || die 'Invalid HOME.'
LAB_DIR="$HOME/ghost-file-lab"
MARKER="$LAB_DIR/.ghost-file-lab-owner"
[[ ! -L $LAB_DIR ]] || die "Refusing a symlink: $LAB_DIR"
if [[ ! -e $LAB_DIR ]]; then
printf '[i] Nothing to clean up: %s\n' "$LAB_DIR"
exit 0
fi
[[ -d $LAB_DIR && -O $LAB_DIR ]] || die 'Expected a lab directory owned by this user.'
[[ -f $MARKER && ! -L $MARKER && -O $MARKER ]] || die 'Lab marker missing or invalid; inspect manually. No files removed.'
[[ $(<"$MARKER") == ghost-file-lab-v2 ]] || die 'Unrecognized lab marker. No files removed.'
printf 'This will permanently remove the lab and any files saved inside it:\n %s\n' "$LAB_DIR"
printf 'Type DELETE to confirm: '
IFS= read -r answer || die 'Confirmation not received.'
[[ $answer == DELETE ]] || die 'Cancelled; no files removed.'
# Only use on the dedicated disposable lab; do not mount other filesystems inside it.
rm -rf --one-file-system -- "$LAB_DIR"
[[ ! -e $LAB_DIR && ! -L $LAB_DIR ]] || die 'Cleanup incomplete; inspect remaining files.'
printf '[+] Removed lab directory: %s\n' "$LAB_DIR"
To reset the lab after completing your repairs, run bash cleanup-ghost-file-lab.sh (type DELETE at the prompt) and then re-run bash setup-ghost-file-lab.sh. Keep unrelated files out of ~/ghost-file-lab and do not mount other filesystems inside it.
Continue the Linux Troubleshooting Labs Series
Pair this lab with Video #1 to master both halves of Linux file access failures—EACCES (Permission Denied) and ENOENT (No Such File or Directory):
- Companion Lab #1: Why chmod 777 Still Gives “Permission Denied” in Linux
- Video #1 on YouTube: Watch “Why chmod 777 Still Gives Permission Denied in Linux”
Author Laxmi Goswami Updated on 2026-12-03