<p>It is simpler to use <code>cut</code> command to remove filename extensions:</p>
<pre><code>ls -1 | cut -d '.' -f1 | sort | uniq
</code></pre>

CC BY-SA 4.0
2022-07-19T19:05:25.467Z
This seems nice and clean, but it will also pick up any directory names. I only want to extract from file names.
Lou_A
CC BY-SA 4.0
2022-07-21T07:20:14.140Z
`ls -1 -p | grep -v '/'` could filter directories. The `-p` flag appends / indicator to directories. Then the `-v` flag only matches files.
Forrest Vigor
CC BY-SA 4.0
2022-06-16T11:08:40.833Z
2022-06-16T11:08:40.833Z
Forrest Vigor
<pre><code>ls | perl -p -n -e  's/(.*)(\..*){1,3}<span class="math-container">$/$</span>1/' | uniq
</code></pre>
<p>or</p>
<pre><code>find . -name &quot;file*&quot; | perl -p -n -e  's/(.*)(\..*){1,3}<span class="math-container">$/$</span>1/' | uniq
</code></pre>
<p><code>find</code> <em>per se</em> is a really powerful command, but its a while since I've used it full functionality.</p>

CC BY-SA 4.0
2022-06-02T14:29:32.923Z
Thanks, M__. I'll test it.  I notice you call perl.  I'm hoping to find a way to run it entirely in shell, but this could work too.
Lou_A
CC BY-SA 4.0
2022-06-02T18:13:08.093Z
Bash has a regex, so it can be rewritten, but dunno if it has the {1,3} syntax
M__
CC BY-SA 4.0
2022-06-01T22:32:37.123Z
2022-06-01T22:37:50.433Z
2022-06-01T22:37:50.433Z
M__
<p>Use <a href="https://www.gnu.org/software/findutils/manual/html_mono/find.html" rel="nofollow noreferrer"><code>find</code></a>, remove all extensions with <code>perl</code>, and make unique with <a href="https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html" rel="nofollow noreferrer"><code>sort</code></a>:</p>
<pre><code>find . -type f -printf '%P\n' | perl -pe 's{[.].*}{}' | sort -u
</code></pre>
<p>The Perl one-liner uses these command line flags:<br />
<code>-e</code> : Tells Perl to look for code in-line, instead of in a file.<br />
<code>-p</code> : Loop over the input one line at a time, assigning it to <code>$_</code> by default. Add <code>print $_</code> after each loop iteration.</p>
<p><code>s{[.].*}{}</code> : replace <code>[.]</code> (a literal dot), followed by <code>.*</code> (any character repeated 0 or more times) with en empty string.</p>
<p><strong>SEE ALSO:</strong><br />
<a href="https://perldoc.perl.org/perlrun.html#Command-Switches" rel="nofollow noreferrer"><code>perldoc perlrun</code>: how to execute the Perl interpreter: command line switches</a><br />
<a href="https://perldoc.perl.org/perlre.html" rel="nofollow noreferrer"><code>perldoc perlre</code>: Perl regular expressions (regexes)</a><br />
<a href="https://perldoc.perl.org/perlre.html#Regular-Expressions" rel="nofollow noreferrer"><code>perldoc perlre</code>: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups</a><br />
<a href="https://perldoc.perl.org/perlrequick" rel="nofollow noreferrer"><code>perldoc perlrequick</code>: Perl regular expressions quick start</a></p>

CC BY-SA 4.0
2022-06-03T17:43:55.383Z
2022-06-03T17:43:55.383Z
Timur Shtatland
<p>I need to generate a list of unique file names without their extensions or using the shell script command &quot;find&quot;</p>
<p>Say I have a list such as:</p>
<pre><code>file2022_col.R1.twx.gz
file2022_col.R2.twx.gz
file2022_col.bwa.bam
file2332_col.R1.twx.gz
file2332_col.bwa.bam
file3442_col.bwa.bam.bai
</code></pre>
<p>I want a list of unique file names such as:</p>
<pre><code>file2022_col
file2332_col
file3442_col
</code></pre>
<p>I'm trying to use <code>find -type f -name</code> but I'm not sure how to construct the option to restrict the find command.</p>

<linux><shell>
How do I find unique file names without their extensions in shell script?
# How do I find unique file names without their extensions in shell script?

(Asked by: username_0 on 2022-06-01)

I need to generate a list of unique file names without their extensions or using the shell script command "find"
Say I have a list such as:
file2022_col.R1.twx.gz
file2022_col.R2.twx.gz
file2022_col.bwa.bam
file2332_col.R1.twx.gz
file2332_col.bwa.bam
file3442_col.bwa.bam.bai

I want a list of unique file names such as:
file2022_col
file2332_col
file3442_col

I'm trying to use find -type f -name but I'm not sure how to construct the option to restrict the find command.

**Tags:** linux, shell

---

## 3 answers

### Answer by username_1 on 2022-06-16. Score: 2 | Accepted answer

It is simpler to use cut command to remove filename extensions:
ls -1 | cut -d '.' -f1 | sort | uniq

> **Comments:**
> **username_0**: This seems nice and clean, but it will also pick up any directory names. I only want to extract from file names.  
> **username_1**: `ls -1 -p | grep -v '/'` could filter directories. The `-p` flag appends / indicator to directories. Then the `-v` flag only matches files.  

---

### Answer by username_2 on 2022-06-01. Score: 1

ls | perl -p -n -e  's/(.*)(\..*){1,3}$/$1/' | uniq

or
find . -name "file*" | perl -p -n -e  's/(.*)(\..*){1,3}$/$1/' | uniq

find per se is a really powerful command, but its a while since I've used it full functionality.

> **Comments:**
> **username_0**: Thanks, M__. I'll test it.  I notice you call perl.  I'm hoping to find a way to run it entirely in shell, but this could work too.  
> **username_2**: Bash has a regex, so it can be rewritten, but dunno if it has the {1,3} syntax  

---

### Answer by username_3 on 2022-06-03. Score: 1

Use find, remove all extensions with perl, and make unique with sort:
find . -type f -printf '%P\n' | perl -pe 's{[.].*}{}' | sort -u

The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
s{[.].*}{} : replace [.] (a literal dot), followed by .* (any character repeated 0 or more times) with en empty string.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
perldoc perlre: Perl regular expressions (regexes)
perldoc perlre: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
perldoc perlrequick: Perl regular expressions quick start


---

