Answers.Body: <p>Try re-indexing your bam file:</p>
  <pre><code>samtools index input.bam
  </code></pre>
  <p>Or update the timestamp via <code>touch index.bam.bai</code> but I think re-indexing is safer. You might want to save any original index file first, e.g. via <code>mv input.bam.bai old_input.bam.bai</code> or similar</p>
  <p>Also I think you meant <code>&gt; chr21.bam</code> instead of piping via <code>| chr21.bam</code> if you want to save the output file</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-28T19:09:53.387Z
Answers.Text: Cool! I think the problem is because I use | chr21.bam.
Answers.UserDisplayName: Scott XU
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-28T19:05:58.300Z
Answers.LastActivityDate: 2022-02-28T19:05:58.300Z
Answers.UserDisplayName: Chris_Rands
Body: <p>When I tried to use samtools to split a bam file based on different chromosomes, I used this command:</p>
  <pre><code>samtools view input.bam -b chr21 | chr21.bam
  </code></pre>
  <p>However, I get error messages like this:</p>
  <pre><code>-bash: chr21.bam: command not found
  [W::hts_idx_load3] The index file is older than the data file: input.bam.bai
  </code></pre>
  <p>How could I solve this problem? Thanks in advance~~~</p>
  <p>The version I am using is:</p>
  <pre><code>Program: samtools (Tools for alignments in the SAM format)
  Version: 1.10 (using htslib 1.10.2)
  </code></pre>
  
Tags: <sam><samtools>
Title: samtools view command not found error
ThreadText: # samtools view command not found error
  
  (Asked by: username_0 on 2022-02-28)
  
  When I tried to use samtools to split a bam file based on different chromosomes, I used this command:
  
  ```
  samtools view input.bam -b chr21 | chr21.bam
  
  ```
  
  However, I get error messages like this:
  
  ```
  -bash: chr21.bam: command not found
  [W::hts_idx_load3] The index file is older than the data file: input.bam.bai
  
  ```
  
  How could I solve this problem? Thanks in advance~~~
  
  The version I am using is:
  
  ```
  Program: samtools (Tools for alignments in the SAM format)
  Version: 1.10 (using htslib 1.10.2)
  
  ```
  
  **Tags:** sam, samtools
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-02-28. Score: 5 | Accepted answer
  
  Try re-indexing your bam file:
  
  ```
  samtools index input.bam
  
  ```
  
  Or update the timestamp via `touch index.bam.bai` but I think re-indexing is safer. You might want to save any original index file first, e.g. via `mv input.bam.bai old_input.bam.bai` or similar
  
  Also I think you meant `> chr21.bam` instead of piping via `| chr21.bam` if you want to save the output file
  
  > **Comments:**
  > **username_0**: Cool! I think the problem is because I use | chr21.bam.  
  
  ---
  
  