Reading the referers of this website, I found that someone reached it querying a search engine for
awk remove file extension
Well, it’s definitely easy to achieve it if your files have just a “.” in their names. You can use this one-liner that works inverting the problem ![]()
# substitute jpg with the extension you need (twice in the line)
for i in $(ls *jpg | awk -F "." '{print $1}'); do mv $i.jpg $i; done
Last update: 2008-04-12
If you enjoyed this post, make sure you subscribe to my RSS feed!
*cough*
rename ’s/\.jpg$//’ *.jpg
Good answer but the original problem was about using AWK
I know, Carlo, and your code is nice.
But to me, the correct answer is “don’t”. Use the appropriate tool, it’s often a good suggestion.
(of course, this is for the original “researcher”… :))
any idea how to make your example work on all subfolders within a directory also? i tried ls -R but it won’t work.
Random Guy:
try with
find folder_name -name *jpg
this is great - i don’t think “tannoiser” understands how an example should illustrate a concept with an easy example so that you can modify it to real needs. In particular, I use a compiler that compiles xyz.sd into xyz.sb. So on a directory of 1000 .sd’s the easiest way to find the 3 that don’t compile is to list all of the .sds and .sbs, remove the extensions and do uniq on the list to see which have only 1 entry (not 2). And AWK is a GREAT tool to do this
Booger:
Good
A solution to your problem could be:
l *.sd *.sb -1 | awk -F "." '{print $1}' | uniq -c | awk '$1=="1" {print $2}'Ahh, but what if you have files like “file.txt.old”
Then you are somewhat stuffed, as the extention gets defined to be .txt.old, rather than .old?
@anonymous:
this is the reason I wrote
Anyway, your problem could be solved with
for i in $(ls *.txt.old | awk -F "." '{print $1}'); do mv $i.txt.old $i.txt; done