1

I'm having problems figuring out how to do this bash script since I'm very new to bash.

I want to grab all the files I have in a directory, change their names (I think the changing names part can be done with mv) and move them to another directory (again with mv). Problem is that the files do not have an extension. Their names are simply numbers like: 1, 2, 3 etc. Just the number. How do I grab each extensionless file, change its name and move it?

Thank you.

2
  • How does your script look?
    – zx485
    Commented Nov 9, 2016 at 17:03
  • You say the current names are name1, name2, etc. What do you want to change them to? othername1, othername2? or name101, name102? Need more detail.
    – Rich
    Commented Aug 21, 2017 at 21:02

2 Answers 2

3

You can use extended globbing to search for files with only digits in their names:

shopt -s extglob
for file in +([0-9]) ; do
    newname=newpath/...    # Build the new name.
    mv "$file" "$newname"
done
0

Personally, I'd use basename command to grab filename itself and test it with grep if it has a dot character, which would mean it has extension. That can be used inside if statement, and if it does use continue statement to go to next file. As for moving, that can go after if statement. That way mv command is unreachable unless it doesn't have extension

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .