Bash script to move and rename files based on a file name

Posted on 2008-10-08
Last Modified: 2013-12-26
I have a bunch of files that I need to move based on a file name and than once moved files need to be renamed:
group of files
to be moved based on its 4th and 5th character (55) to a folder 55 and then renamed to file 12345..742394.txt (remove first four characters). All files are numeric names and all folders are in the same directory level, and folders are already created.

Please help ASAP.
Thank you.
Question by:keserm
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Accepted Solution

omarfarid earned 500 total points
ID: 22676095

cd /dir
ls | while read file
   dir=`echo $file | cut -c3,4`
   nfile=`echo $file | cut -c5-`
   mv $file /path/to/$dir/$nfile

Author Comment

ID: 22676178
yes, works perfect.

thank you

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question