Linux commands: CP, TIME, cut, awk, get file Size command

Source: Internet
Author: User
Tags tmp folder

Recently wrote a shell script that used the CP, time, cut, and awk commands to write down their use methods:

1.CP and MV

CP command: Used to complete the copying of files from the source folder to the destination folder . Both the source and destination folders have source files after execution.

MV command: Used to move files. After execution, there is no source file in the source folder . I often use the MV command to rename files

Note: The CP command source folder cannot be the same as the destination folder, but it can be the same in MV.

Command parsing:

#cp [-r] Source file destination Folder

The CP-R parameter---used when copying the entire folder.

#mv Source File destination Folder

Command instance:

1.1 Copy a single file.

#cp/etc/init.d/rcs/tmp---Copy the RCS file of the INIT.D directory to the TMP directory, RCS is a file name of 1.2

1.2 Copy the folder.

#cp-R/etc/init.d//tmp---Copy all files in the INIT.D directory to the TMP directory INIT.D a folder name

1.3 Copy multiple files.

#cp/tmp/a*.txt/tmp/tmp---

1.4 File renaming

#mv/TMP/TEST1/TMP/TEST2---Speak the tmp folder under Test1 renamed to Test2.

1.5 Moving files

#mv/tmp/test1/home/kuaixiaoqian/test2---Move the test1 file under the/tmp folder to the/kuaixiaoqian folder and rename it to Test2

2.time command

function: used to calculate the execution time of a command.

format:time shell command

return form: after the execution result of the command under the shell, add the following statement:

2.1 Examples of Use:

(1) Easy to use

#time echo "Hello"

return Result:

(2) Save the time value to a file

# (Time echo "Hello") 2>time.txt ---Note: There can be no spaces between 2 and > in 2>time.txt

Execution Result:

3. Cut and awk commands

cut function : Cut each row of data, you can specify a delimiter (single character delimiter), and the field to get. Click here for details

awk function: separates each row of data with a space (which can be a contiguous number of spaces), and gets the specified value. The command is detailed here.

3.1 Examples of Use:

(1) Cut get file size

#ls-L Time.txt | cut-d '-f5 ---Note: Only when Ls-l is separated by a single space, cut takes a number of consecutive spaces to think of as multiple separators

Execution Result:

(2) awk gets the file size

#ls-L Time.txt | awk ' {print $} ' ---awk defaults to a space delimiter, and a contiguous number of spaces is considered a delimiter

Execution Result:

PS: I run a script on the embedded arm board, with the cut command does not get the file size correctly, a careful look only to find that is due to consecutive multiple spaces to separate the ls-l result of the cause

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.