When working on the command line, administrators are often faced with the need to do something with a specific file: delete, move, copy. When performing such tasks, it is often necessary to refer to files by name, which can be difficult, since file names can contain a variety of characters. Even those that are not on the keyboard. In this regard, the work can be facilitated by the file manager, in which the file can be simply selected and performed with the desired action. But for those who are used to working exclusively on the command line, the following methods are offered.
As a shell shell, consider bash, as the most used. And as a file operation, consider deletion as the most destructive.
Situations can be different. In the current directory there may be files that need to be deleted along with those that need to be left. Their names can be very different. Moreover, the former may differ from the latter only by one intricate character.
The file name has a service symbol bash
The easiest case. To delete files that contain service characters in their names like spaces, quotes, double quotes, asterisks, back quotes, etc., you can screen them with a backslash or use single quotes:
')
$ rm -f my \: file \ name
$ rm -f 'my file with white: spaces: and: colons'
$ rm -f \ (filename \)
Using single quotes, you cannot delete a file with a single quote in its name, even if it is shielded.
A complete list of service characters and the screening mechanism in
bash can be found in
man bash . Section QUOTING.
File name begins with a hyphen
Delete a file starting with a hyphen by simple escaping will not work, and the
rm command will perceive the hyphen as the beginning of its argument. Solving the problem is quite simple:
$ rm -f ./-abc
or:
$ rm -f - -abc
Delete by wildcard
If deleting files falls under the wildcard mask, then you can delete the entire group of files:
$ rm -f * .jpg
Files with a control character in the name
The name of the file can contain an ASCII control character, such as line break (\ n), tabulation (\ t), backspace (\ b). These are characters with ASCII codes less than 0x20, as well as DELETE and ESC characters. The following construction is suitable for deleting such files:
$ rm -f $ 'any \ nfile'
$ rm -f $ 'any \ bfile'
Another way to delete such files is to enter a control character from the keyboard. To do this, use the key combination that shields the next character entered, thereby prohibiting the system from processing it. Typically, this combination is CTRL + V. You can verify this with the
stty -a command by looking at the
lnext parameter. Delete the file containing the escape character:
$ rm -f any # press CTRL + V, then ESC
$ rm -f any ^ [# append the last characters
$ rm -f any ^ [file
Deleting files with utf8 characters
If the file name contains a character in utf8 encoding, which we cannot type on the keyboard, then such a file can be deleted by selecting it with the mouse, copying it to the clipboard and then pasting the
rm command. The main condition is that our terminal should work in utf8 encoding. Encoding is set in the terminal settings. Be it xterm, putty or brutal linux tty.
$ ls -1
朲 晦
朲 晩
朲 晪
$ rm -f 朲 晪
File name conversion
Suspecting that the file name is in a different encoding from the terminal encoding, we can recoding all the files in the current directory. As a result, files with broken encoding will be recoded, and files with ascii-symbols will not undergo changes. A significant advantage of this method is to bring all files into a readable form.
$ convmv -fcp1251 -tutf8 *
As you can see, in order to implement the correct transcoding, you need to know two encodings: the intended encoding of the file and the encoding of our terminal. It is most difficult to recognize the intended encoding of a file by incomprehensible characters. There is a wonderful
signYou can also use third-party programs that try to recognize the encoding automatically. For example, the
online decoder Lebedev.
If you encounter such characters in a mounted media or mounted Windows partition, do not rush to transcode anything. Perhaps you simply specified incorrect mount options.
Autocomplete
If in the directory the name of the required file starts uniquely, and this name can be uniquely generated by autocomplete, then this is a fairly simple way to delete the file:
$ rm -f icantype_ # we press TAB
$ rm -f icantype_ \ .jpg
Delete the file through the selection menu
If we got here, things are bad. Let's try to delete a specific file by creating a selection menu for this. In the end, all we have to do is select the desired menu item instead of entering the file name. To do this, we need to program the action that will occur with the file or files after we have entered the necessary menu items.
$ select i in *; do rm -f $ i; done
1) file.zip 3) ??? ???. doc 5) 朲 晩
2) ?? ??. jpg 4) 朲 晦
#? 2
#? ^ C
Delete by inode number
You can delete a file by its inode number. The inode number uniquely identifies the file in the file system. You can find out the inode number using the
ls command , and remove it using
find . The disadvantage of this method is the same as the previous one. Uncomfortable in the case of a large number of files.
$ ls -1
144368 ???. Txt
144363 ??. Txt
$ find. -inum 144368 -delete
Hex removal
And not to mention one harsh method. Deleting by hex codes. The bottom line is this: we recognize the hex codes of all the bytes in the file name, and then delete the file, instead of the name specifying hex codes.
$ for i in *; do echo -n $ i | xxd; done
0000000: face c0d0 32a4 .... 2.
$ rm -f $ '\ xfa \ xce \ xc0 \ xd0 \ x32 \ xa4'
Well, after all, that in practice such files come across infrequently.