ASIC/FPGA Design and Verification Out Source Services
rename files easily from the command line
The following tip shows how to rename files easily from the command line.
I have copied some JPEG files from WINDOWS and than I noticed that all files end with the following suffix: jpg.jpg.
To remove it (file.jpg.jpg --> file.jpg) I just used:
rename 's/\.jpg$//' *.jpg.jpg
Another way to rename multiple files, if your system does not have rename, is by using the command basename in a for f loop in a single line command:
for f in *; do mv $f "$(basename "$f" txt)tmp"; done
The following shows how to rename files of the following format: 1.mp3, 2.mp3, ... to D1_date.mp3, D2_date.mp3, ....
#!/bin/bash
numb=`echo $1 | sed 's/.*\([0-9]\)\.mp3/\1/'`
date=`date +%y%m%d_%H%M%S`
name=D${numb}_$date.mp3
sleep 2
mv -f $1 $name
Note the sleep command is required to make sure we get different values from the date command output.
One way to run this script isls ../../*.mp3 | xargs -n1 | xargs -i= ./mp3_name.unx =
When I download files from my phone, there are some Hebrew characters, which I like to remove. The following simple perl script can do the job.
The perl script is started per each file using bash's for f style, so the perl script itself is simple and handles one file at a time.
First it extracts the file name, using split command. Next only allowed text is allowed to pass.
Last the command for file rename is build and executed.
#!/usr/bin/perl
#for f in `ls *.3gp`;do perl /home/pini/bin/rm_heb_letters.pl $f;done
@a=split(/\./, $ARGV[0]);
my $ok_chars = 'a-zA-Z0-9_';
$cmd="mv " . $ARGV[0];
$a[0] =~ s/[^$ok_chars]//go;
$cmd=$cmd . " RedInSouth_" . $a[0] . "." . $a[1];
print("$cmd\n");
system($cmd);
|