Two Methods for batch file name modification in linux

Source: Internet
Author: User

 

# Two Methods for batch file name modification in linux
# Date: 21: 34 2011-11-10
# Author: Old Boy
# QQ: 31333741 MAIL: oldboy521@hotmail.com
Psite: http://oldboy.cc (coming soon)

# Pblog: http://etiantian.org
######################################## ##################
Question: see the requirements of a brother. I will cut the figure below.

In particular, some brothers are very careful. The above is about removing _ finished. My answer is to remove finished, but it does not affect the overall situation. Please pay attention to it.

Answer:
[Prepare Test Data]
Mkdir/oldboy cd/oldboy
Touch stu_102999_1_finished.jpg
Touch stu_102999_2_finished.jpg
Touch stu_102999_3_finished.jpg
Touch stu_102999_4_finished.jpg
Touch stu_102999_5_finished.jpg

Method 1: shell script for loop combined with sed
[Check Data]
[Root @ oldboy] # ll
Total 4
-Rw-r -- 1 root 85 Oct 2 :54 oldboy_modi_file.sh
-Rw-r -- 1 root 0 Oct 2 stu_102999_1_finished.jpg
-Rw-r -- 1 root 0 Oct 2 :50 stu_102999_2_finished.jpg
-Rw-r -- 1 root 0 Oct 2 :50 stu_102999_3_finished.jpg
-Rw-r -- 1 root 0 Oct 2 12:50 stu_102999_4_finished.jpg
-Rw-r -- 1 root 0 Oct 2 :50 stu_102999_5_finished.jpg

[Development script]
[Root @ oldboy] # cat oldboy_modi_file.sh
#! /Bin/sh
For file in 'ls./*. jpg'
# Shell script for loop, file is the variable to get the results file name of ls./*. jpg in turn
Do
Mv $ file 'echo $ file | sed's/finished // g''
# Use the mv command to change the file. splicing the new file name string is the focus of this question.
Done
# Note: Through this question, you can learn how to use the for loop and how to use commands such as mv and sed.

[Execute the script]
[Root @ oldboy] # sh oldboy_modi_file.sh

[Check Results]
[Root @ oldboy] # ll
Total 4
-Rw-r -- 1 root 85 Oct 2 :54 oldboy_modi_file.sh
-Rw-r -- 1 root 0 Oct 2 stu_102999_0000.jpg
-Rw-r -- 1 root 0 Oct 2 :50 stu_102999_2_.jpg
-Rw-r -- 1 root 0 Oct 2 12:50 stu_102999_3_.jpg
-Rw-r -- 1 root 0 Oct 2 12:50 stu_102999_4_.jpg
-Rw-r -- 1 root 0 Oct 2 :50 stu_102999_5_.jpg


Method 2: ls combined with awk implementation
[Root @ oldboy] # ll
Total 0
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_1_finished.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_2_finished.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_3_finished.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_4_finished.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_5_finished.jpg
[Root @ oldboy] # ls | awk-F 'finished' {print $0 }'
Stu_102999_1_finished.jpg
Stu_102999_2_finished.jpg
Stu_102999_3_finished.jpg
Stu_102999_4_finished.jpg
Stu_102999_5_finished.jpg
[Root @ oldboy] # ls | awk-F 'finished' {print $1 }'
Stu_102999_1 _
Stu_102999_2 _
Stu_102999_3 _
Stu_102999_4 _
Stu_102999_5 _
[Root @ oldboy] # ls | awk-F 'finished' {print $2 }'
. Jpg
. Jpg
. Jpg
. Jpg
. Jpg
[Root @ oldboy] # ls | awk-F 'finished' {print $1 $2 }'
Stu_102999_0000.jpg
Stu_102999_2_.jpg
Stu_102999_3_.jpg
Stu_102999_4_.jpg
Stu_102999_5_.jpg
[Root @ oldboy] # ls | awk-F 'finished' {print "mv" $0 "" $1 $2 ""} '|/bin/bash
[Root @ oldboy] # ll
Total 0
-Rw-r -- 1 root 0 Nov 13 stu_102999_0000.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_2_.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_3_.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_4_.jpg
-Rw-r -- 1 root 0 Nov 13 18:13 stu_102999_5_.jpg
Note: If you need to answer your questions, leave a message on your blog. We exchange, discuss, and make progress together.
The question is not important. The answer to the question is the most important.

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.