0

I have many subdirectories where each directory contains a time at the end of its name. I want to remove this. Is there an easy way to do this?

.
├── ACRA
│   └── acra2017-12-25 16:01:57
├── Activiti
│   └── Activiti2017-05-02 17:15:37

to

.
├── ACRA
│   └── acra2017-12-25
├── Activiti
│   └── Activiti2017-05-02

2 Answers 2

2
find . -type d -name '*[0-2][0-9]:[0-5][0-9]:[0-5][0-9]' -exec sh -c '
    for d; do mv -- "$d" "${d% *}"; done
' findsh {} +

Before actually running the script, you may put an echo between do and mv to be sure that the correct mv operation will apply.

  • -type d: Select only directories
  • -name '*[0-2][0-9]:[0-5][0-9]:[0-5][0-9]': A glob pattern to match only names with those times at the end.
  • for d; do mv -- "$d" "${d% *}"; done: For each directory found, apply the mv operation, renaming it. The ${d% *} removes from the filename all after (and including) the last space.
0
1

Using find and the Perl rename utility:

find . -type d -name "* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]" \
  -exec rename -n 's/ \d{2}:\d{2}:\d{2}$//' {} \;

Remove the -n option if the output looks as expected.

You must log in to answer this question.

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