1

I have a directory named underscore.~1~ and several files inside with names like main.js.~1~, index.js.~1~ etc.

How can I rename the directory underscore and the files inside to the original names main.js, index.js etc.

2

The easiest way to do it (in bash) would be to change into the directory and do something like:

for i in *.~1~; do mv $i ${i%.~1~}; done

The above will iterate over all the *.~1 files and move them to their equivalent without the extension (specified by the ${i%.~1~} target). Look up your shell's textual substitutions.

You can do something similar for directories, but if you only have one directory there, it's simpler to just do it manually

mv underscore.~1~ underscore
1

In a terminal can you not simply move the folder & files using mv command?

Example

$ find adir_.~1~/ -ls
10757186    4 drwxrwxr-x   2 saml     saml         4096 Oct 14 23:54 adir_.~1~/
10370964    0 -rw-rw-r--   1 saml     saml            0 Oct 14 23:50 adir_.~1~/afile_.~1~

Now move the dir + file:

$ mv adir_.~1~/ adir
$ mv adir/afile_.~1~ adir/afile

And confirm:

$ find adir/ -ls
10757186    4 drwxrwxr-x   2 saml     saml         4096 Oct 15 00:01 adir/
10370964    0 -rw-rw-r--   1 saml     saml            0 Oct 14 23:50 adir/afile
1

If you want to rename all files and the directory in one go, you can use the rename command, which at least on Debian-based Linux distributions is included with the perl package:

rename 's/\.~1~$//' */*.~1~ underscore.~1~

The first argument to rename is a Perl Regular Expression, which describes the substitution to be done. Subsequent arguments are file names to operate on. Note that in order to manage with only a single invocation, the directory is specified last to avoid the path to the files within from changing after the argument path expansion takes place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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