3

I'm using a mac. I need to get a list of files in a folder without their extensions being included in the list. This is because I need a list of products for a website import and conveniently, the image names are the same as the product names. However, my list needs to NOT have the extensions.

I've gotten a list of file names by using terminal and was able to copy them into a text file. Only problem is that the file names include the extension, ".jpg" Using the list I have from terminal, I will now have to go through my text file and manually delete the extension from each file name (there are over a thousand) and this is going to take a long time. Is there an easier way? Can I get terminal to display a list of file names without their extensions so I can copy and paste it into a list? Or can I remove the extensions from these files, then list them in terminal and copy their names? Is there another way? Confused here. I'm not a programmer, just a website design.

Hope someone can help with this. Thanks everyone!

3 Answers 3

3

Borrowed from Stackoverflow:

ls -1 | sed -e 's/\..*$//'

I tried flagging as duplicate but didn't work.

One of my own solutions given we know the file extension(s).

find -type f -exec basename -s '.jpg' "{}" \;

So find all files as opposed to directories - type f and then execute basename for each. The -s allows you to specify a known suffix to remove.

Another alternate solution: from SU

2
  • 2
    As the comments on the answer you linked to point out, this removes the first . (and everything to the right of it), so Mr.Coffee_machine.jpg becomes Mr.  That might be good enough for this OP's needs, but it's a dangerous assumption to make, given that the question doesn't have any example file names, and it's a poor solution to the problem of "removing filename extensions". May 1, 2015 at 21:50
  • Good point I don't know what the file names are.
    – fswings
    May 1, 2015 at 21:55
1

If you have your list of files in the file "in.txt", run this script to get the list of files without the extension in "out.txt" :

#!/bin/bash    
while read fullfile; do
    filename=$(basename "$fullfile")
    filename="${filename%.*}"
    echo $filename >> out.txt
done <in.txt
1

Do you use vi (or vim)?  If you do, then, since you've already got the JPEG file names in a text file, the easiest way may be to edit that file with vi / vim and type

:%s/\.jpg$//

which says, on every line in the file, look for .jpg occurring at the end of a line, remove it, and replace it with nothing.  If there are some lines that don't end with .jpg, that's OK; they will be ignored (skipped over).  If you've got multiple product names (file names) per line, as in

        hammer.jpg              screwdriver.jpg         wrench.jpg

just leave out the $ and add g and say

:%s/\.jpg//g

which will remove all occurrences of .jpg, even if they aren't at the end of a line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .