Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:10:04+00:00 2026-06-14T09:10:04+00:00

I have some working code, it’s very simple – it copies every *.jpg file,

  • 0

I have some working code, it’s very simple – it copies every *.jpg file, renames it into *1.jpg, no worries.

for i in *.jpg; do cp $i ${i%%.jpg}1.jpg;  done

how can I run this, so that it works on every file in a directory, every file in the subdirectories of that directory

example directory structure:

    test/test1/test2/somefile.jpg
    test/anotherfile.jpg
    test/test1/file.jpg
etc
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:10:05+00:00Added an answer on June 14, 2026 at 9:10 am

    The command to do anything recursively to a directory structure is find:

    find . -name "*.jpg" -exec bash -c 'file="{}"; cp "$file" "${file%%.jpg}1.jpg"' \;
    

    Using -exec instead of for i in $(find ...) will handle filenames that contain a space. Of course, there is still one quoting gotcha; if the filename contains a ", the file="{}" will expand to file="name containing "quote characters"", which is obviously broken (file will become name containing quote and it will try to execute the characters command).

    If you have such filenames, or you might, then print out each filename separated with null characters (which are not allowed in filenames) using -print0, and use while read -d $'\0' i to loop over the null-delimited results:

    find . -name "*.jpg" -print0 | \
        (while read -d $'\0' i; do cp "$i" "${i%%.jpg}1.jpg";  done)
    

    As with any complex command like this, it’s a good idea to test it without executing anything to make sure it’s expanding to something sensible before running it. The best way to do this is to prepend the actual command with echo, so instead of running it, you will see the commands that it will run:

    find . -name "*.jpg" -print0 | \
        (while read -d $'\0' i; do echo cp "$i" "${i%%.jpg}1.jpg";  done)
    

    Once you’ve eyeballed it and the results looks good, remove the echo and run it again to run it for real.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far, I have some working code that goes into a list of servers
I have some working code with a crutch to add BOM marker to a
I have some code I am working on that worked just fine until I
I am working on a memory matching game. I have some code that checks
I have been working on some code to submit an uploaded image to PHP
I am working on some code, basically I have a form. On that form
I'm working on some code which fills a GridView full of images that have
Hi guys I have my endless page js code working great but for some
Need some regular expressions help. So far I have my code working to allow
I'm working on some JQuery code and I have a question about caching selectors.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.