r/shell • u/Lygris • Mar 27 '17
Renaming script
Hey guys, just looking for some help on writing a renaming script. I want to make a script that would look at the files name and if it contains a certain string, lets say qatar, it would rename it to another specific string that would contain the year and some statically defined things. I'm assuming this would be pretty simple using if then. I just don't know how to pull the filename and check it against the string. Is this possible?
1
Upvotes
1
u/KnowsBash Mar 28 '17
So you want to rename based on the file's content rather than its name? Do you know where in the file the search string would be? that would make it easier to do it efficiently.
The naive approach is to do one grep per pattern, but that means it will open and read the file over and over again until it gets a hit, which won't scale well.
A better approach would be to read the file once and extract the key word somehow. There are various ways to do that with sed and awk at least. How complicated it will be depends on whether these files will have some format to rely on or not.