Automatic rename of filenames with bash script
2
0
Entering edit mode
23 months 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 • 1.7k 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.

ADD REPLY
0
Entering edit mode

I wrote

ls -lah "${filename}"

not

ls -lah '${filename}'
ADD REPLY
0
Entering edit mode

I posted only the output of the script.

I copied one to one your new script.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
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.

ADD REPLY
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.

ADD REPLY
1
Entering edit mode
23 months 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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode
23 months ago
fracarb8 ★ 1.6k

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

ADD COMMENT

Login before adding your answer.

Traffic: 2457 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6