DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Top Three Methods for iOS Development on Linux
  • 7 Great Terminal/CLI Tools Not Everyone Knows
  • How to Completely Uninstall Xcode From Mac
  • Debugging Core Dump Files on Linux - A Detailed Guide

Trending

  • When Airflow Tasks Get Stuck in Queued: A Real-World Debugging Story
  • How to Introduce a New API Quickly Using Micronaut
  • Is Big Data Dying?
  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  1. DZone
  2. Data Engineering
  3. Databases
  4. How To Rename Files in Linux and macOS Terminal

How To Rename Files in Linux and macOS Terminal

Let's look at how to rename files on Linux and MacOS.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Apr. 26, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
3.9K Views

Join the DZone community and get the full member experience.

Join For Free

Renaming files is something that occurs frequently, and in Linux on the terminal, there are many ways to rename files. Let's look at some of the main ways you can rename your files on Linux and other Unix-based systems like MacOS.

Renaming Files With the MV Command

The mv command moves a file to another place. It can also be used to move a file to the same location, and simply change its name. For example, if you wanted to rename a file called file-1.txt to file-2.txt, you would first cd into that directory, and run the following:

Shell
 
mv file-1.txt file-2.txt

This technically "moves" the file, but in reality, the end result is that the file is renamed and in the same location.

Renaming Multiple Files

If you want to rename more than one file at once, we can't just use the mv command. For that, we need to loop through each file we want to rename. We can use this in conjunction with the find command to find all the files that fit our criteria easily. For example, the below will find all .txt, .csv and .html files, and turn them into .js files:

Shell
 
for f in $(find . -name '*.txt' -or -name '*.csv' -or -name '*.html'); do 
    mv $f ${f%.*}.js
done

Let's look at how this works in a little more detail:

  • We run a for loop, for f in $(). This will find all files, where it matches what is inside $(). In this example, it finds anything with the file type .txt, .csv, or .html. 
  • For each of the matching files, we run the mv command. We run mv $f ${f%.*}.js.
  • $f is the current matching file we are looping through.
  • ${f%.*}.html is saying rename any file with any extension ({f%.*} could be {f%.html}, {f%.csv}, etc), and change its name to the file name with our new extension, .js.

After that, you'll have successfully moved all files.

MacOS Linux (operating system) terminal

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Top Three Methods for iOS Development on Linux
  • 7 Great Terminal/CLI Tools Not Everyone Knows
  • How to Completely Uninstall Xcode From Mac
  • Debugging Core Dump Files on Linux - A Detailed Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!