user3672247
user3672247

Reputation: 11

Using first 4 characters of file name to create unique list

I primarily use Linux Mint 17.1 and I like to use command line to get things done. At the moment, I am working on organising a whole lot of family pictures and making them easy to view via a browser. I have a directory with lots of images. As I was filling the directory I made sure to keep the first four letters of the filename unique to a specific topic, eg, car_, hse_, chl_ etc The rest of the filename keeps it unique. There are some 120 different prefixes and I would like to create a list of the unique prefix. I have tried 'ls i | uniq -d -w 4' and it works but it gives me the first filename of each prefix. I just want the prefixes. Fyi, I will use this list to generate an HTML page as a kind of catalogue. Summary, Convert car_001,car_002,car_003,dog_001,dog_002 to car_,dog_

Upvotes: 1

Views: 2130

Answers (2)

karakfa
karakfa

Reputation: 67507

try this

$ ls -1 | cut -c1-3 | sort -u

uses the first 3 chars of the file names.

Upvotes: 3

TessellatingHeckler
TessellatingHeckler

Reputation: 28993

Try something like

ls -1 | cut -d'_' -f1 | uniq | sort

where cut splits the text by _ and takes the first field of each.

Upvotes: 1

Related Questions