How to Deal With Spaces in Filenames on Linux

Published on Aug. 22, 2023, 12:15 p.m.

When dealing with filenames that contain spaces on Linux, it is important to properly quote or escape the spaces in order to avoid errors when running commands.

Here are a few ways to deal with spaces in filenames on Linux:

  1. Use double quotes (“”) or single quotes (‘’) to enclose the filename:
cp "my file" /path/to/destination

Or

cp 'my file' /path/to/destination
  1. Use backslashes to escape the spaces:
cp my\ file /path/to/destination
  1. Use tab completion to avoid typing the filename with spaces altogether:
cp my<tab>/file /path/to/destination

This will autocomplete the filename for you, escaping the spaces as necessary.

Keep in mind that different command line tools may require different quoting or escaping methods, so it’s important to consult the documentation when in doubt.

I hope this helps you deal with spaces in filenames on Linux!

Tags:

related content