Rename multiple files on the command line

Source: Internet
Author: User
From:
Http://www.basicallytech.com/blog? /Archives/10-shell-stuff-rename-multiple-files-on-the-command-line.html # remove_spaces_tr

If you wish to quickly rename multiple files in a directory, a For Loop
(Sometimes combined with other utilities such as sed or Tr) is one way
Do the job.

The examples in this article include removing spaces from filenames,
Adding and removing suffixes and prefixes, and changing from uppercase
Lowercase.

Here is a full list of the examples I'm going to look:
Remove spaces from file names
Alternative way to remove spaces from file names
Add a suffix
Add a prefix
Remove a prefix
Remove a suffix
Uppercase to lowercase
Uppercase to lowercase (suffix only)
An unlikely situation
Remove spaces from file names

I 've got some imaginary MP3 files with spaces in the file names. spaces in
Filenames are a bad idea. It's inconvenient, spaces are the delimiters
Between command-line arguments, and for files you're putting out over
Internet, it can cause other problems, so we're re going to get rid of these
Spaces as a matter of principle. (whose foolish idea was it to put Spaces
Into filenames, anyway ?)

$ LS-l
Total 468
-RW-r -- 1 Rob 15015 01-some song.mp3
-RW-r -- 1 Rob 17313 02-another song.mp3
-RW-r -- 1 Rob 17381 03-yet another song.mp3
-RW-r -- 1 Rob 410075 04-a new song.mp3
-RW-r -- 1 Rob 7327 05-some instrumental
Piece.mp3

I'm going to change the spaces to underscores. Since I don't want to mess
Things up, I'll be cautious to begin:

$ For file in *. MP3; do newfile = 'echo $ file | SED's // _/g''; echo
"$ File will be renamed as $ newfile"; done
01-some song.mp3 will be renamed as 01_-_some_song.mp3
02-another song.mp3 will be renamed as 02_-_another_song.mp3
03-yet another song.mp3 will be renamed as 03_-_yet_another_song.mp3
04-a new song.mp3 will be renamed as 04_-_a_new_song.mp3
05-some instrumental piece.mp3 will be renamed
05_-_some_instrumental_piece.mp3

That looks okay, so I'll try it for real.

$ For file in *. MP3; do newfile = 'echo $ file | SED's // _/g''; mv "$ file"
$ Newfile; done
$ LS-l
Total 468
-RW-r -- 1 Rob 15015 2006-10-20 01_-_some_song.mp3
-RW-r -- 1 Rob 17313 02_-_another_song.mp3
-RW-r -- 1 Rob 17381 2006-10-20 03_-_yet_another_song.mp3
-RW-r -- 1 Rob 410075 04_-_a_new_song.mp3
-RW-r -- 1 Rob 7327
05_-_some_instrumental_piece.mp3

I realize that there is an alternative way, using TR, which is easier (
Type). However I wowould argue that the principle of the above technique
(Using SED) can be applied to more situations, making it more useful as
Template.

For those who are unfamiliar with the command line, I'm going to break
This down into it's constituent components. If you are familiar with this
Sort of thing, feel free to skip down to the other examples.

This is not the place to discuss for loops in great detail. There are
Loads of resources on the Internet for that sort of thing. When I was
Starting to learn this stuff, I always found detailed analysis of these
Things confusing, and that the best way to learn was to actually do it.

For file in *. MP3

In this part of the loop, file is the variable, and *. MP3 is the argument.
The for loop generates a variable for each argument until there are no
Arguments left. In the example abve, the variables generated are all
Files which match the regular expression *. MP3, in other words:
01-some song.mp3
02-another song.mp3
03-yet another song.mp3
04-a new song.mp3
05-some instrumental piece.mp3

; Do

The semicolon is special character which allows you end one command and
Run another command on the same line. Every for requires a do.

Newfile = 'echo $ file | SED's // _/g''

