replace files in the command line with specific string

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash 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







share|improve this question

















  • 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







  • 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 and 1.txt?
    – Stéphane Chazelas
    Jun 13 at 20:56














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







share|improve this question

















  • 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







  • 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 and 1.txt?
    – Stéphane Chazelas
    Jun 13 at 20:56












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







share|improve this question













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









share|improve this question












share|improve this question




share|improve this question








edited Jun 13 at 20:21
























asked Jun 13 at 17:17









user4839775

9110




9110







  • 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







  • 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 and 1.txt?
    – Stéphane Chazelas
    Jun 13 at 20:56












  • 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







  • 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 and 1.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










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






share|improve this answer





















  • Yes thanks. That is
    – user4839775
    Jun 13 at 21:40

















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.






share|improve this answer























  • 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










  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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






share|improve this answer





















  • Yes thanks. That is
    – user4839775
    Jun 13 at 21:40














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






share|improve this answer





















  • Yes thanks. That is
    – user4839775
    Jun 13 at 21:40












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






share|improve this answer













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







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 13 at 21:05









Stéphane Chazelas

279k53513844




279k53513844











  • 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




Yes thanks. That is
– user4839775
Jun 13 at 21:40












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.






share|improve this answer























  • 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










  • 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














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.






share|improve this answer























  • 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










  • 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












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.






share|improve this answer















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.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 13 at 20:21


























answered Jun 13 at 19:43









DopeGhoti

39.8k54779




39.8k54779











  • 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










  • 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










  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































UxS0R2Squ2fZUUU6VN,nD0a,MG2MoElL 7vJ7 0Knf ef8QNIDSXYb 2ygU,Fj mivqNVR5r,w3rtcdbnqYyAs V2e3R B,n,52KRSz7N2
SjiEV9GNtCINELZpt yYFXhJ3

Popular posts from this blog

How to check contact read email or not when send email to Individual?

How many registers does an x86_64 CPU actually have?

Displaying single band from multi-band raster using QGIS