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 8285499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:29:00+00:00 2026-06-08T11:29:00+00:00

I have this script that does only the parent folder, it does not rename

  • 0

I have this script that does only the parent folder, it does not rename sub folders and files – i’m hoping to get it to strip all non numeric with a _ to it.

#!/bin/bash
for f in *
do
   new="${f// /_}"
   if [ "$new" != "$f" ]
   then
      if [ -e "$new" ]
      then
         echo not renaming \""$f"\" because \""$new"\" already exists
      else
         echo moving "$f" to "$new"
         mv "$f" "$new"
      fi
   fi
done
  • 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-08T11:29:02+00:00Added an answer on June 8, 2026 at 11:29 am

    Getting the file and directory list:

    To operate on files recursively, using find is a better solution compared to globs. I would recommend populating a bash array with the file names before you start operating on them. Also I think doing this one step at a time, directories then files, would be prudent. You don’t want to rename a directory and then later while renaming a file find that the file does not exist. It is also important that the script works on progressively deeper levels on the filesystem hierarchy for the same reason (hence the use of sort below).

    Operating on the lists:

    Once you have the list, you can call on a common shell function to do the file or directory name normalising and renaming. Please note the importance of quoting the names properly to get what you want. This is extremely important since bash (or any shell for that matter) uses spaces as word boundary while parsing the command line.

    The script:

    The following script (named ./rename_spaces.bash in the example output below) should do what you want. To add your own weird characters, add them to the weirdchars variable. Note that you need to escape the characters as appropriate (e.g. the single quote has been escaped). The script skips over with a message if the new file name exists. This also means it will print the message for trivial renames (file names that did not have weird characters in their original names). This can be annoying to some people (e.g. me :-p)

    #!/bin/bash
    
    # set -o xtrace # uncomment for debugging
    
    declare weirdchars=" &\'"
    
    function normalise_and_rename() {
      declare -a list=("${!1}")
          for fileordir in "${list[@]}";
          do
              newname="${fileordir//[${weirdchars}]/_}"
              [[ ! -a "$newname" ]] && \
                mv "$fileordir" "$newname" || \
                    echo "Skipping existing file, $newname."
          done
    }
    
    declare -a dirs files
    
    while IFS= read -r -d '' dir; do
        dirs+=("$dir")
    done < <(find -type d -print0 | sort -z)
    
    normalise_and_rename dirs[@]
    
    while IFS= read -r -d '' file; do
        files+=("$file")
    done < <(find -type f -print0 | sort -z)
    
    normalise_and_rename files[@]
    

    Here is a sample output with a directory tree with directories and files with spaces in there names before and after running the above script.

    $ tree
     .
     ├── dir1
     │   ├── subdir1\ with\ spaces
     │   │   └── file1
     │   └── subdir2\ with\ spaces&weird\ chars'
     │       └── file2
     ├── dir2
     │   ├── subdir1\ with\ spaces
     │   │   └── file1\ with\ space
     │   └── subdir2\ with\ spaces
     │       └── file2\ with\ space
     ├── dir3
     │   ├── subdir1
     │   │   └── file1
     │   ├── subdir2
     │   │   └── file2
     │   └── subdir3
     │       └── file3
     └── rename_spaces.bash
    
     10 directories, 8 files
     $ ./rename_spaces.bash
     $ tree
     .
     ├── dir1
     │   ├── subdir1_with_spaces
     │   │   └── file1
     │   └── subdir2_with_spaces_weird_chars_
     │       └── file2
     ├── dir2
     │   ├── subdir1_with_spaces
     │   │   └── file1_with_space
     │   └── subdir2_with_spaces
     │       └── file2_with_space
     ├── dir3
     │   ├── subdir1
     │   │   └── file1
     │   ├── subdir2
     │   │   └── file2
     │   └── subdir3
     │       └── file3
     └── rename_spaces.bash
    
     10 directories, 8 files
    

    Note: Implementing the script to “do the right thing” for anything non-alphanumeric seems to be non-trivial. For example, I am not sure how to deal with dots or pre-existing underscores or other “regular” allowed characters in the file or directory names.

    Identifying undesirable/special characters in a generic way is also a problem. It is even more complicated in international language environments. I do not know any easy way of saying allow “only numerals or characters from the English alphabet”. If anyone has ideas, please go ahead and post an answer.

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

Sidebar

Related Questions

I have to write a Matlab script that does this: The input is 2
I have a simple script that does some search and replace. This is basically
I have this nifty little script that does a nice job of manipulating some
I have a page with a script that does only 2 things: change the
I have this script that run to fix my menu bar to the browser
I have this script that collects data from users and I want to check
I have this script that works, but I need to change the format of
I have this jQuery script that works fine: $(select).change(function () { var finalPrice =
I have this simple script that I'm working on. I must admit, I'm totally
I have this ruby script that allows me to enter the name of a

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.