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

  • SEARCH
  • Home
  • 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 8212241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:41:19+00:00 2026-06-07T10:41:19+00:00

I have a list of folders. Each folder names for example: pippo1 containing the

  • 0

I have a list of folders. Each folder names for example:

  • pippo1 containing the file pippo1.txt,
  • pippo2 containing the file pippo2.txt,
  • pippo3 containing the file pippo3.txt etc.

I would like to go in the first folder, remove the number from the filename pippo1.txt and then exit, go in the second folder called pippo2 containing the file pippo2.txt, remove 2 from the file name etc etc. I tried the following code, but it doesn’t work! the code is:

for i in *
do
    cd $i
    ls *.txt | sed -e "s/[0-9]//g"
    cd ..
done

What am I doing wrong?

  • 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-07T10:41:21+00:00Added an answer on June 7, 2026 at 10:41 am

    If the requirements are just that simple, then you can do this with only bash substitution:

    for file in `find . -name 'pippo*.txt'`; do
        mv $file ${file/[0-9].txt/.txt}
    done
    

    However, this will fail on more than single digits in the filename, e.g. pippo999/pippo999.txt. This can be addressed by setting the extglob option:

    shopt -s extglob
    for file in `find . -name 'pippo*.txt'`; do
        mv $file ${file/+([0-9]).txt/.txt}
    done
    

    The +(...) construct in bash file globbing is only available when extglob is set.

    Instead of traversing the directory tree with a loop and cd, this uses find to get a list of all the relevant files, then loops over that list of filenames, which will be of the form: ./pippoN/pippoN.txt. The rename uses bash parameter replacement to get the modified file name. See also filename expansion.

    Note that spaces in file or directory names will break this approach, beginning with the use of find: a path that includes spaces will be split up into multiple tokens around the spaces with each token being a separate result in the list iterator. So if you need to anticipate space characters in the paths then this becomes a bit more complicated:

    shopt -s extglob
    IFS=$'\n'
    for file in `find . -name 'pippo*.txt'`; do
        mv "$file" "${file/+([0-9]).txt/.txt}"
    done
    

    Here we’ve quoted the filenames for the mv command, which will handle spaces correctly, and we’ve changed the field separator IFS so that bash won’t recognize whitespace as a word boundary. This will fix up the problem with find in the loop iterator. Now spaces in the path will be handled correctly, so that e.g. ./pippo 9/pippo9.txt will also be renamed to: ./pippo 9/pippo.txt.

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

Sidebar

Related Questions

I have a folder which file name is: List of names. Inside that folder
I have a file folders.txt one two three four ... that has a list
I have over a thousand folders, each folder contains one or more files with
I have a list of folders that has those names variations: (there are around
I have the following code that displays all folders and files within each folder.
I have an XML file with a list of image names (plus other data)
I have this much: A table which used to store the Folders. Each folder
enter code here I have a property file placed in the etc folder. myapplication.properties
I have a list of folders in my python file, and in an HTML
My file system contains a large number of folders, with the folder names in

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.