Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip first part of filename with awk statement 1

Status
Not open for further replies.

mrimagepueblo

Programmer
Dec 15, 2003
52
US
I need to write a bash shell script to strip the first part of the filename in a directory of files.
The original filenames are
05-12345_1.jpg
05-12345_2.jpg
05-12345_3.jpg
06-56789_1.jpg
06-56789_2.jpg
06-56789_3.jpg
06-56789_4.jpg
etc.
I simply want to strip out the 05- or 06- whatever happens to be in the front, so the end results are:
12345_1.jpg
12345_2.jpg
12345_3.jpg
56789_1.jpg
56789_2.jpg
56789_3.jpg
56789_4.jpg

An awk statement would be preferred but I'm not really picky at this point.

 
A starting point:
ls /path/to/dir | awk '/^..-.*\.jpg$/{print substr($0,4)}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
That command echos to the console fine but the files are not renamed when it's complete?
 
Where did you say that you wanted to rename the files ?
cd /path/to/dir && ls ??-*.jpg | awk '{print mv" "$1" "substr($1,4)}' | sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The same directory is fine if that is possible. If not I could move them to a temp directory /path/to/dir/temp.
 
I did, I get a command not found.
I was even in the directory and just ran
ls ??-*.jpg | awk '{print mv" "$1" "substr($1,4)}' | sh
and I get the command not found???
 
OOps, sorry for the typo :~/
ls ??-*.jpg | awk '{print "mv "$1" "substr($1,4)}' | sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top