Working with files and directories prefixed with backticks () can be particularly troublesome when using command-line tools. While GUI interfaces typically handle these characters without issue, terminal commands often misinterpret them as command substitution markers.
The common approach of using a backslash escape doesn't work as expected because:
scp -r host:/\\directory/ ~/ # Fails with syntax errors
The shell first processes the escape sequence before the command interpreter sees it, leading to unexpected behavior.
Solution 1: Single Quotes for Local Paths
cp 'important_file.txt' ~/backups/
# Or with wildcards:
mv 'project'*/ archived_projects/
Solution 2: Double Escaping for Remote Paths
For SSH/SCP operations, you need to escape both for the local shell and the remote shell:
scp -r host:'/\\remote_dir/' ~/local_dest/
# Alternative format:
scp -r host:"'/\\\remote_dir/'" ~/local_dest/
Solution 3: Using $'' Syntax (Bash Specific)
scp -r host:$'/\\remote_path/' ~/local/
# Works for local commands too:
ls -l $'special_file'
Using find with -exec
find . -name '*' -exec cp {} ~/backups \;
Handling Multiple Backticks
rsync -avz 'host:/path/to/dirwithmultipleticks/' ~/destination/
While these solutions work, consider these alternatives to backtick prefixes:
- Use underscore prefix: _important_file
- Dot prefix: .special_project (hidden files)
- Number prefix: 00_priority_item
If you encounter errors like "unexpected EOF" or "syntax error", try:
1. Wrapping the entire path in single quotes
2. Using ASCII code: $'\x60' instead of backtick
3. Temporarily renaming the file/directory
Many developers (myself included) use the backtick character () as a prefix for frequently accessed files and directories. It's conveniently located on the keyboard and provides excellent sorting behavior in GUI file managers. However, this practice creates significant challenges when working with command-line tools like scp, cp, or rsync.
When attempting to escape the backtick in paths, standard escaping methods fail because:
- The backtick has special meaning in shell (command substitution)
- Shell parsing happens before path resolution
- Recursive escaping creates evaluation order problems
# This fails because the shell tries to evaluate the backtick as command substitution
scp remote:/path/file.txt .
# This fails because the escape is processed by the local shell before reaching remote
scp remote:/\\path/file.txt .
Option 1: Single Quotes Protection
The most reliable method is wrapping the entire path in single quotes:
scp -r 'dns.local:/Downloads/CrazyRussianCars/' ~/
Option 2: Double Escaping for Remote Paths
When dealing with remote paths, you need to escape both for local shell and remote shell:
scp -r dns.local:'/\\Downloads/CrazyRussianCars/' ~/
Option 3: Alternative Path Specification
For scripts, consider using alternative path notations:
# Using $HOME expansion
scp -r "dns.local:$HOME/\\\Downloads/CrazyRussianCars/" ~/
# Using absolute path (if known)
scp -r dns.local:/home/user/\\\Downloads/CrazyRussianCars/ ~/
- Avoid backticks in shared paths when possible
- Document special character requirements in READMEs
- Consider symbolic links for frequently accessed paths
- Use tab completion to let the shell handle escaping
When writing scripts that process such paths, use arrays to preserve literal values:
files=(
"/special/file1"
"/special/file2"
)
for file in "${files[@]}"; do
scp "remote:'$(printf '%q' "$file")'" .
done