Linux shell scripts traverse file names with spaces

Source: Internet
Author: User

The general statement is:

For f in 'ls ./'

However, if the file name contains spaces, for example, "a B", although ls can be correctly listed, for will use spaces as separators to assign A and B to F respectively, resulting in two file names.

Study found question mark? The file name can also contain spaces. Therefore, you can write as follows:

For f in 'ls./| TR """\? "'

First, replace the space in each line of LS with TR as the question mark. Such F can perform operations such as mV and CP. ECHO can also correct? It is displayed as a space. If String operations are involved, you can directly replace them in the variable.

F = 'tr "\? "<$ F'

Add a recursively changing the file name to lowercase.Program

#! /Bin/sh
function XX () {
Local oldpwd = "$ PWD"
Cd $1
Local F = ""
Local m = ""
for F in' ls. /| TR """\? "'
DO
echo $ F
# F = 'tr "\? "<$ F'
If [-d" $ F "]
then
xx" $ F "
fi
M = 'tr' [A-Z] ''[A-Z] '<" $ F "'
If [" $ F "! = "$ M"]
then
If [-e. /"$ M"]
then
echo in $ PWD: file $ F can \'t be transform since File $ M exists
else
mV. /"$ F ". /"$ M"
fi
done
Cd "$ oldpwd"
}< br> XX $1

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.