Solved

Mac Terminal or Automater command to remove file extension from files in folder

Posted on 2013-11-11
7
707 Views
Last Modified: 2013-11-11
Hi Experts

I am on a Mac Computer.  I have a folder with a lot of text files with an extension of:  .txt.imp

Is there a terminal command or a method on how I can quickly remove the .imp    and/or  remove the .txt.imp  from the tail end of the file?

Example
Please do not suggest me to download any software.  I would like a command, script, or automater action to do this.  Thanks!  (Preferably a terminal command would be awesome).
0
Comment
Question by:activematx
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39640757
you can run command like

rm *.txt.imp

OR

rm *.imp

You need to be careful since these files will be removed and can't be recovered.
0
 
LVL 2

Expert Comment

by:goubun
ID: 39640770
You also can run

rm -rf *.txt.imp

to remove the files without prompting for confirmation, it's so useful when there are a lot of files
0
 
LVL 9

Author Comment

by:activematx
ID: 39640771
I am sorry, let me clarify.  I am trying to rename the files and not remove them.  I would like to rename them by removing the extension(s).
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 2

Expert Comment

by:goubun
ID: 39640781
You can use this

find . -iname "*.txt.imp" -exec bash -c 'mv "$0" "${0%\.txt.imp}"' {} \;

to remove the extension of all files that end with txt.imp, if you need remove the extension txt.imp and add another extension like .php you can use

find . -iname "*.txt.imp" -exec bash -c 'mv "$0" "${0%\.txt.imp}.php"' {} \;
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 39640784
you can run

for file in *.txt.imp
  do
    f=`awk -F'.' '{ print $1 }'`
    mv $file $f
done
0
 
LVL 9

Author Closing Comment

by:activematx
ID: 39640800
Thanks, you got me on the right track.  I ended up using:

for f in *.txt.imp; do
  mv "$f" "${f%.*}"
done

Any idea on why yours would not quit in the terminal.  (would not proccess done)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 39640847
Ok, I missed this:

  f=`awk -F'.' '{ print $1 }'`

It should be

  f=`echo $file | awk -F'.' '{ print $1 }'`
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question