Sometimes, using linux to modify file names in batches can save a lot of manpower operations. This is a very practical tip and I will share it with you.
Modify file names in batches in linux
0. Problem Source: add the date order to some files. That is, add the current year, month, and day before the file name.
The date format is as follows:
ZianEd@ Ubuntu :~ /Public_web_sSmO9OUVY1/FileS/image $Date+ % Y % m % d
20091112
1. UseFind, Awk/Grep/Sed,MvAnd so onCommandCombination
Under Ubuntu
Use the awk command to create an image as follows:
1) Find the file name to be operated
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ find-name &LsQuo; *. jpg '-printf % fn | grep-v ^ [0-9] | grep-v logo.jpg
Se_platfoRm. Jpg
Soa1.jpg
Soa4.jpg
Java_primitive_data_type.jpg
Soa3.jpg
Soa2.jpg
2) extract the file name and keep the file extension unchanged.
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ find-name '*. jpg '-printf % fn | grep-v ^ [0-9] | grep-v logo.jpg | awk-F '. ''{print $1 }'
Se_platform
Soa1
Soa4
Java_primitive_data_type
Soa3
Soa2
3) replace files in batches
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ find-name '*. jpg '-printf % f n | grep-v ^ [0-9] | grep-v logo.jpg | awk-F '. ''{print $1} '|
Xargs-I {} mv 00000000.jpg 'date used policyuncdomaind'00000000.jpg
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ ls
20090818a.jpg 20090919twitter4.jpg
20090818learn.jpg 20091110open_project.jpg
20090819java1.png 20091112java_primitive_data_type.jpg
20090819javaseplatformroadmap.gif 20091112se_platform.jpg
20090916neiji.jpg 20091112soa1.jpg
20090916waiji.jpg 20091112soa2.jpg
20090919photo.jpg 20091112soa3.jpg
20090919twitter.jpg 20091112soa4.jpg
20090919twitter2.jpg logo.jpg
20090919twitter3.jpg
Use the nawk command to create the file as follows:
1) Find the file name to be operated
Zianed @ ubuntu :~ /Sedcommand $ find./-type f | grep "log" | sed-n's/. ///P'
Test2.log
Test1.log
2) Change the file name
Zianed @ ubuntu :~ /Sedcommand $ ls *. log
Test1.log test2.log
Zianed @ ubuntu :~ /Sedcommand $ find./-type f | grep "log" | sed-n's/. ///P' |
Nawk '{oldname = $1; $2 = 'date + % Y % m % d' $1; newname = $1; print "mv" oldname "" newname} '|/bin/sh
Nawk: warning: escape sequeNcE '.'TrEated as plain '.'
Zianed @ ubuntu :~ /Sedcommand $ ls *. log
Test1_test.log test2_test.log
Zianed @ ubuntu :~ /Sedcommand $
Use the sed command to perform the following operations:
Find/-type f-name "*. SQL" | sed s/(. *). SQL/mv & 1.pls/ '| sh
2. Compile a Shell script to run
Save as a Shell script
#! /Bin/bash
# Script_name: rename_ALL
Find./-type f | grep "txt" | sed-n's/. // p '| nawk' {oldname = $1;
SuB (/./, "_ test .");
Newname = $1;
Print "mv" oldname "" newname} '|/bin/sh
Save the above Code as the file rename_ALL
Modify the attributes of the script so that it can execute:ChmodU + x rename_ALL
Run:./rename_ALL
From Network
It looks very useful. It would be better to transfer the file name format to be modified.
3. Use the rename Command
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ rename-help
Unknown option: help
Usage: rename [-v] [-n] [-f] perlExPr [filenames]
Man rename found,
"Rename" renames the filenames supplied according to the rule specified as the first argument.
The perlExprArgument is a Perl expressionWhichIs expected to modify the $ _ string in Perl for at least some of the filenames specified. if a given filename is not modified by the expression, it will not be renamed. if no filenames are given on the command line, filenames will be read via standard input.
For example, to rename all files matching "*. bak" to strip the extension, you might say
Rename's/. bak $ // '*. bak
To translate uppercase names to lower, you 'd use
Rename 'y/A-Z/a-z /'*
Command Format:
Rename [-v] [-n] [-f] perlexpr [files]
Perlexpr is a Perl script format.Regular ExpressionExpression.
Files file name wildcard
Parameters:
-V,-verbose
Verbose: print names of files successfully renamed.
Verbose mode: print the list of successfully changed file names
-N,-no-act
No Action: show what files wowould have been renamed.
Test Mode: The command is not actually executed, but only used to show which file names should be changed.
-F,-force
Force: overWriteExisting files.
Force mode: when you change the file name, if the changed file already exists, overwrite the existing file.
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ ls
20090819java1.png 200909v9twitter4.jpg
20090819javaseplatformroadmap.gif 2009vvv0epen_prEject. Jpg
200908v8lerrn.jpg 2009vvv2jrvr_primitive_drtr_type.jpg
200908v8r.jpg 2009vvv2se_plrtferm.jpg
200909v6neiji.jpg 2009vvv2ser2.jpg
200909v6wriji.jpg 2009vvv2ser3.jpg
200909v9phete.jpg 2009vvv2ser4.jpg
200909v9twitter.jpg 2009vvv2serv.jpg
200909v9twitter2.jpg lege.jpg
200909v9twitter3.jpg
Change 09v9 in *. jpg file name to 0919
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ rename's/09v9/0919/'*. jpg
Zianed @ ubuntu :~ /Public_web_sSmO9OUVY1/files/image $ ls
20090819java1.png 200909v6wriji.jpg
20090819javaseplatformroadmap.gif 2009vvv0epen_preject.jpg
200908v8lerrn.jpg 2009vvv2jrvr_primitive_drtr_type.jpg
200908v8r.jpg 2009vvv2se_plrtferm.jpg
20090919phete.jpg 2009vvv2ser2.jpg
20090919twitter.jpg 2009vvv2ser3.jpg
20090919twitter2.jpg 2009vvv2ser4.jpg
20090919twitter3.jpg 2009vvv2serv.jpg
20090919twitter4.jpg lege.jpg
200909v6neiji.jpg
Typical rename applications:
0. Modify file extensions in batches
$ Rename's/. txt/. ext /'*
1. Batch delete file extensions
$ Rename's/. txt //'*
2. Add file extensions in batches
$ Rename's/$/. txt /'*
3. rename files in batches in your own way
$ Rename's/(d)/Chapter $1 /'*
$ Rename s/_/-/*. jpg modify _ in all. jpg files containing _-