?
Solved

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

Posted on 2008-10-08
2
Medium Priority
?
923 Views
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:
Example:
group of files
[...]
005512345.txt
0055742394.txt
[...]
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.
0
Comment
Question by:keserm
[X]
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
2 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 2000 total points
ID: 22676095
try

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

Author Comment

by:keserm
ID: 22676178
omarfarid,
yes, works perfect.

thank you
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
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 (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

800 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