Musings on Christianity, Politics, and Computer Science Geekery

Category: Linux tricks

Quick Multi-File Rename in Linux

Call it the little things, but I'm usually excited when I find a new tool that speeds things up.  I accidentally ran a set of experiments that produced multiple files that were named incorrectly.  That is, I wrote files as aq*-female-1_day-*_event*.csv but I was actually supposed to write the files as aq*-female-edt-*_event*.csv.  These files fit into a larger set of results with varying time lengths (edt, month, year, etc), so I needed to change dozens of files to fix my mistake.

Cue the head scratching.  Do I rename each individually?  Do I write a bash or PHP script to perform the renaming, perhaps taking time away from other things? No! I found out there's a tool for that built into the Linux command line environment: rename.

The rename command takes a Perl regular expression and a set of files and performs a rename on them based on the regular expression.  So, in this way, I could rename dozens of files with one command:

> rename 's/(.*)-1_day-(.*)/\1-edt-\2/' *.csv


Quick Bar-Chart of disk usage

Today I was in search of a command that I had used a long time ago, but ran into a much more interesting one instead.  At the time, I must have been needing to discover what files were the largest disk hogs and if there was a long tail (i.e. how many of the 3.7M files in this directory--not my fault, by the way--were inconsequential).  That brings us to this wonderful "one-line" command:

find /dir/ -name "*.xml" -exec du -s {} ; | perl -ni -e 'if (/^(d+)s+(.*)/) { $h{$2} = $1; if ($max < $1) { $max = $1; } if (length($2) > $maxfname) { $maxfname = length($2); } } END { map { $barlen = ($h{$_} / $max) * 50; $bar = "*" x $barlen; printf ("%" . $maxfname . "s" . "(%5d): %s", $_, $h{$_}, $bar); print "n"; } sort { $h{$b} <=> $h{$a} } keys %h }' 2> /dev/null > report.txt

What that specifically does is to find every XML file in the dir directory, use the linux du command to get the file's size.  That list of filenames and sizes is passed to a hacky perl script that pulls out the size, creates a horizontal histogram bar based on the max size (limit 50 *s wide), sort and return the list from max to min.  Lastly, that's saved to report.txt.

That's quite a quick and dirty trick, but produces a nice command-line output like this:

/dir/w6bz9whg.xml(36560): **************************************************
/dir/w6km312r.xml(31772): *******************************************
/dir/w68d03gz.xml(27728): *************************************
/dir/w6vt5fhv.xml(27076): *************************************
/dir/w6m07v80.xml(17420): ***********************
/dir/w68m0zj8.xml(15276): ********************
/dir/w6mq7qpz.xml(15052): ********************
/dir/w6vq30tq.xml(13808): ******************
/dir/w6tb51hr.xml(13160): *****************


© 2022 Mininook

Theme by Anders NorenUp ↑