replace files in the command line with specific string
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I need to search (on the whole disk) and replace (where there are matches) one file with another (both in the same path).
Example:
Folder 1
x*.txt (good) (e.g.: xFile.txt)
*.txt (bad) (e.g.: File.txt)
If there is a match of both files in the same path, i need to delete: *.txt (e.g.: File.txt)
and rename: x*.txt (e.g.: xFile.txt)
to *.txt (e.g.: File.txt)
Result:
Folder 1
*.txt (e.g: File.txt... old xFile.txt)
I use this command:
find -name 'x*.txt' | sed -r 'p;s/g([^/]*.txt)/1/' | xargs -d 'n' -n2 mv
The problem is that the command does not verify if both files exist (xFile.txt and File.txt in the same path) before executing the order
How can I solve it? Thanks in advance
linux bash command-line
add a comment |Â
up vote
1
down vote
favorite
I need to search (on the whole disk) and replace (where there are matches) one file with another (both in the same path).
Example:
Folder 1
x*.txt (good) (e.g.: xFile.txt)
*.txt (bad) (e.g.: File.txt)
If there is a match of both files in the same path, i need to delete: *.txt (e.g.: File.txt)
and rename: x*.txt (e.g.: xFile.txt)
to *.txt (e.g.: File.txt)
Result:
Folder 1
*.txt (e.g: File.txt... old xFile.txt)
I use this command:
find -name 'x*.txt' | sed -r 'p;s/g([^/]*.txt)/1/' | xargs -d 'n' -n2 mv
The problem is that the command does not verify if both files exist (xFile.txt and File.txt in the same path) before executing the order
How can I solve it? Thanks in advance
linux bash command-line
1
findxfile.txt
, and check[[ -f "$file#x" ]]
(which strips a leadingx
from the found file and checks to see if that file is present. If present, then do your switcheroo.
â DopeGhoti
Jun 13 at 17:25
1
Alternatively, findfile.txt
and check[[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.
â DopeGhoti
Jun 13 at 17:26
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
What if there's axx1.txt
,x1.txt
and1.txt
?
â Stéphane Chazelas
Jun 13 at 20:56
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to search (on the whole disk) and replace (where there are matches) one file with another (both in the same path).
Example:
Folder 1
x*.txt (good) (e.g.: xFile.txt)
*.txt (bad) (e.g.: File.txt)
If there is a match of both files in the same path, i need to delete: *.txt (e.g.: File.txt)
and rename: x*.txt (e.g.: xFile.txt)
to *.txt (e.g.: File.txt)
Result:
Folder 1
*.txt (e.g: File.txt... old xFile.txt)
I use this command:
find -name 'x*.txt' | sed -r 'p;s/g([^/]*.txt)/1/' | xargs -d 'n' -n2 mv
The problem is that the command does not verify if both files exist (xFile.txt and File.txt in the same path) before executing the order
How can I solve it? Thanks in advance
linux bash command-line
I need to search (on the whole disk) and replace (where there are matches) one file with another (both in the same path).
Example:
Folder 1
x*.txt (good) (e.g.: xFile.txt)
*.txt (bad) (e.g.: File.txt)
If there is a match of both files in the same path, i need to delete: *.txt (e.g.: File.txt)
and rename: x*.txt (e.g.: xFile.txt)
to *.txt (e.g.: File.txt)
Result:
Folder 1
*.txt (e.g: File.txt... old xFile.txt)
I use this command:
find -name 'x*.txt' | sed -r 'p;s/g([^/]*.txt)/1/' | xargs -d 'n' -n2 mv
The problem is that the command does not verify if both files exist (xFile.txt and File.txt in the same path) before executing the order
How can I solve it? Thanks in advance
linux bash command-line
edited Jun 13 at 20:21
asked Jun 13 at 17:17
user4839775
9110
9110
1
findxfile.txt
, and check[[ -f "$file#x" ]]
(which strips a leadingx
from the found file and checks to see if that file is present. If present, then do your switcheroo.
â DopeGhoti
Jun 13 at 17:25
1
Alternatively, findfile.txt
and check[[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.
â DopeGhoti
Jun 13 at 17:26
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
What if there's axx1.txt
,x1.txt
and1.txt
?
â Stéphane Chazelas
Jun 13 at 20:56
add a comment |Â
1
findxfile.txt
, and check[[ -f "$file#x" ]]
(which strips a leadingx
from the found file and checks to see if that file is present. If present, then do your switcheroo.
â DopeGhoti
Jun 13 at 17:25
1
Alternatively, findfile.txt
and check[[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.
â DopeGhoti
Jun 13 at 17:26
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
What if there's axx1.txt
,x1.txt
and1.txt
?
â Stéphane Chazelas
Jun 13 at 20:56
1
1
find
xfile.txt
, and check [[ -f "$file#x" ]]
(which strips a leading x
from the found file and checks to see if that file is present. If present, then do your switcheroo.â DopeGhoti
Jun 13 at 17:25
find
xfile.txt
, and check [[ -f "$file#x" ]]
(which strips a leading x
from the found file and checks to see if that file is present. If present, then do your switcheroo.â DopeGhoti
Jun 13 at 17:25
1
1
Alternatively, find
file.txt
and check [[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.â DopeGhoti
Jun 13 at 17:26
Alternatively, find
file.txt
and check [[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.â DopeGhoti
Jun 13 at 17:26
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
What if there's a
xx1.txt
, x1.txt
and 1.txt
?â Stéphane Chazelas
Jun 13 at 20:56
What if there's a
xx1.txt
, x1.txt
and 1.txt
?â Stéphane Chazelas
Jun 13 at 20:56
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
With GNU tools, you could do something like:
(export LC_ALL=C
find . -name '*.txt' -print0 |
sed -Ez 's|/x([^/]*)$|/1|' |
sort -z |
uniq -zd |
sed -z 'h;s|.*/|&x|;G' |
xargs -r0n2 echo mv)
That assumes there are not files whose name starts with more than one x
. For instance, it won't do mv ./xx.txt ./x.txt
Yes thanks. That is
â user4839775
Jun 13 at 21:40
add a comment |Â
up vote
0
down vote
First, we define a function to do the work for us:
switcheroo()
[[ -f x"$1" ]] && mv -f x"$1" "$1"
export -f switcheroo
Now, we use find
and some black magic to iterate over our foundlings:
find . -name *.txt -execdir bash -c 'switcheroo "$0"' "" ;
We use find
to locate all files with a .txt
extension (this can be refined to suit whatever criteria you like) and in turn run them through the switcheroo process -- we use [[ ... ]]
to test for the existence of the replacement file, and if it is present, use mv
to effect the replacement.
I don't thinkfind
can run a shell function (but it's simple enough to just put inline)
â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
 |Â
show 2 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With GNU tools, you could do something like:
(export LC_ALL=C
find . -name '*.txt' -print0 |
sed -Ez 's|/x([^/]*)$|/1|' |
sort -z |
uniq -zd |
sed -z 'h;s|.*/|&x|;G' |
xargs -r0n2 echo mv)
That assumes there are not files whose name starts with more than one x
. For instance, it won't do mv ./xx.txt ./x.txt
Yes thanks. That is
â user4839775
Jun 13 at 21:40
add a comment |Â
up vote
3
down vote
accepted
With GNU tools, you could do something like:
(export LC_ALL=C
find . -name '*.txt' -print0 |
sed -Ez 's|/x([^/]*)$|/1|' |
sort -z |
uniq -zd |
sed -z 'h;s|.*/|&x|;G' |
xargs -r0n2 echo mv)
That assumes there are not files whose name starts with more than one x
. For instance, it won't do mv ./xx.txt ./x.txt
Yes thanks. That is
â user4839775
Jun 13 at 21:40
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With GNU tools, you could do something like:
(export LC_ALL=C
find . -name '*.txt' -print0 |
sed -Ez 's|/x([^/]*)$|/1|' |
sort -z |
uniq -zd |
sed -z 'h;s|.*/|&x|;G' |
xargs -r0n2 echo mv)
That assumes there are not files whose name starts with more than one x
. For instance, it won't do mv ./xx.txt ./x.txt
With GNU tools, you could do something like:
(export LC_ALL=C
find . -name '*.txt' -print0 |
sed -Ez 's|/x([^/]*)$|/1|' |
sort -z |
uniq -zd |
sed -z 'h;s|.*/|&x|;G' |
xargs -r0n2 echo mv)
That assumes there are not files whose name starts with more than one x
. For instance, it won't do mv ./xx.txt ./x.txt
answered Jun 13 at 21:05
Stéphane Chazelas
279k53513844
279k53513844
Yes thanks. That is
â user4839775
Jun 13 at 21:40
add a comment |Â
Yes thanks. That is
â user4839775
Jun 13 at 21:40
Yes thanks. That is
â user4839775
Jun 13 at 21:40
Yes thanks. That is
â user4839775
Jun 13 at 21:40
add a comment |Â
up vote
0
down vote
First, we define a function to do the work for us:
switcheroo()
[[ -f x"$1" ]] && mv -f x"$1" "$1"
export -f switcheroo
Now, we use find
and some black magic to iterate over our foundlings:
find . -name *.txt -execdir bash -c 'switcheroo "$0"' "" ;
We use find
to locate all files with a .txt
extension (this can be refined to suit whatever criteria you like) and in turn run them through the switcheroo process -- we use [[ ... ]]
to test for the existence of the replacement file, and if it is present, use mv
to effect the replacement.
I don't thinkfind
can run a shell function (but it's simple enough to just put inline)
â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
 |Â
show 2 more comments
up vote
0
down vote
First, we define a function to do the work for us:
switcheroo()
[[ -f x"$1" ]] && mv -f x"$1" "$1"
export -f switcheroo
Now, we use find
and some black magic to iterate over our foundlings:
find . -name *.txt -execdir bash -c 'switcheroo "$0"' "" ;
We use find
to locate all files with a .txt
extension (this can be refined to suit whatever criteria you like) and in turn run them through the switcheroo process -- we use [[ ... ]]
to test for the existence of the replacement file, and if it is present, use mv
to effect the replacement.
I don't thinkfind
can run a shell function (but it's simple enough to just put inline)
â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
 |Â
show 2 more comments
up vote
0
down vote
up vote
0
down vote
First, we define a function to do the work for us:
switcheroo()
[[ -f x"$1" ]] && mv -f x"$1" "$1"
export -f switcheroo
Now, we use find
and some black magic to iterate over our foundlings:
find . -name *.txt -execdir bash -c 'switcheroo "$0"' "" ;
We use find
to locate all files with a .txt
extension (this can be refined to suit whatever criteria you like) and in turn run them through the switcheroo process -- we use [[ ... ]]
to test for the existence of the replacement file, and if it is present, use mv
to effect the replacement.
First, we define a function to do the work for us:
switcheroo()
[[ -f x"$1" ]] && mv -f x"$1" "$1"
export -f switcheroo
Now, we use find
and some black magic to iterate over our foundlings:
find . -name *.txt -execdir bash -c 'switcheroo "$0"' "" ;
We use find
to locate all files with a .txt
extension (this can be refined to suit whatever criteria you like) and in turn run them through the switcheroo process -- we use [[ ... ]]
to test for the existence of the replacement file, and if it is present, use mv
to effect the replacement.
edited Jun 13 at 20:21
answered Jun 13 at 19:43
DopeGhoti
39.8k54779
39.8k54779
I don't thinkfind
can run a shell function (but it's simple enough to just put inline)
â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
 |Â
show 2 more comments
I don't thinkfind
can run a shell function (but it's simple enough to just put inline)
â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
I don't think
find
can run a shell function (but it's simple enough to just put inline)â ilkkachu
Jun 13 at 19:48
I don't think
find
can run a shell function (but it's simple enough to just put inline)â ilkkachu
Jun 13 at 19:48
Yeah, you do need to use
-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Yeah, you do need to use
-execdir sh '[ -f "x$1" ] && mv "x$1" "$1"' sh ;
â ilkkachu
Jun 13 at 20:01
Nop. syntax error
â user4839775
Jun 13 at 20:14
Nop. syntax error
â user4839775
Jun 13 at 20:14
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
@DopeGhoti Your command does not solve my problem, described in the question. Thanks anyway
â user4839775
Jun 13 at 20:28
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
Could you explain how this does not do what you are asking after?
â DopeGhoti
Jun 13 at 21:06
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f449609%2freplace-files-in-the-command-line-with-specific-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
find
xfile.txt
, and check[[ -f "$file#x" ]]
(which strips a leadingx
from the found file and checks to see if that file is present. If present, then do your switcheroo.â DopeGhoti
Jun 13 at 17:25
1
Alternatively, find
file.txt
and check[[ -f x"$file" ]]
. If the replacement is there, do your switcheroo.â DopeGhoti
Jun 13 at 17:26
@DopeGhoti Can you explain this with a single command line?
â user4839775
Jun 13 at 19:28
What if there's a
xx1.txt
,x1.txt
and1.txt
?â Stéphane Chazelas
Jun 13 at 20:56