Score:-1

How to change file names in Shell Script?

it flag

I have a task to rename a bunch of files to their content at row1 colum2 (a string). The files do not have extensions. I also have to add .pdb as an extension to every file.

so far I've written this and it does not work :(

#! /bin/bash
for f in sequences/*; do
mv "$f" "($ awk 'NR==1 {print=$2}').pdb";
done

I am very very new to Shell script and still have no idea how to use it. Any help would be appreciated!

Someone avatar
my flag
Looks like a homework problem
it flag
Yes it is homework though the lecturer did nor provide any examples or material how to even select files, use awk, or pretty much do anything :)
Someone avatar
my flag
It doesn't look like a problem releated to Ubuntu (maybe you are running ubuntu but this is not a problem with Ubuntu but it is a problem with bash scripting) , I don't think that this will be well-answered on this website, So post your question on [Stack overflow](https://stackoverflow.com/) (BTW this question is on topic)
it flag
Ah okay thank you!!
Liso avatar
sd flag
Is this university problem or hs problem? Just asking..
Will avatar
id flag
I know this is opinion and not shared by everyone … but what’s wrong with a homework question when the OP has gone to some effort to solve it? I don’t want to do people’s homework for them, but for me it’s ok to help if they’ve tried.
it flag
That's a university problem.
Score:1
hr flag

A few things to note:

  1. the syntax for command substitution is $( command ) not ($ command )

  2. the syntax for printing the second field in awk is print $2 not print=$2

  3. you need to pass the name of the file as an argument to awk ex. awk 'NR==1 {print $2}' "$f"

Also note that the new file will be created relative to the current directory, rather than the subdirectory sequences (there's not enough detail in your question to know whether that's the intended outcome or not).

Score:0
it flag

Thank you steeldriver! The remarks helped a lot! The 3rd one was the real problem solver! So here's the script I wrote in the end:

#! /bin/bash
for f in $(ls); do
mv "$f" "$( awk 'NR==1 {print $2}' "$f" ).pdb";
done

If anyone has any 'smarter' ways of doing it I would be very happy to hear them.

Thanks!

Pilot6 avatar
cn flag
`f in $(ls)` makes no sense ;-)
it flag
It might not make sense but it did the job and that's the best I can do currently. If you have smarter ways, you are welcome to enlighten the inferior being with your large brain ;-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.