Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing part of a filename with mv

Tags:

bash

mv

How would I rename all -m.css files to .css with the shell utility mv?

like image 657
fread2281 Avatar asked Dec 03 '11 01:12

fread2281


People also ask

How do you change part of a file name?

Type the following command to replace part of file name and press Enter: ls | Rename-Item -NewName {$_.name -replace "OLD-FILE-NAME-PART","NEW-FILE-NAME-PART"} In the command, replace "OLD-FILE-NAME-PART" and "NEW-FILE-NAME-PART" with the old and new part of the file name.

How do I trim a filename in Windows?

Browse to the folder with the offending file, hit Shift + Right Click, and select Open a command window here. Now, input dir /x to see a list of shortened file names rather than the full-length version. From the same Command Prompt window, you can now delete the files using the short name.

How do I remove a file with the name '- something?

How do I remove or access a file with the name '-something' or containing another strange character ? If your file starts with a minus, use the -- flag to rm; if your file is named -g, then your rm command would look like rm -- -g.


1 Answers

Use a for loop:

for file in *-m.css ; do mv "$file" "${file%-m.css}.css" ; done

Using rename might be less verbose.

like image 147
choroba Avatar answered Oct 24 '22 18:10

choroba