<p>I don't think you need to go to R or Python to do that. In bash</p>
<pre><code>set -euxo pipefail
for f in *.bed
do
  bn=&quot;$(basename ${f})&quot;
  on=&quot;${bn%%.bed}_with_header.bed&quot;
  echo 'track type=narrowPeak name=&quot;narrowPeak&quot;' &gt; &quot;${on}&quot;
  cat &quot;${f}&quot; &gt;&gt; &quot;${on}&quot;
done
</code></pre>
<p>EDIT: I just read you want to set the name based on the filename.</p>
<p>EDIT2: And also to add an arbitrary directory</p>
<pre><code>set -euxo pipefail

INDIR=&quot;/path/to/bedfiles&quot;

for f in <span class="math-container">$(find "$</span>{INDIR}&quot; -name &quot;*.bed&quot;)
do
  bn=<span class="math-container">$(basename "$</span>{f}&quot;)
  on=&quot;<span class="math-container">${bn%%.bed}_with_header.bed"
  dn=$</span>(dirname &quot;<span class="math-container">${f}")
  echo "track type=narrowPeak name=\"$</span>{bn}\&quot;&quot; &gt; &quot;<span class="math-container">${dn}"/"$</span>{on}&quot;
  cat &quot;<span class="math-container">${f}" &gt;&gt; "$</span>{dn}&quot;/&quot;${on}&quot;
done
</code></pre>

CC BY-SA 4.0
2020-10-26T12:26:45.913Z
Thanks, never worked in bash before. How can I set the origin directory? In addition, can you please if it's not too complicated to add a short comment for each line so it will be easier for me to google it?
Libby Kosolapov
CC BY-SA 4.0
2020-10-26T12:29:43.753Z
You can change the working directory of your terminal with `cd <dir>`. You can also add the directory to the for-loop `for f in <path_to_dir>/*.bed`
Bastian Schiffthaler
CC BY-SA 4.0
2020-10-26T13:08:02.750Z
I added this line : INDIR=/MacHD/Users/walkerlab/Documents/ChIPSeq”, kept everything else the same and it claims that the folder doesn't  exist            ++ find $'/MacHD/Users/walkerlab/Documents/ChIPSeq?\200\235' -name '*.bed'
find: /MacHD/Users/walkerlab/Documents/ChIPSeq”: No such file or directory
Libby Kosolapov
CC BY-SA 4.0
2020-10-26T13:22:03.350Z
It looks like there is some byte sequence in your path: `ChIPSeq?\200\235`. Can you check for non-ASCII characters there?
Bastian Schiffthaler
CC BY-SA 4.0
2020-10-26T11:31:03.177Z
2020-10-26T12:32:50.613Z
2020-10-26T12:32:50.613Z
Bastian Schiffthaler
<p>I want to create custom tracks from these<a href="https://i.sstatic.net/NqJjt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NqJjt.png" alt="enter image description here" /></a> files</p>
<p>I can add the line : track type=narrowPeak name=“narrowPeak” manually by opening it with text editor:
track type=narrowPeak name=“narrowPeak”
<a href="https://i.sstatic.net/kl8KN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kl8KN.png" alt="enter image description here" /></a>
but I wonder if there is no simple way to do it with code- to open all files that end with bed and add them this line.
Didnt find this option in bed tools, If it is possible to add the name extracting it from the file name it would be amazing.</p>
<p>Many thanks</p>

<python><chip-seq><bed><ucsc>
How can I add for several bed files the header : track type=narrowPeak name=“narrowPeak” preferably in python ,can handle with R
# How can I add for several bed files the header : track type=narrowPeak name=“narrowPeak” preferably in python ,can handle with R

(Asked by: username_0 on 2020-10-26)

I want to create custom tracks from these[![enter image description here](https://i.sstatic.net/NqJjt.png)](https://i.sstatic.net/NqJjt.png) files

I can add the line : track type=narrowPeak name=“narrowPeak” manually by opening it with text editor:
track type=narrowPeak name=“narrowPeak”
[![enter image description here](https://i.sstatic.net/kl8KN.png)](https://i.sstatic.net/kl8KN.png)
but I wonder if there is no simple way to do it with code- to open all files that end with bed and add them this line.
Didnt find this option in bed tools, If it is possible to add the name extracting it from the file name it would be amazing.

Many thanks

**Tags:** python, chip-seq, bed, ucsc

---

## 1 answers

### Answer by username_1 on 2020-10-26. Score: 1

I don't think you need to go to R or Python to do that. In bash
set -euxo pipefail
for f in *.bed
do
  bn="$(basename ${f})"
  on="${bn%%.bed}_with_header.bed"
  echo 'track type=narrowPeak name="narrowPeak"' > "${on}"
  cat "${f}" >> "${on}"
done

EDIT: I just read you want to set the name based on the filename.
EDIT2: And also to add an arbitrary directory
set -euxo pipefail

INDIR="/path/to/bedfiles"

for f in $(find "${INDIR}" -name "*.bed")
do
  bn=$(basename "${f}")
  on="${bn%%.bed}_with_header.bed"
  dn=$(dirname "${f}")
  echo "track type=narrowPeak name=\"${bn}\"" > "${dn}"/"${on}"
  cat "${f}" >> "${dn}"/"${on}"
done

> **Comments:**
> **username_0**: Thanks, never worked in bash before. How can I set the origin directory? In addition, can you please if it's not too complicated to add a short comment for each line so it will be easier for me to google it?  
> **username_1**: You can change the working directory of your terminal with `cd <dir>`. You can also add the directory to the for-loop `for f in <path_to_dir>/*.bed`  
> **username_0**: I added this line : INDIR=/MacHD/Users/walkerlab/Documents/ChIPSeq”, kept everything else the same and it claims that the folder doesn't  exist            ++ find $'/MacHD/Users/walkerlab/Documents/ChIPSeq?\200\235' -name '*.bed' find: /MacHD/Users/walkerlab/Documents/ChIPSeq”: No such file or directory  
> **username_1**: It looks like there is some byte sequence in your path: `ChIPSeq?\200\235`. Can you check for non-ASCII characters there?  

---

