Automatic rename of filenames with bash script
2
0
Entering edit mode
5 weeks ago
Morta • 0

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 • 645 views ADD COMMENT 0 Entering edit mode 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  ADD REPLY 0 Entering edit mode [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? ADD REPLY 2 Entering edit mode 1. Do not use root account for normal operations 2. Aur is a package repository for arch linux. What is your distro? ADD REPLY 0 Entering edit mode 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 ? ADD REPLY 1 Entering edit mode 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.

0
Entering edit mode

I wrote

ls -lah "${filename}"  not ls -lah '${filename}'

0
Entering edit mode

I posted only the output of the script.

I copied one to one your new script.

0
Entering edit mode

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

0
Entering edit mode

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.

0
Entering edit mode

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.

1
Entering edit mode
5 weeks ago

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.

0
Entering edit mode

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

0
Entering edit mode
5 weeks ago
fracarb8 ▴ 810

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/'