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.
SO many ways to achieve this.
I find that the old standbys
sedandawkare often the most powerful.If you’re satisfied that the commands look right, pipe the command line through a shell:
I put NAME in brackets and used
\2so 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):As with the first sample, this produces commands which, if they make sense to you, can be piped through a shell by appending
| shto the end of the line.Note that with all these
mvcommands, I’ve added the-iand-voptions. This is for your protection. Read the man page formv(by typingman mvin 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.
As with the others, append
| shto run the commands.I should mention that it is generally recommended to use constructs like
for(in Tim’s answer) orfindinstead of parsing the output ofls. That said, when your filename format is as simple as/[0-9]{4}word/, I find the quicksedone-liner to be the way to go.Lastly, if by
NAMEyou actually mean “any string of characters” rather than the literal string “NAME”, then in all my examples above, replaceNAMEwith.*.