How to rename a file to have the same name and extension as another file in same directory [closed]
up vote
1
down vote
favorite
I am trying to use the find command to edit files named "msf.txt" in many different sub-directories and I want it to take the same filename and extension as whatever file shares the same extension in it's directory. So as an example, let's pretend I'm inside one of the sub directories which has an msf.txt file and an msf-01.txt file. I want to rename msf.txt to msf-01.txt. I want to automate this across many sub-directories, but only do it in sub-directories that have the phrase "data" in the directory name. Can anyone provide some suggestions? I'd prefer to use something like find for this.
This is what the current directory looks like
main/
|--sub01
├── data/
│ ├── mgw.txt
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw.txt
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
This is what I want it to look like and the .txt files inside the data directories are meant to contain what was inside the mgw.txt files
main/
|--sub01
├── data/
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap.png
│ └── bootstrap.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
bash find
closed as unclear what you're asking by Sparhawk, Stephen Harris, G-Man, schily, RalfFriedl Dec 3 at 17:55
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 3 more comments
up vote
1
down vote
favorite
I am trying to use the find command to edit files named "msf.txt" in many different sub-directories and I want it to take the same filename and extension as whatever file shares the same extension in it's directory. So as an example, let's pretend I'm inside one of the sub directories which has an msf.txt file and an msf-01.txt file. I want to rename msf.txt to msf-01.txt. I want to automate this across many sub-directories, but only do it in sub-directories that have the phrase "data" in the directory name. Can anyone provide some suggestions? I'd prefer to use something like find for this.
This is what the current directory looks like
main/
|--sub01
├── data/
│ ├── mgw.txt
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw.txt
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
This is what I want it to look like and the .txt files inside the data directories are meant to contain what was inside the mgw.txt files
main/
|--sub01
├── data/
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap.png
│ └── bootstrap.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
bash find
closed as unclear what you're asking by Sparhawk, Stephen Harris, G-Man, schily, RalfFriedl Dec 3 at 17:55
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
Thank you. So in the first sub-directory (main/sub01/data/
),mgw.txt
is renamed tomgw-01.txt
. Presumably the01
in this filename comes fromsub01
. Where does the originalmgw-01.txt
go? Is it just deleted?
– Sparhawk
Dec 3 at 6:06
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to use the find command to edit files named "msf.txt" in many different sub-directories and I want it to take the same filename and extension as whatever file shares the same extension in it's directory. So as an example, let's pretend I'm inside one of the sub directories which has an msf.txt file and an msf-01.txt file. I want to rename msf.txt to msf-01.txt. I want to automate this across many sub-directories, but only do it in sub-directories that have the phrase "data" in the directory name. Can anyone provide some suggestions? I'd prefer to use something like find for this.
This is what the current directory looks like
main/
|--sub01
├── data/
│ ├── mgw.txt
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw.txt
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
This is what I want it to look like and the .txt files inside the data directories are meant to contain what was inside the mgw.txt files
main/
|--sub01
├── data/
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap.png
│ └── bootstrap.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
bash find
I am trying to use the find command to edit files named "msf.txt" in many different sub-directories and I want it to take the same filename and extension as whatever file shares the same extension in it's directory. So as an example, let's pretend I'm inside one of the sub directories which has an msf.txt file and an msf-01.txt file. I want to rename msf.txt to msf-01.txt. I want to automate this across many sub-directories, but only do it in sub-directories that have the phrase "data" in the directory name. Can anyone provide some suggestions? I'd prefer to use something like find for this.
This is what the current directory looks like
main/
|--sub01
├── data/
│ ├── mgw.txt
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw.txt
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
This is what I want it to look like and the .txt files inside the data directories are meant to contain what was inside the mgw.txt files
main/
|--sub01
├── data/
│ ├── mgw-01.txt
├── pictures/
│ ├── bootstrap.png
│ └── bootstrap.png
└── extra/
├── stuff.txt
|--sub02
├── data/
│ ├── mgw-02.txt
├── pictures/
│ ├── bootstrap1.png
│ └── bootstrap2.png
└── extra/
├── stuff.txt
bash find
bash find
edited Dec 3 at 6:33
asked Dec 3 at 3:39
M76
62
62
closed as unclear what you're asking by Sparhawk, Stephen Harris, G-Man, schily, RalfFriedl Dec 3 at 17:55
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Sparhawk, Stephen Harris, G-Man, schily, RalfFriedl Dec 3 at 17:55
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
1
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
Thank you. So in the first sub-directory (main/sub01/data/
),mgw.txt
is renamed tomgw-01.txt
. Presumably the01
in this filename comes fromsub01
. Where does the originalmgw-01.txt
go? Is it just deleted?
– Sparhawk
Dec 3 at 6:06
|
show 3 more comments
1
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
Thank you. So in the first sub-directory (main/sub01/data/
),mgw.txt
is renamed tomgw-01.txt
. Presumably the01
in this filename comes fromsub01
. Where does the originalmgw-01.txt
go? Is it just deleted?
– Sparhawk
Dec 3 at 6:06
1
1
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
Thank you. So in the first sub-directory (
main/sub01/data/
), mgw.txt
is renamed to mgw-01.txt
. Presumably the 01
in this filename comes from sub01
. Where does the original mgw-01.txt
go? Is it just deleted?– Sparhawk
Dec 3 at 6:06
Thank you. So in the first sub-directory (
main/sub01/data/
), mgw.txt
is renamed to mgw-01.txt
. Presumably the 01
in this filename comes from sub01
. Where does the original mgw-01.txt
go? Is it just deleted?– Sparhawk
Dec 3 at 6:06
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Maybe something like:
#! /bin/zsh -
would-do() print -r -- "$@"
for f (**/data/mgw.txt(N)) {
others=($f:r-*.txt(N))
if (($#others)); then
would-do mkdir -p $f:h:h/backup &&
would-do mv $others $f:h:h/backup/ &&
would-do mv $f $others[1]
fi
}
Change the would-do
definition to would-do() "$@"
to actually do it.
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion(**/data/mgw.txt(N)
would still work right? Or would I need more?
– M76
Dec 4 at 16:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Maybe something like:
#! /bin/zsh -
would-do() print -r -- "$@"
for f (**/data/mgw.txt(N)) {
others=($f:r-*.txt(N))
if (($#others)); then
would-do mkdir -p $f:h:h/backup &&
would-do mv $others $f:h:h/backup/ &&
would-do mv $f $others[1]
fi
}
Change the would-do
definition to would-do() "$@"
to actually do it.
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion(**/data/mgw.txt(N)
would still work right? Or would I need more?
– M76
Dec 4 at 16:02
add a comment |
up vote
0
down vote
Maybe something like:
#! /bin/zsh -
would-do() print -r -- "$@"
for f (**/data/mgw.txt(N)) {
others=($f:r-*.txt(N))
if (($#others)); then
would-do mkdir -p $f:h:h/backup &&
would-do mv $others $f:h:h/backup/ &&
would-do mv $f $others[1]
fi
}
Change the would-do
definition to would-do() "$@"
to actually do it.
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion(**/data/mgw.txt(N)
would still work right? Or would I need more?
– M76
Dec 4 at 16:02
add a comment |
up vote
0
down vote
up vote
0
down vote
Maybe something like:
#! /bin/zsh -
would-do() print -r -- "$@"
for f (**/data/mgw.txt(N)) {
others=($f:r-*.txt(N))
if (($#others)); then
would-do mkdir -p $f:h:h/backup &&
would-do mv $others $f:h:h/backup/ &&
would-do mv $f $others[1]
fi
}
Change the would-do
definition to would-do() "$@"
to actually do it.
Maybe something like:
#! /bin/zsh -
would-do() print -r -- "$@"
for f (**/data/mgw.txt(N)) {
others=($f:r-*.txt(N))
if (($#others)); then
would-do mkdir -p $f:h:h/backup &&
would-do mv $others $f:h:h/backup/ &&
would-do mv $f $others[1]
fi
}
Change the would-do
definition to would-do() "$@"
to actually do it.
answered Dec 3 at 6:41
Stéphane Chazelas
297k54562907
297k54562907
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion(**/data/mgw.txt(N)
would still work right? Or would I need more?
– M76
Dec 4 at 16:02
add a comment |
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion(**/data/mgw.txt(N)
would still work right? Or would I need more?
– M76
Dec 4 at 16:02
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion
(**/data/mgw.txt(N)
would still work right? Or would I need more?– M76
Dec 4 at 16:02
How does the backup of the old files work? Where do they get sent? Also, I'm guessing if I ever wanted to apply this to other directories with many (+5) levels of sub directories then the wildcard expansion
(**/data/mgw.txt(N)
would still work right? Or would I need more?– M76
Dec 4 at 16:02
add a comment |
1
I'm pretty confused here. Could you please edit your question, removing all extraneous information, and distilling the problem to its core? i.e. what is (a minimal version of) the current situation, and what is the expected version.
– Sparhawk
Dec 3 at 3:46
@Sparkhawk Okay I see what you mean. Just edited. Hopefully will be more clear
– M76
Dec 3 at 4:51
I'm still confused. Just show the literal directory structure, before and after.
– Sparhawk
Dec 3 at 4:52
@Sparhawk okay I added the directory structure
– M76
Dec 3 at 5:48
Thank you. So in the first sub-directory (
main/sub01/data/
),mgw.txt
is renamed tomgw-01.txt
. Presumably the01
in this filename comes fromsub01
. Where does the originalmgw-01.txt
go? Is it just deleted?– Sparhawk
Dec 3 at 6:06