1

I'm using Elementary OS (still very new to any UNIX system) and have hundreds of text files on my old hard drive that I need to sort. All text files have a name such as;

lua - eng1-6TH4eI8gtf3.txt

I need a command that can remove the last 11 characters from all of them, but still leaving the extension. Looking around I saw the "rename" command, but I'm not sure if that would work for me.

A command that i could just copy, paste and then change anything like a directory would be amazing, and an explanation of what everything does.

  • Do this command: mv filename1.txt filename.txt. This will basically move the file and rename it at the same time. – Kevdog777 Mar 24 '16 at 8:42
3

This command should work well for you as I tested it on some sample files with same pattern as yours:

rename 's/(.*)(-.{11})\.txt$/$1\.txt/' *

Explanation:

See this simple command:

rename 's/pattern/result/' filename(s)

Pattern can be regex. So, we can take different part of the text (which is the filename). If you don't understand it, google for regex. It looks complex but if you learn it, it is easy.

In result, I take the content of first parenthesis by $1 and leave the second parenthesis and at the end, add .txt to it.

Importnat Notice
First run the command with -n -v option to just see what the command would do. If the result is what you want, run it without options. So first run it like this:

$ rename -n -v 's/(.*)(-.{11})\.txt$/$1\.txt/' *
rename(lua - eng1-6TH4eI8gtf3.txt, lua - eng1.txt)
rename(lua - eng2-6TH4eI8gtf4.txt, lua - eng2.txt)
rename(lua - eng4-6TH4eI8gtf5.txt, lua - eng4.txt)
  • Do I need to do cd 'Hard Drive' for this, or just anywhere? – Zachary Wight Mar 24 '16 at 11:39
  • If your files are in the same folder, go to that folder using cd. But if they are in different folders, let me know the structure – Mostafa Ahangarha Mar 24 '16 at 11:47

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.