0

I would like to rename my file from myscript.js to .myscript.js. How can I do it with bash? I've tried different options like mv myscript.js \.*, but it doesn't work. I've tried to experiment by adding non-dot symbol like - mv myscript.js m*, but now I don't even know where my file is (of course, I have a copy ;).

4 Answers 4

3

You're trying too hard.

mv myscript.js .myscript.js

3

Brace expansion

mv {,.}myscript.js
2

You can use rename utility to rename all *.js file by placing a dot before them:

rename 's/(.+\.js)/.$1/' *.js

Or in pure BASH use this for loop:

for i in *.js; do mv "$i" ".$i"; done
1

Don't use patterns as the target of mv (or cp for that matter). The command won't do what you want or expect, most of the time: Bash will expand the pattern at the moment when you run the command, using the file names as they were before your command was executed. So .* matches nothing (since the file doesn't exist, yet) and m* will match any file or folder which starts with m. Since you didn't get an error, chances are that the last match was a folder.

There is no way to access the previous parameter of the current command. If you want to avoid typing too much, then use Tab for both file names (so you end up with mv myscript.js myscript.js) and then use Ctrl+Left and Ctrl+Right to move quickly to the start of the second argument to insert the missing ..

You can access the parameters of the previous command, though. But that's not a feature of BASH - it's a feature of readline.

Not the answer you're looking for? Browse other questions tagged or ask your own question.