Solved

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

Posted on 2008-10-08
2
913 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
2 Comments
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
busybox bash/ash - bash -x | grep 19 121
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 88
EXCHANGE 2007, SCRIPTS 67 75
how to pick specific file from ftp 13 40
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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