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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now