Skip to Content
bash Cookbook, 2nd Edition
book

bash Cookbook, 2nd Edition

by Carl Albing, JP Vossen
October 2017
Beginner to intermediate
723 pages
15h 31m
English
O'Reilly Media, Inc.
Content preview from bash Cookbook, 2nd Edition

Chapter 17. Housekeeping and Administrative Tasks

These recipes cover tasks that come up in the course of using or administering computers. They are presented here because they don’t fit well anywhere else in the book.

17.1 Renaming Many Files

Problem

You want to rename many files, but mv *.foo *.bar doesn’t work. Or, you want to rename a group of files in arbitrary ways.

Solution

We presented a simple loop to change file extensions in Recipe 5.18; see that recipe for more details. Here is a for loop example:

for FN in *.bad
do
    mv "${FN}" "${FN%bad}bash"
done

What about more arbitrary changes? For example, say you are writing a book and want the chapter filenames to follow a certain format, but the publisher has a conflicting format. You could name the files like chNN=Title=Author.odt, then use a simple for loop and cut in a command substitution to rename them:

for i in *.odt; do mv "$i" "$(echo "$i" | cut -d'=' -f1,3)"; done

Discussion

You should always use quotes around file arguments in case there’s a space. While testing the code in the solution we also used echo and angle brackets to make it very clear what the arguments are (using set -x is also helpful). Once we were very sure our command worked, we removed the angle brackets and replaced echo with mv:

# Testing
$ for i in *.odt; do echo "<$i>" "<$(echo "$i" | cut -d'=' -f1,3)>"; done <ch01=Beginning Shell Scripting=JP.odt><ch01=JP.odt> <ch02=Standard Output=CA.odt><ch02=CA.odt> <ch03=Standard Input=CA.odt><ch03=CA.odt> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Bash Shell Scripting, 2nd Edition

Bash Shell Scripting, 2nd Edition

Sander van Vugt
Bash Cookbook

Bash Cookbook

Ron Brash, Ganesh Sanjiv Naik
bash Cookbook

bash Cookbook

Carl Albing, JP Vossen, Cameron Newham

Publisher Resources

ISBN: 9781491975329Errata Page