NetAdminTools.com
 
Categories:
GNU/Linux | Homebrew designs | Perl | Ruby | Administration | Backup/Recovery | Bugs/Fixes | Certification | Database | Email | File/Print | Hardware | Information Grab Bag | Interoperability | GNU/Linux ABCs | Monitoring | Name Resolution | Network Services | Networking | Remote Control | Security | Desktop | Web | BSD | Solaris | GIAGD | ERP | REALbasic

Last 30 Days | Last 60 Days | Last 90 Days | All Articles | RSS


Categories:
·GNU/Linux
·Homebrew designs
·Perl
·Ruby
·Administration
·Backup/Recovery
·Bugs/Fixes
·Certification
·Database
·Email
·File/Print
·Hardware
·Information Grab Bag
·Interoperability
·GNU/Linux ABCs
·Monitoring
·Name Resolution
·Network Services
·Networking
·Remote Control
·Security
·Desktop
·Web
·BSD
·Solaris
·GIAGD
·ERP
·REALbasic
·All Categories


Rename and Increment Files with Bash
Topic:GNU/Linux   Date: 2008-08-24
Printer Friendly: Print   Mobile View: mobile

spacerspacer
<<  <   >  >>

Subject

Doing a visual audit of a data center with a digital camera is a quick and easy way to help get your bearings when assisting remote hands. We have a bunch of pictures we took of our server racks. Notice that some pictures that weren't very good are already deleted:

$ ls
IMG_0918.JPG	IMG_0925.JPG	IMG_0931.JPG	IMG_0940.JPG
IMG_0922.JPG	IMG_0926.JPG	IMG_0932.JPG	IMG_0945.JPG
IMG_0923.JPG	IMG_0927.JPG	IMG_0938.JPG	IMG_0946.JPG
IMG_0924.JPG	IMG_0930.JPG	IMG_0939.JPG	IMG_0947.JPG

Using a simple bash script, we can rename these and put them in order:

$ for i in *.JPG; do let j+=1 ; mv $i datacentershots$j.jpg ; done
$ ls
datacentershots1.jpg	datacentershots13.jpg	datacentershots2.jpg	datacentershots6.jpg
datacentershots10.jpg	datacentershots14.jpg	datacentershots3.jpg	datacentershots7.jpg
datacentershots11.jpg	datacentershots15.jpg	datacentershots4.jpg	datacentershots8.jpg
datacentershots12.jpg	datacentershots16.jpg	datacentershots5.jpg	datacentershots9.jpg
$ 

As long as we keep the shell open, the increment variable, j, will let us add pictures and continue where we left off:

$ set | tail -n 1
j=16
$ 

Alternatively, if we want to start numbering the pictures at 5, we could set j and run the script again:

$ export j=4
$ for i in *.JPG; do let j+=1 ; mv $i datacentershots$j.jpg ; done
$ ls
datacentershots10.jpg	datacentershots14.jpg	datacentershots18.jpg	datacentershots6.jpg
datacentershots11.jpg	datacentershots15.jpg	datacentershots19.jpg	datacentershots7.jpg
datacentershots12.jpg	datacentershots16.jpg	datacentershots20.jpg	datacentershots8.jpg
datacentershots13.jpg	datacentershots17.jpg	datacentershots5.jpg	datacentershots9.jpg
$

Let's take the same set of pictures but create a smaller version with a width of 600 pixels so it dosn't overrun the screen:

$ ls
IMG_0918.JPG	IMG_0925.JPG	IMG_0931.JPG	IMG_0940.JPG
IMG_0922.JPG	IMG_0926.JPG	IMG_0932.JPG	IMG_0945.JPG	
IMG_0923.JPG	IMG_0927.JPG	IMG_0938.JPG	IMG_0946.JPG
IMG_0924.JPG	IMG_0930.JPG	IMG_0939.JPG	IMG_0947.JPG



$ export j=0
$ for i in *.JPG; do let j+=1 ; convert -scale 600 $i datacentershotssm$j.jpg ; mv $i datacentershotslg$j.jpg ; done
$
$ ls
datacentershotslg1.jpg	datacentershotslg5.jpg	datacentershotssm15.jpg
datacentershotslg10.jpg	datacentershotslg6.jpg	datacentershotssm16.jpg
datacentershotslg11.jpg	datacentershotslg7.jpg	datacentershotssm2.jpg
datacentershotslg12.jpg	datacentershotslg8.jpg	datacentershotssm3.jpg
datacentershotslg13.jpg	datacentershotslg9.jpg	datacentershotssm4.jpg
datacentershotslg14.jpg	datacentershotssm1.jpg	datacentershotssm5.jpg
datacentershotslg15.jpg	datacentershotssm10.jpg	 datacentershotssm6.jpg
datacentershotslg16.jpg	datacentershotssm11.jpg	 datacentershotssm7.jpg
datacentershotslg2.jpg	datacentershotssm12.jpg	 datacentershotssm8.jpg
datacentershotslg3.jpg	datacentershotssm13.jpg	 datacentershotssm9.jpg
datacentershotslg4.jpg	datacentershotssm14.jpg	
$

The images with lg are the original large images. The images with sm are scalled to 600 pixels across.


People:
Places:
Things:
Times:





Please read our Terms of Use and our Privacy Policy
Microsoft, Windows, Windows Server are either trademarks or registered trademarks of Microsoft Corporation. NetAdminTools.com is not affiliated with Microsoft Corporation. Linux is a registered trademark of Linus Torvalds, and refers to the Linux kernel. The operating system of most distributions that contain the Linux kernel is GNU/Linux. All logos and trademarks in this site are property of their respective owner. Copyright 1997-2013 NetAdminTools.com