3

So I have put a load of names of files in a text file, these are specifically .log files:

ls *.log > finished_data.txt

Now that I Have the list of .log files, how do I keep the names but remove the .log extension? My thought process is renaming them all?

1
  • 2
    Have you noticed the Search Q&A box in the upper right corner of this page? Commented Jun 21, 2016 at 13:14

2 Answers 2

7

Just loop through the .log files and move them:

for file in *.log
do
    mv "$file" "${file%.log}"
done

This uses shell parameter expansion:

$ d="a.log.log"
$ echo "${d%.log}"
a.log
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, works perfectly how could I reverse the action? say adding the .log on the end to certain files?
@user3667111 just say mv "$file" "${file}.log".
@gniourf_gniourf oh thanks, I always get confused with % and %%. I see % strips the shortest match, which is most accurate. Regarding the syntax of the for loop: is there any difference?
@gniourf_gniourf ah true! I misunderstood your comment and thought you referred to using for file in ...; do instead of for file in ... + new line + do. You are absolutely right, updating.
1

Using rename to rename all .log files by removing .log from the end:

rename 's/\.log$//' *.log
  • \.log$ matches .log at the end of the file name and it is being omitted by replacing with blank

If you are using prename, then you can do a dry-run first:

rename -n 's/\.log$//' *.log

If satisfied with the changes to be made:

rename 's/\.log$//' *.log

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.