find filenames with special characters in linux

Find files with special characters in name Linux

Linux allows you to create filenames with special characters in them. Sometimes you may need to find such files on your disk. In this article, we will look at how to find files with special characters in name in Linux. You can use these steps on almost every Linux distribution since these commands are available on most systems.


Find files with special characters in name Linux

We will look at several commands to work with files having special characters in their name.


1. Using Find

Find is the most common tool to find files with special characters in their names. Here is a command to find files with special characters like ~ and * in them.

$ find . -name '*[~*]*'

In the above command, replace dot(.) with the folder location where you want to look for the files. Replace ~* inside square brackets [] with the special characters present in your file’s name.

If your file contains newline character (\n) or tab character (\t) then include them in your find command as shown.

$ find . -name '*[\t \n]*'


2. Using Text Editors (vi, nano)

If you want to open a file with special characters in filename, in text editor, then you need to mention full path, or relative path, or enclose the filename in quotes. Here are the commands to open filenames starting with # character. You can use the same syntax for file names with any special character.

# using vi editor
$ vi ./#abc.txt
$ vi '#abc.txt'
$ vi /home/ubuntu/#abc.txt

# using nano editor
$ nano ./#abc.txt
$ nano '#abc.txt'
$ nano /home/ubuntu/#abc.txt

# using touch
$ touch ./#abc.txt
$ touch '#abc.txt'
$ touch /home/ubuntu/#abc.txt


3. Using mv, cp command

Similarly, if you want to move or copy files with special characters in their filename, you need to mention their full path, relative path or enclose the filename within quotes. Here are commands to move and copy files with special characters in their filenames.

# using mv command
$ mv ./#abc.txt /home/
$ mv '#abc.txt' /home/
$ mv /home/ubuntu/#abc.txt /home/

# using cp command
$ cp ./#abc.txt /home/
$ cp '#abc.txt' /home/
$ cp /home/ubuntu/#abc.txt /home/

That’s it. In this article, we have learnt different ways to deal with filenames with special characters.

Also read:

How to Find Last Occurrence of Character in String in JS
How to Find Last Occurrence of Character in String in Python
How to Split Python List into N Sublists
How to Insert Text At Certain Line in Linux
Django Get Unique Values from Queryset

Leave a Reply

Your email address will not be published. Required fields are marked *