|
|
|
|
|
by AdieuToLogic
480 days ago
|
|
> I was doing some file management last weekend and wanted a little script to remove any numbers at the start of a file name and any region values at the end. I also wanted to ensure any duplicate files were moved into a separate folder so i could remove them. > I could have written that code. Not knowing precisely your desired result, the benefit of going through the effort of writing a script is experienced gained and deepening one's understanding of the tools involved. For example, assume this file structure exists: .
├── 001name-eu-central1.ext
├── 001name-us-west.ext
├── 002name-eu-central1.ext
├── 003name-eu-central1.ext
├── keep
└── research
The script logic to do what you describe could be similar to: for file in [0-9]*
do
dest="$(echo $file | sed -E -e 's/^[0-9]*//' -e 's/-(eu-central1|us-west)//')"
if [[ -f "keep/$dest" ]]
then
mv "$file" "research/$file"
else
mv "$file" "keep/$dest"
fi
done
This results in: .
├── keep
│ └── name.ext
└── research
├── 001name-us-west.ext
├── 002name-eu-central1.ext
└── 003name-eu-central1.ext
The net-net is that the journey is sometimes more valuable than the destination. |
|