This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Automatic rename of filenames with bash script

Hi i want to rename all files with a 1 in a directory where the script is

#!/bin/bash
for filename in *.fasta; do 
     [ -f "$filename" ] || continue
     mv "$filename" "${filename//1/}"
done

But when I'm in /tmp and the execute filename.sh the filenames have still a 1 innit

Why?

rename bash script

run this version; show us the output:

#!/bin/bash
set -x
for filename in *.fasta; do
     ls -lah "${filename}"
     [ -f "${filename}" ] || continue
     mv -v "${filename}" "${filename//1/}"
done

[root@lapt0p tmp]# bash filename.sh

 for filename in *.fasta
 ls -lah '*.fasta'
 ls: Zugriff auf '*.fasta' nicht möglich: Datei oder Verzeichnis nicht gefunden
 '[' -f '*.fasta' ']'
 continue

Seems not to know the command fasta. How to fix it?

I can install a AUR package named fasta?! Is this command not part of the basic commands from bash?

  1. Do not use root account for normal operations
  2. Aur is a package repository for arch linux.

What is your distro?

  1. Ok. I know is risky!
  2. Yes

I replaced .fasta with .txt now it's editing all files with ending *.txt. I wasn't clear that the coder of this script want only modify fasta files.

How I do it if i want modifiy all files in a folder? A . instead of .txt or a simple ``?

How I do it if i want modifiy all files in a folder? A . instead of .txt or a simple ``?

instead of for filename in *.fasta, you can use for i in * or for i in $(ls *). You can also use find.

I wrote

ls -lah "${filename}"

not

ls -lah '${filename}'

I posted only the output of the script.

I copied one to one your new script.

what are you trying to do? You do not need a loop here. Use rename.

I did with rename but I was curious about how I do with this bashscript. I try to understand the error of the script for what I want to do.

can you try this?

#!/bin/bash
for filename in *.fasta; do 
     [ -f "$filename" ] || continue
     mv "$filename" "${filename/1/}"
done

But this would remove all '1's in file names.

2 answers

ah yes sorry, that was the output , not my script. OK the message in german is clear. There is no file with the suffix *.fasta in the directory.

Ok. Thanks is now working with txt files with the word .txt instead of .fasta!

You can use rename. Depending on the version installed one of the commands below should do:

rename "1" "" *.fasta
rename "s/1//" *.fasta

The structure of the command is rename 's/search/replace/'

Log in to answer this question.