I would like to rename all directories in the same directory if the directory matches a pattern the pattern to find is user1 [files.sentfrom.com] - Example: Directory name is:

user1 [files.sentfrom.com] - Directory-Subject

Here's a before image of directory. Before image

I want to rename the directory name to say only:

Directory-Subject (this could have different type of text)

Here's the after image with the renamed directories notice it doesn't rename any other folders or files in the directory. Here's the after image when it's renamed notice it doesn't rename anything else in the directory

Can this be done in regex all at once or do I need a for loop I'm open to another way also.


  • 1
    You want to remove the literal text "User1 [files.sentform.com] - "? Or is part of that a pattern? Please provide two or three sample conversions to we can understand what you're looking for. Are these directories that need renaming all in the same directory, or are they in a tree of directories? Commented Feb 11, 2017 at 15:10
  • @roaima ok these directories are all in the same directory and it's the entire pattern
    – Rick T
    Commented Feb 11, 2017 at 15:21
  • @roaima I added an images does this help?
    – Rick T
    Commented Feb 11, 2017 at 15:29
  • @don_crissti ok I'll add more images.
    – Rick T
    Commented Feb 11, 2017 at 15:31
  • 1
    Your image is useless; it just repeats the single example you've shown in the text. As I asked in the first comment, please provide two or three (or more) sample conversions so we can understand what you're looking for. At the moment it's unclear which part of the name is invariant - and without that we can't hook on to the Directory-Subject component. Commented Feb 11, 2017 at 15:40

3 Answers 3

for name in *\[*\]\ -\ *; do
  if [[ -d "$name" ]] && [[ ! -e "${name#* - }" ]]; then
    mv "$name" "${name#* - }"

The loop above, which would work in bash or ksh93 at least, goes through all names in the current directory matching the filename globbing pattern *\[*\]\ -\ * (the brackets and spaces need to be escaped from the shell) and tests to make sure it's a directory and that the modified name doesn't already exist. If that's all ok, then it renames the directory.

The parameter expansion ${name#* - } will take the value of the variable name and remove everything from the start of it to the first occurrence of ␣-␣ (space-dash-space).

  • Thanks but Directory-Subject could have any type of text not just Directory-Subject.
    – Rick T
    Commented Feb 11, 2017 at 15:52
  • @RickT fixed it now.
    – Kusalananda
    Commented Feb 11, 2017 at 16:03

You can use rename for this:

rename 's/.* ([^ ]*)$/$1/' *

It will remove every character until last space in every filename in directory.

  • (1) Based on the fact that the question shows a folder called “misc fold not touched”, I interpret the question not to mean “remove everything up to (and including) the last space in every name”.  It’s not clear, but the question appears to be about directories whose names begin with user1 [files.sentfrom.com] - .  (2) I don’t see anything in this answer that prevents it from renaming plain files. Commented Feb 11, 2017 at 23:36
  • @G-Man, i answered before question was edited and there weren't restrictions like existing other files in current directory.
    – ValeriyKr
    Commented Feb 12, 2017 at 16:33

find and rename is a powerful team. With the -0 option, all possible filenames are cleanly handled:

find -maxdepth 1 -type d -print0 | xargs -0 rename s/.*Directory-/Directory-/
  • (1) While the question is very unclear, the OP has stated that, when he says “Directory-Subject”, he means a directory and a subject, and not the literal text Directory-Subject.  Therefore, a command that looks for Directory- is probably not what he wants.  (2) You should always quote deferred wildcards, such as, for example, s/ .* …  /  …  /. Commented Feb 11, 2017 at 23:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .