<p>You can use the <a href="https://www.nextflow.io/docs/latest/script.html#check-file-attributes" rel="nofollow noreferrer"><code>getBaseName()</code></a> method on <code>file</code> objects to get the basename:</p>
<blockquote>
<p>The invocation of any method name starting with the <code>get</code> prefix can be shortcut by omitting the <code>get</code> prefix and <code>()</code> parentheses. Therefore, writing <code>myFile.getName()</code> is exactly the same as <code>myFile.name</code> and <code>myFile.getBaseName()</code> is the same as <code>myFile.baseName</code> and so on.</p>
</blockquote>
<p>For example:</p>
<pre><code>process GTF2BED {

    input:
    path gtf

    output:
    path &quot;${gtf.baseName}.bed&quot;

    script:
    &quot;&quot;&quot;
    gtf2bed --gtf &quot;<span class="math-container">${gtf}" --bed "$</span>{gtf.baseName}.bed&quot;
    &quot;&quot;&quot;
}
</code></pre>
<p>If you need to remove a double extension (.e.g. <code>.tar.gz</code>) and don't want to remove all extensions (e.g. using the <code>getSimpleName()</code> method), you can use the long form to supply the number of extensions to remove. For example:</p>
<pre><code>tarball = file( 'my.file.tar.gz' )

println( tarball.getBaseName(2) )
</code></pre>
<p>Prints:</p>
<pre><code>my.file
</code></pre>

CC BY-SA 4.0
2022-12-14T11:33:16.280Z
2022-12-14T11:58:07.167Z
2022-12-14T11:58:07.167Z
Steve
<p>The following Nextflow scripts takes the $gtf as an input and outputs a .bed file. What I want is to keep just the basename of the gtf file and add the .bed extension to it. I tried declaring a variable x and using basename from bash but it gives error.</p>
<pre><code>// Declare syntax version
nextflow.enable.dsl=2

/* 
 * pipeline input parameters 
 */

params.gtf_file = &quot;$projectDir/data/Glycine_max.gtf&quot;


    /*
 * define the `GTF2BED` process that creates a BED file
 * given the params.gtf_file as input. 
 */
 
process GTF2BED {

    input:
    path gtf

    output:
    &quot;${gtf}/.bed&quot;

    script:
    &quot;&quot;&quot;
    gtf2bed --gtf <span class="math-container">$gtf --bed "$</span>{gtf}/.bed&quot;
    &quot;&quot;&quot;
}
</code></pre>
<p>Best wishes.</p>

<nextflow>
How to keep the basename of an input file and change its extension in Nextflow?
# How to keep the basename of an input file and change its extension in Nextflow?

(Asked by: username_0 on 2022-12-14)

The following Nextflow scripts takes the $gtf as an input and outputs a .bed file. What I want is to keep just the basename of the gtf file and add the .bed extension to it. I tried declaring a variable x and using basename from bash but it gives error.
// Declare syntax version
nextflow.enable.dsl=2

/* 
 * pipeline input parameters 
 */

params.gtf_file = "$projectDir/data/Glycine_max.gtf"


    /*
 * define the `GTF2BED` process that creates a BED file
 * given the params.gtf_file as input. 
 */
 
process GTF2BED {

    input:
    path gtf

    output:
    "${gtf}/.bed"

    script:
    """
    gtf2bed --gtf $gtf --bed "${gtf}/.bed"
    """
}

Best wishes.

**Tags:** nextflow

---

## 1 answers

### Answer by username_1 on 2022-12-14. Score: 3 | Accepted answer

You can use the getBaseName() method on file objects to get the basename:

The invocation of any method name starting with the get prefix can be shortcut by omitting the get prefix and () parentheses. Therefore, writing myFile.getName() is exactly the same as myFile.name and myFile.getBaseName() is the same as myFile.baseName and so on.

For example:
process GTF2BED {

    input:
    path gtf

    output:
    path "${gtf.baseName}.bed"

    script:
    """
    gtf2bed --gtf "${gtf}" --bed "${gtf.baseName}.bed"
    """
}

If you need to remove a double extension (.e.g. .tar.gz) and don't want to remove all extensions (e.g. using the getSimpleName() method), you can use the long form to supply the number of extensions to remove. For example:
tarball = file( 'my.file.tar.gz' )

println( tarball.getBaseName(2) )

Prints:
my.file


---

