How to handle file names containing spaces and special characters in Linux

Source: Internet
Author: User

How to handle file names containing spaces and special characters in Linux

We often see the file name and folder name. Most of the time, the file/folder name and content are related and begin with a number and a letter. Letters and numbers are the most common and widely used file names. However, you must always process file names/folder names that contain special characters.

Note: We can have different types of files, but in order to make it simple and convenient, in this document we only use the example file (.txt) for demonstration.

Example of the most common file name:

  1. abc.txt
  2. avi.txt
  3. debian.txt
  4. ...

Example of digital file name:

  1. 121.txt
  2. 3221.txt
  3. 674659.txt
  4. ...

Letter/number file name example:

  1. eg84235.txt
  2. 3kf43nl2.txt
  3. 2323ddw.txt
  4. ...

Examples of file names containing special characters are not common:

  1. #232.txt
  2. #bkf.txt
  3. #bjsd3469.txt
  4. #121nkfd.txt
  5. -2232.txt
  6. -fbjdew.txt
  7. -gi32kj.txt
  8. --321.txt
  9. --bk34.txt
  10. ...

An obvious problem is-who on this planet will create and process the inclusion of a well number?(#), Semicolon(;), Break number(-)Or other special characters.

Like you think, this type of file name is really uncommon, but when you have to handle this type of file name, your shell should not go wrong or strike. Technically, everything in Linux, such as folders, drives, and others, is processed as files.

Process a file whose name contains a break (-)

Create with a broken mark(-)For example, -abx.txt.

  1. $ touch -abc.txt
Test output
  1. touch: invalid option --'b'
  2. Try'touch --help'for more information.

The cause of the above error is that the shell returns a broken mark(-)The following content is regarded as a parameter, but it is obvious that this parameter is not used, so an error is returned.

To solve this problem, we need to tell Bash shell (yes, most of the examples below are based on the BASH environment) not to include special characters (here it is a break) the following characters are interpreted as parameters.

There are two ways to solve this error:

  1. $ Touch ---abc.txt [method #1]
  2. $ Touch./-abc.txt [method #2]

You can run the ls or ls-l command to list the details and check the files created in the preceding two methods.

  1. $ ls -l
  2. total 0
  3. -rw-r--r--1 avi avi 0Jun811:05-abc.txt

To edit the preceding file, you can:

  1. $ nano ---abc.txt
  2. Or
  3. $ nano ./-abc.txt

Note: You can replace nano with any other editor you like, such as vim:

  1. $ vim ---abc.txt
  2. Or
  3. $ vim ./-abc.txt

If you just move the file, you can do this:

  1. $ mv ---abc.txt -a.txt
  2. Or
  3. $ mv ---a.txt -abc.txt

To delete such an object, you can:

  1. $ rm ---abc.txt
  2. Or
  3. $ rm ./-abc.txt

If a directory contains a large number of files whose names contain broken numbers, you can delete them all at a time as follows:

  1. $ rm ./-*

Important:

  1. The rules discussed above can also be applied to files with names containing arbitrary numbers and connection symbols at any location. In other words, -a-b-c.txt, ab-c.txt, abc-.txt, and so on.

  2. The rules discussed above can also be applied to folders whose names contain any number of and connecting symbols at any location. In addition to one case, you must use this method when deleting a folder.rm -rf:

    $ Rm-rf ---abc or $ rm-rf./-abc

 

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • 3
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.