We often come across file and folder names. In most cases, file/folder names are related to the contents of the file/folder and begin with a number and a character. Alphanumeric file names are very common and widely used, but not when we have to deal with file/folder names that contain special characters.
Note: We can have any type of file, but for simplicity and ease of implementation we will deal with text files (.txt) throughout this article.
The most common examples of file names are:
rumenz.txt
abc.txt
..
Copy the code
Examples of digital file names are:
121.txt
3221.txt
234.txt
...
Copy the code
Examples of alphanumeric file names are:
abc123.txt
def1.txt
123abc.txt
...
Copy the code
Examples of unusual file names with special characters:
#232.txt
#bkf.txt
#bjsd3469.txt
#121nkfd.txt
-2232.txt
-fbjdew.txt
-gi32kj.txt
--321.txt
--bk34.txt
...
Copy the code
One of the most obvious questions here is who actually creates/processes Hash(#), semicolons (;) , dashes (-), or any other special characters for the file/folder name.
Create a file that begins with a dash -, such as **-abx.txt**.
>touch -abc.txt
touch: invalid option -- 'b'
Try 'touch --help' for more information.
Copy the code
The reason for the above error is that the shell interprets anything after a dash – as an option, which obviously has no such option and is therefore an error.
To fix such errors, we have to tell the Bash shell (this and most of the other examples in this article are Bash specific) not to interpret anything after a special character (a dash in this case) as an option.
There are two ways to resolve this error:
>touch -- -abc.txt [Option #1]
>touch ./-abc.txt [Option #2]
Copy the code
You can verify the files created by both methods to get a long list by running the command ls -l.
>ls -l
total 0
-rw-r--r-- 1 avi avi 0 Jun 8 11:05 -abc.txt
Copy the code
To edit the above file, you can:
>nano -- -abc.txt
or
>nano ./-abc.txt
Copy the code
Note: You can replace Nano with any other editor you choose, such as Vim:
>vim -- -abc.txt
or
>vim ./-abc.txt
Copy the code
To also move such files, you must do the following:
>mv -- -abc.txt -a.txt
or
>mv -- -a.txt -abc.txt
Copy the code
To delete this file, you must:
>rm -- -abc.txt
or
>rm ./-abc.txt
Copy the code
If there are many file names in the folder that contain dashes, and you want to delete them all at once, do the following:
>rm./-* Important note:
1. Follow the same rules as above for any number of hyphens and their occurrence in file names. TXT, ab-c.txt, ab-c.txt, ABC -. TXT, etc.
** For folder names with any number of hyphens and their occurrence, follow the same rules as above, except that “rm -rf” must be used when deleting folders:
>rm -rf -- -abc
or
>rm -rf ./-abc
Copy the code
Handles files with HASH (#) in their names
The symbol # has a very different meaning in BASH. Anything after a is # interpreted as a comment and therefore ignored by BASH.
Create a file **#abc.txt**.
>touch #abc.txt
touch: missing file operand
Try 'touch --help' for more information.
Copy the code
The reason for the above error is that Bash is interpreting the **#abc.txt** comment and therefore ignoring it. So the touch command is already passed without any file operands and is therefore an error.
To fix this type of error, you can ask BASH not to interpret **#** as a comment.
>touch ./#abc.txt
or
>touch '#abc.txt'
Copy the code
Verify that the file you just created is:
>ls -l
total 0
-rw-r--r-- 1 avi avi 0 Jun 8 12:14 #abc.txt
Copy the code
Create a file whose name contains # anywhere except begging.
>touch ./a#bc.txt
>touch ./abc#.txt
Copy the code
or
>touch 'a#bc.txt'
>touch 'abc#.txt'
Copy the code
Run ‘ls -l’ to verify it:
>ls -l
total 0
-rw-r--r-- 1 avi avi 0 Jun 8 12:16 a#bc.txt
-rw-r--r-- 1 avi avi 0 Jun 8 12:16 abc#.txt
Copy the code
What happens when you create two files at the same time (such as a and **# BC **) : >touch a.txt #bc.txt Validates the file you just created:
>ls -l
total 0
-rw-r--r-- 1 avi avi 0 Jun 8 12:18 a.txt
Copy the code
It is obvious from the example above that it only creates file “A” and file “# BC” has been ignored. To successfully execute the above scenario, we can do this,
>touch a.txt ./#bc.txt
or
>touch a.txt '#bc.txt'
Copy the code
And verify it as:
>ls -l
total 0
-rw-r--r-- 1 avi avi 0 Jun 8 12:20 a.txt
-rw-r--r-- 1 avi avi 0 Jun 8 12:20 #bc.txt
Copy the code
File movement:
>mv ./#bc.txt ./#cd.txt
or
>mv '#bc.txt' '#cd.txt'
Copy the code
Copy it as:
>cp ./#cd.txt ./#de.txt
or
>cp '#cd.txt' '#de.txt'
Copy the code
Edit special file name:
>vi ./#cd.txt
or
>vi '#cd.txt'
>nano ./#cd.txt
or
>nano '#cd.txt'
Copy the code
Delete special file names
>rm ./#bc.txt
or
>rm '#bc.txt'
Copy the code
To delete all files with hash # in their file names, you can use:
# rm./#* Process names with semicolons; If you don’t know, semicolons act as command separators in BASH and other shells. Semicolons let you execute more than one command at a time and act as separators. Have you ever handled any file names with semicolons? You would if you weren’t here. Create a file that contains a semicolon.
>touch ; abc.txt touch: missing file operand Try 'touch --help' for more information. bash: abc.txt: command not foundCopy the code
The reason for the above error is that when you run the above command, BASH interprets touch as a command, but can’t find any file operands before the semicolon, so it reports an error. It also reports another error that the “abc.txt” command cannot be found, simply because “abc.txt” is not a command while waiting for another command after the semicolon BASH.
To resolve this type of error, tell BASH not to interpret semicolons as command separators, as follows:
>touch ./'; abc.txt' or >touch '; abc.txt'Copy the code
Note: We enclose the filename ” in single quotation marks. It tells BASH this; Is part of the file name, not the command separator.
The rest of the operations on files and folders with semicolons in their names (that is, copy, move, delete) can be performed directly by enclosing the names in single quotes. Handling the plus sign (+) in file names with other special characters in file/folder names doesn’t require anything extra, just the normal way, simple file names like the one shown below.
You must enclose the file name in single quotation marks, as we did in the case of semicolons. The rest is straightforward..
>touch ‘$12.txt’ % in filename you don’t need to do anything different, treat it as a normal file.
* Including an asterisk in a file name does not change anything, and you can continue to use it as a normal file.
Note: When you must delete files starting with *, do not use the following command to delete such files.
>rm *
or
>rm -rf *
Copy the code
Instead, you use,
>rm./*. TXT Exclamation mark in file name! Simply enclose the file name in single quotes and the rest is the same.
>touch ‘! 12. TXT ‘in filename @ nothing extra, consider filename with At Sign as abnormal file.
>touch ‘@12.txt’ ^ No extra attention in filename. Use the file in the file ^ name as a normal file.
The ampersand (&) in the file name should be enclosed in single quotes, and you’re ready to start.
>touch ‘&12.txt’ parentheses in the file name () If the file name has parentheses, you need to enclose the file name in single quotation marks.
>touch ‘(12.txt)’ curly braces {} in the file name require no extra care. Just think of it as another file.
>touch {12.txt} ### v-shaped <> Filenames with the word V must be enclosed in single quotes.Copy the code
>touch ‘<12.txt>’ square brackets in file name [] treats the file name with square brackets as a normal file and you don’t need to pay special attention to it.
>touch [12.txt] scores in filenames _ They are very common and do not require anything extra. Just do what you would do with a normal file.
>touch _12.txt having the equal = sign in the file name doesn’t change anything, you can use it as a normal file.
>touch =12.txt Handles backslashes; backslashes tell the shell to ignore the next character. You must enclose file names in single quotes, as we did in the semicolon case. The rest is straightforward.
>touch ‘.txt’ special case of forward slash. You cannot create files whose names contain forward slashes (/) until your file system has an error. There is no way to escape the forward slash.
So, if you can create ** files such as **’/12.txt’ or ‘b/c.txt’, then either your file system has an error or you have Unicode support, which allows you to create files with forward slashes. In this case, a forward slash is not a real forward slash, but a Unicode character that looks like a forward slash.
Question mark in file name? Again, you don’t need any special try examples. File names with question marks can be handled in the most general way. >touch ? 12.txt
The dot (.) in the file name Files that begin with dot. Very special in Linux, called dot files. They are hidden files, usually configuration or system files. You must use the switch ‘-a’ or ‘-a’ in the ls command to view such files.
Creating, editing, renaming, and deleting such files is simple.
>touch.12.txt Note: In Linux, you can (.) Include any number of dots in the file name. Unlike other system points in file names, this does not mean that names and extensions are separated. You can create a file with multiple points:
TXT >ls -l total 0-rw-r --r-- 1 avi avi 0 Jun 8 14:32 1.2.3.4.5.6.7.8.9.10.txtCopy the code
The comma in the file name. You can include as many commas in the file name as you want without requiring any additional content. Just do it the normal way, like a simple file name.
>touch ,12.txt
or
>touch ,12,.txt
Copy the code
Colons in file names (:) you can include any number of colons in file names without requiring anything extra. Just do it the normal way, like a simple file name.
>touch :12.txt
or
>touch :12:.txt
Copy the code
Include quotes in filename (single and double quotes) To include quotes in filename, we must use swapping rules. That is, if you need to use single quotation marks in the file name, enclose it in double quotation marks; if you need to use double quotation marks in the file name, enclose it in single quotation marks.
>touch "15'.txt" and >touch '15 '.txt 'Copy the code
Tilde (~) in file names Some editors in Linux, such as Emacs, create backup files for the files being edited. Backup files have the name of the original file and a tilde at the end of the file name. You can have a file with a tilde in its name anywhere, as follows:
>touch ~1a.txt
or
$touch 2b~.txt
Copy the code
Create a file with a name with Spaces between characters/words, say “Hi, my name is rumenz.txt”.
Spaces in filenames are not a good idea; if you must distinguish readable names, use underscores or dashes. However, if you must create such a file, you must use a backslash to ignore its next character. To create the above file, we must do this..
>touch hi\ my\ name\ is\ rumenz.txt
hi my name is rumenz.txt
Copy the code