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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:09:03+00:00 2026-06-02T05:09:03+00:00

Edit: I think this has been answered successfully, but I can’t check ’til later.

  • 0

Edit: I think this has been answered successfully, but I can’t check ’til later. I’ve reformatted it as suggested though.

The question: I have a series of files, each with a name of the form XXXXNAME, where XXXX is some number. I want to move them all to separate folders called XXXX and have them called NAME. I can do this manually, but I was hoping that by naming them XXXXNAME there’d be some way I could tell Terminal (I think that’s the right name, but not really sure) to move them there. Something like

mv *NAME */NAME

but where it takes whatever * was in the first case and regurgitates it to the path.

This is on some form of Linux, with a bash shell.

In the real life case, the files are 0000GNUmakefile, with sequential numbering. I’m having to make lots of similar-but-slightly-altered versions of a program to compile and run on a cluster as part of my research. It would probably have been quicker to write a program to edit all the files and put in the right place in the first place, but I didn’t.

This is probably extremely simple, and I should be able to find an answer myself, if I knew the right words. Thing is, I have no formal training in programming, so I don’t know what to call things to search for them. So hopefully this will result in me getting an answer, and maybe knowing how to find out the answer for similar things myself next time. With the basic programming I’ve picked up, I’m sure I could write a program to do this for me, but I’m hoping there’s a simple way to do it just using functionality already in Terminal. I probably shouldn’t be allowed to play with these things.

Thanks for any help! I can actually program in C and Python a fair amount, but that’s through trial and error largely, and I still don’t know what I can do and can’t do in Terminal.

  • 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-02T05:09:05+00:00Added an answer on June 2, 2026 at 5:09 am

    SO many ways to achieve this.

    I find that the old standbys sed and awk are often the most powerful.

    ls | sed -rne 's:^([0-9]{4})(NAME)$:mv -iv & \1/\2:p'
    

    If you’re satisfied that the commands look right, pipe the command line through a shell:

    ls | sed -rne 's:^([0-9]{4})(NAME)$:mv -iv & \1/\2:p' | sh
    

    I put NAME in brackets and used \2 so that if it varies more than your example indicates, you can come up with a regular expression to handle your filenames better.

    To do the same thing in gawk (GNU awk, the variant found in most GNU/Linux distros):

    ls | gawk '/^[0-9]{4}NAME$/ {printf("mv -iv %s %s/%s\n", $1, substr($0,0,4), substr($0,5))}'
    

    As with the first sample, this produces commands which, if they make sense to you, can be piped through a shell by appending | sh to the end of the line.

    Note that with all these mv commands, I’ve added the -i and -v options. This is for your protection. Read the man page for mv (by typing man mv in your Linux terminal) to see if you should be comfortable leaving them out.

    Also, I’m assuming with these lines that all your directories already exist. You didn’t mention if they do. If they don’t, here’s a one-liner to create the directories.

    ls | sed -rne 's:^([0-9]{4})(NAME)$:mkdir -p \1:p' | sort -u
    

    As with the others, append | sh to run the commands.

    I should mention that it is generally recommended to use constructs like for (in Tim’s answer) or find instead of parsing the output of ls. That said, when your filename format is as simple as /[0-9]{4}word/, I find the quick sed one-liner to be the way to go.

    Lastly, if by NAME you actually mean “any string of characters” rather than the literal string “NAME”, then in all my examples above, replace NAME with .*.

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

Sidebar

Related Questions

My apologies if this question has been answered already - I have searched, but
So, I'm sure this has been answered somewhere out there before, but I couldn't
First, I apologize if this has been answered. I've seen many similar questions but
I bet this has been answered many times over, but... For a simple situation
I know this has been asked before but I can't find it so... Say
I'm sure this has been asked hundreds of times, but I just can't point
I know this has been asked before , but I don't think these solutions
I'm sorry if this question has already been answered but I couldn't find it.
I have been given a project to edit. I think this is a simple
I apologize if this has been asked before, but I haven't quite found the

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.