Skip to content

Instantly share code, notes, and snippets.

@guisehn
Created February 4, 2020 20:28
Show Gist options
  • Select an option

  • Save guisehn/5438bbc22138435665c6e996493fe02b to your computer and use it in GitHub Desktop.

Select an option

Save guisehn/5438bbc22138435665c6e996493fe02b to your computer and use it in GitHub Desktop.
Remove prefix from multiple files in Linux console

Bash

for file in prefix*; do mv "$file" "${file#prefix}"; done;

The for loop iterates over all files with the prefix. The do removes from all those files iterated over the prefix.

Here is an example to remove "bla_" form the following files:

bla_1.txt
bla_2.txt
bla_3.txt
blub.txt

Command

for file in bla_*; do mv "$file" "${file#bla_}";done;

Result in file system:

1.txt
2.txt
3.txt
blub.txt
@BennisonDevadoss
Copy link

@guisehn, Thank you for sharing this; it saves a significant amount of time!

@rocketmike12
Copy link

Thank you!

@caseychambliss
Copy link

Where can I find a technical explanation as to what this is doing? It worked for me, but I'm not clear on the formatting in the "do" statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment