Applications of printf in Shell

Source: Internet
Author: User

1. How to use echo to output a string STR = "-n". Because-N is a parameter of ECHO, the General echo "$ Str" method cannot be output.
The solution can be:

Echo x $ STR | SED's/^ x //'
Echo-ne "$ STR \ n"
Echo-e "$ STR \ n \ c"
Printf"% S \ n" $ STR (this can also be done)

Printf format output

The % s format character indicates that the output string is followed by a character array or character pointer.

2. How does Shell define the data structure of a directory? (Find.-type D-maxdepth 1)

Q: What should I do if I want to collect the basic information of each file in a directory, such as the file name, file directory, and size, and generate a file in a unified manner?

A: Use awk to process the results from LS-LH.

1) The last field (usually 9th) in each row is the file name.

2) Does the file directory refer to the directory where the file is located, or is it a directory to determine whether a file is located? If it is the former, use the PWD command to obtain it. (The following code is based on this situation) if it is the latter, the first character of each line is determined based on the LS-lH result. If it is D, it is represented as directory ).

3) obtain the file size based on 5th fields. The shell script is roughly as follows :#! /Bin/shls-lH "$1" | awk 'begin {printf ("FILENAME \ tdirectory \ tsize \ n ")} NR> 1 {printf ("% s \ t % s \ n", $ NF, '\ "$1 \"', $5 )} '> the file.txt exit 0 script is saved as statistic. sh run the following command: Sh statistic. sh [directory name for Statistics]

The results are stored in the file.txt file in the current directory.

Q: The file directory refers to the directory where the file is located. How should I continue to collect statistics on files in sub-directories?

A: There are subdirectories under the directory, and there are subdirectories under the subdirectory. This is endless. Run the "ls-LHR> file.txt" command to list files including subdirectories. Or: Tree-HD> file.txt generates the file directory of the tree structure, including the size of each file (directory) and the last modification time.

Related Article

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.