Now I'm creating a new variable called newfile. Sed is used to replace
Each space in the name of the MP3 file referenced by file with
Underscore (S // _/g), so the variable newfile is the same as file, cannot
The value of each variable has every space replaced with an underscore.

The next bit is the command we want to run on each argument:

Mv "$ file" $ newfile

Note that I put the loop-generated $ file variable inside double-quote
Marks for the MV command. This is required because of those spaces in
Original file name. If you don't do that loop will try to run
Following command on each file:
MV 01-some song.mp3 01_-_some_song.mp3

Instead
Mv "01-some song.mp3" 01_-_some_song.mp3

; Done

Finally, every do requires a done.

Here are some other examples.
Alternative way to remove spaces from file names

$ LS-l
Total 468
-RW-r -- 1 Rob 15015 01-some song.mp3
-RW-r -- 1 Rob 17313 02-another song.mp3
-RW-r -- 1 Rob 17381 03-yet another song.mp3
-RW-r -- 1 Rob 410075 04-a new song.mp3
-RW-r -- 1 Rob 7327 05-some instrumental
Piece.mp3

$ For file in *. MP3; Do MV "$ file" 'echo $ file | tr ''' _ ''; done
$ LS-l
Total 468
-RW-r -- 1 Rob 15015 2006-10-20 01_-_some_song.mp3
-RW-r -- 1 Rob 17313 02_-_another_song.mp3
-RW-r -- 1 Rob 17381 2006-10-20 03_-_yet_another_song.mp3
-RW-r -- 1 Rob 410075 04_-_a_new_song.mp3
-RW-r -- 1 Rob 7327
05_-_some_instrumental_piece.mp3
Add a suffix to multiple files

$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices

In this example, I wish to add a. txt suffix to each file in this
Directory.

We don't need sed here:

$ For file in *; Do MV $ file into file.txt; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra.txt
-RW-r -- 1 Rob 194 calculus.txt
-RW-r -- 1 Rob 117 equations.txt
-RW-r -- 1 Rob 402 geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices.txt
Add a prefix

Taking the files from the previous example:

$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra.txt
-RW-r -- 1 Rob 194 calculus.txt
-RW-r -- 1 Rob 117 equations.txt
-RW-r -- 1 Rob 402 geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices.txt

I wish to add a maths _ prefix to each file:

$ For file in *; Do MV $ file maths _ $ file; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 maths_algebra.txt
-RW-r -- 1 Rob 194 maths_calculus.txt
-RW-r -- 1 Rob 117 maths_equations.txt
-RW-r -- 1 Rob 402 maths_geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 maths_matrices.txt
Remove a prefix

Sticking with the files we 've been using:

$ LS-l
Total 20
-RW-r -- 1 Rob 733 maths_algebra.txt
-RW-r -- 1 Rob 194 maths_calculus.txt
-RW-r -- 1 Rob 117 maths_equations.txt
-RW-r -- 1 Rob 402 maths_geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 maths_matrices.txt

I 've changed my mind about that maths _ prefix and want to remove it.

$ For file in *; do newfile = 'echo $ file | SED's/^ maths _ // ''; MV $ File
$ Newfile; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra.txt
-RW-r -- 1 Rob 194 calculus.txt
-RW-r -- 1 Rob 117 equations.txt
-RW-r -- 1 Rob 402 geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices.txt

The ^ symbol in sed matches the start of a line.
Remove a suffix

While I'm at it, I think I'll remove the suffix as well.

$ For file in *. txt; do newfile = 'echo $ file | SED's/. txt $ // ''; MV $ File
$ Newfile; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices

The $ symbol in sed matches the end of a line.
Uppercase to lowercase

$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices

$ For file in *; Do MV $ file 'echo $ file | tr '[A-Z] ''[A-Z]''; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices
Uppercase to lowercase (suffix only)

$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra. txt
-RW-r -- 1 Rob 194 calculus. txt
-RW-r -- 1 Rob 117 equations. txt
-RW-r -- 1 Rob 402 geometry. txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices. txt

$ For file in *. txt; do newfile = 'echo $ file | SED's/. txt $/. txt/''; MV
$ File $ newfile; done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra.txt
-RW-r -- 1 Rob 194 calculus.txt
-RW-r -- 1 Rob 117 equations.txt
-RW-r -- 1 Rob 402 geometry.txt
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices.txt
An unlikely situation

Finally, an unlikely situation. I have a bunch of files with multiple
Spaces and capitalized suffixes. I want the spaces changed to
Underscore, distinct T where there are two or more consecutive spaces, where I
Want just a single underscore. While we're at it, if a space is next
The dot of a prefix, let's just remove it. I also want to make
Suffixes lowercase. Hmm.

$ LS-l
Total 20
-RW-r -- 1 Rob 201 2006-10-23 a file with specified spaces. txt
-RW-r -- 1 Rob 1579 2006-10-23 another wierdly named file. txt
-RW-r -- 1 Rob 1452 2006-10-23 my mate Arron spels beter
This.txt
-RW-r -- 1 Rob 924 2006-10-23 not really. txt
-RW-r -- 1 Rob 379 2006-10-23 whoo named this. txt

I'm not going to try to correct the spelling as well!

$ For file in *; do newfile = 'echo $ file | sed-E's/. txt $/. txt/'-E's /[
] * []/_/G'-E's/_ [.]/./g''; mv "$ file" $ newfile; done
$ LS-l
Total 20
-RW-r -- 1 Rob 201 2006-10-23 a_file_with_many_spaces.txt
-RW-r -- 1 Rob 1579 2006-10-23 another_wierdly_named_file.txt
-RW-r -- 1 Rob 1452 2006-10-23
My_mate_arron_spels_beter_than_this.txt
-RW-r -- 1 Rob 924 2006-10-23 not_really.txt
-RW-r -- 1 Rob 379 2006-10-23 whoo_named_this.txt

That was somewhat contrived, but it does provide an example of the power
Of the command line.

Bear in mind that some people pay for this sort of functionality. Mind
You, consider their option.
Addendum

It occurred to me after I wrote this article that some one might want
Emulate the Windows option.

Giving all the files the same name, but with a different number seems,
Well, foolish, but let's do it anyway. Parentheses are also a bad idea in
Filenames, but this is just an exercise.

$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices

$ Num = 0; for file in *; do num = 'expr $ num + 1'; MV $ File
Foolish \ ($ num \); done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 foolish (1)
-RW-r -- 1 Rob 194 foolish (2)
-RW-r -- 1 Rob 117 foolish (3)
-RW-r -- 1 Rob 402 foolish (4)
-RW-r -- 1 Rob 50 2006-10-20 13:23 foolish (5)

What might be more useful wocould be to retain the original name and
Seperate it from the sequential number with a much more command-line
Friendly underscore.

$ LS-l
Total 20
-RW-r -- 1 Rob 733 Algebra
-RW-r -- 1 Rob 194 Calculus
-RW-r -- 1 Rob 117 Equations
-RW-r -- 1 Rob 402 Geometry
-RW-r -- 1 Rob 50 2006-10-20 13:23 Matrices

$ Num = 0; for file in *; do num = 'expr $ num + 1'; MV $ File $ {file }_$ num;
Done
$ LS-l
Total 20
-RW-r -- 1 Rob 733 algebra_1
-RW-r -- 1 Rob 194 calculus_2
-RW-r -- 1 Rob 117 2006-10-20 equations_3
-RW-r -- 1 Rob 402 geometry_4
-RW-r -- 1 Rob 50 2006-10-20 13:23 matrices_5

Happy renaming.

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.