4

I have a directory named "test" containing data files saved during a simulation at different times. The names of the files are like 2.01149, 2.1149, 2.35698, 2.545, 2.95874566..... and many more etc.

I want to have the time files with only 4 digits after the decimal point or I would like to have the file names rounded-off to a nearest integer such as 2.0115, 2.1149, 2.3670, 2.5450, 2.9588... etc. Can anyone help me how to do this?

I am new to ubuntu. May I request anyone to explain how to execute on command line, a bit clearly like where to keep directory name, etc. Thanks for understanding!!!

Query after obtaining the solution to the above

Hi, the command prename -n 's/\d\.\d+/sprintf "%.4f", $&/e' *([0-9.]) helped rename my time files.

But, I am facing an error while trying to open paraview.

The terminal says: FOAM Warning :

From function void Foam::Time::setControls()
in file db/Time/Time.C at line 307

Reading "/Re600/2.498/uniform/time" from line 15 to line 23
Time read from time dictionary 2.497956694 differs from actual time 2.498.
This may cause unexpected database behaviour. If you are not interested in preserving time state delete the time dictionary.   

After opening the paraview, some field values like vorticity, Lambda2 are not displayed.

How to rectify this?

  • If you have a new question, make a new post. Don't drastically change the post and make existing valid answers invalid. – muru Jun 14 '17 at 6:40
2

With perl-based prename and an extended glob to select the files:

$ prename -n 's/\d\.\d+/sprintf "%.4f", $&/e' *([0-9.])
2.01149 renamed as 2.0115
2.35698 renamed as 2.3570
2.545 renamed as 2.5450
2.95874566 renamed as 2.9587

(remove the -n once you are satisfied that it is doing the right thing).

Or - equivalently - with bash builtins and mv

$ for f in *([0-9.]); do printf -v g '%.4f' "$f"; echo mv -- "$f" "$g"; done
mv -- 2.01149 2.0115
mv -- 2.1149 2.1149
mv -- 2.35698 2.3570
mv -- 2.545 2.5450
mv -- 2.95874566 2.9587

(remove the echo to actually do it).

| improve this answer | |
  • Hello, I am new to ubuntu. My directory name is test. It contains many files such as 2.01149, 2.35698 etc. May I request you to explain how to execute your suggestions on command line, a bit clearly. – vidyadhar Jun 8 '17 at 14:54
  • Thank you. It helped me really, when "-n" is not used. Sorry for the earlier comment, I have done a silly mistake while using your suggestion. – vidyadhar Jun 8 '17 at 16:13
2

There are probably other ways to do it, but Perl is the one that comes to me most easily. To do so, go into the directory where the files are located and run the command perl -E 'rename $_, sprintf(q(%0.4f), $_) for @ARGV' *.

The following example demonstrates, by using touch to create files with the names specified in the question, runs the command to rename them, and then uses ls to show that they have been renamed as specified:

$ touch 2.01149, 2.1149 2.35698 2.545 2.95874566
$ perl -E 'rename $_, sprintf(q(%0.4f), $_) for @ARGV' *
$ ls
2.0115  2.1149  2.3570  2.5450  2.9587
| improve this answer | |
  • Hello, I am new to ubuntu. My directory name is test. It contains many files such as 2.01149, 2.35698 etc. May I request you to explain how to execute your suggestions on command line, a bit clearly. – vidyadhar Jun 8 '17 at 14:54
  • Hi, I could work out the way you have suggested. But, I have so many files. How to select all of them as mentioned in the first line $ touch 2.01149, 2.1149 2.35698 2.545 2.95874566 – vidyadhar Jun 8 '17 at 16:09
  • @vidyadhar - Edited to explain more clearly. You don't need to run the touch line, that was just to create dummy files to demonstrate with; sorry that was unclear. The perl -E... command is the only one you actually need to run. – Dave Sherohman Jun 9 '17 at 7:30
0

If you have the Perl rename executable, this will do it:

rename -n 's/(\d+\.\d{4})\d*/$1/' *

Remove the -n once you have verified it does what you want.

Note this only truncates the filenames. If you want rounding up, more work would be needed.

| improve this answer | |
  • Hello, I am new to ubuntu. My directory name is test. It contains many files such as 2.01149, 2.35698 etc. May I request you to explain how to execute your suggestions on command line, a bit clearly. – vidyadhar Jun 8 '17 at 14:55
  • Same answer as Sergiy gave. cd test then copy and paste exactly the command I gave. – Joe P Jun 8 '17 at 15:06
  • In console windows, you may need Ctrl-Shift-V to paste - or use menus. – Joe P Jun 8 '17 at 15:07
0

find + while + printf

bash-4.3$ find -type f -name "[1-9].[1-9]*" -printf "%P\0" | while IFS= read -r -d '' file;  do      echo "$file"  "$(printf "%.4f" "$file")"; done
2.1149 2.1149
2.35698 2.3570
2.545 2.5450
2.95874566 2.9587

Replace echo with mv for actual renaming


bash for loop and printf`:

bash-4.3$ for f in *;do new=$(printf "%.4f" "$f"); echo "$f" "->" "$new"; done
2.01149 -> 2.0115
2.1149 -> 2.1149
2.35698 -> 2.3570
2.545 -> 2.5450
2.95874566 -> 2.9587

Replace echo with mv and remove "->" part for actual replacement.


Python

bash-4.3$ ls
2.01149  2.1149  2.35698  2.545  2.95874566
bash-4.3$ python -c 'import sys,os;f=map(lambda x: (x,"%.4f" % float(x)),sys.argv[1:]);[os.rename(i[0],i[1]) for i in f]' *
bash-4.3$ ls
2.0115  2.1149  2.3570  2.5450  2.9587
| improve this answer | |
  • Hello, I am new to ubuntu. My directory name is test. It contains many files such as 2.01149, 2.35698 etc. May I request you to explain how to execute your suggestions on command line, a bit clearly. – vidyadhar Jun 8 '17 at 15:00
  • 1
    @vidyadhar simply cd to directory and type the commands. There's really nothing more to do – Sergiy Kolodyazhnyy Jun 8 '17 at 15:04

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.