How to use a Bash Script to move and rename files with grep

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















Hi I an FTP folder called Input where I get sent files which look something like this:



"Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf"
"Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf"


Basicly the description of the file can be different but it always includes "T-" and some numbers.



I need to create a bash script that copies the file to the folder ../Output and renames the file so the "T-" and the numbers comes first, and then rest of the filename.
Example: "T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf"



I intended to use Grep to get the name right.
Searching for (.+)(T-d+) and replacing it with $2 - $1



But I can't get it to work right.










share|improve this question



















  • 1





    Does your system have the perl-based rename / prename command?

    – steeldriver
    Jan 11 at 1:02











  • I am running this on Macintosh and intended to use the move mv command to rename it.

    – Þorgeir Valur Ellertsson
    Jan 11 at 1:28






  • 1





    What do your current efforts look like?

    – tink
    Jan 11 at 1:50















0















Hi I an FTP folder called Input where I get sent files which look something like this:



"Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf"
"Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf"


Basicly the description of the file can be different but it always includes "T-" and some numbers.



I need to create a bash script that copies the file to the folder ../Output and renames the file so the "T-" and the numbers comes first, and then rest of the filename.
Example: "T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf"



I intended to use Grep to get the name right.
Searching for (.+)(T-d+) and replacing it with $2 - $1



But I can't get it to work right.










share|improve this question



















  • 1





    Does your system have the perl-based rename / prename command?

    – steeldriver
    Jan 11 at 1:02











  • I am running this on Macintosh and intended to use the move mv command to rename it.

    – Þorgeir Valur Ellertsson
    Jan 11 at 1:28






  • 1





    What do your current efforts look like?

    – tink
    Jan 11 at 1:50













0












0








0








Hi I an FTP folder called Input where I get sent files which look something like this:



"Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf"
"Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf"


Basicly the description of the file can be different but it always includes "T-" and some numbers.



I need to create a bash script that copies the file to the folder ../Output and renames the file so the "T-" and the numbers comes first, and then rest of the filename.
Example: "T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf"



I intended to use Grep to get the name right.
Searching for (.+)(T-d+) and replacing it with $2 - $1



But I can't get it to work right.










share|improve this question
















Hi I an FTP folder called Input where I get sent files which look something like this:



"Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf"
"Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf"


Basicly the description of the file can be different but it always includes "T-" and some numbers.



I need to create a bash script that copies the file to the folder ../Output and renames the file so the "T-" and the numbers comes first, and then rest of the filename.
Example: "T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf"



I intended to use Grep to get the name right.
Searching for (.+)(T-d+) and replacing it with $2 - $1



But I can't get it to work right.







linux shell-script grep rename






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 11 at 1:27







Þorgeir Valur Ellertsson

















asked Jan 11 at 0:50









Þorgeir Valur EllertssonÞorgeir Valur Ellertsson

11




11







  • 1





    Does your system have the perl-based rename / prename command?

    – steeldriver
    Jan 11 at 1:02











  • I am running this on Macintosh and intended to use the move mv command to rename it.

    – Þorgeir Valur Ellertsson
    Jan 11 at 1:28






  • 1





    What do your current efforts look like?

    – tink
    Jan 11 at 1:50












  • 1





    Does your system have the perl-based rename / prename command?

    – steeldriver
    Jan 11 at 1:02











  • I am running this on Macintosh and intended to use the move mv command to rename it.

    – Þorgeir Valur Ellertsson
    Jan 11 at 1:28






  • 1





    What do your current efforts look like?

    – tink
    Jan 11 at 1:50







1




1





Does your system have the perl-based rename / prename command?

– steeldriver
Jan 11 at 1:02





Does your system have the perl-based rename / prename command?

– steeldriver
Jan 11 at 1:02













I am running this on Macintosh and intended to use the move mv command to rename it.

– Þorgeir Valur Ellertsson
Jan 11 at 1:28





I am running this on Macintosh and intended to use the move mv command to rename it.

– Þorgeir Valur Ellertsson
Jan 11 at 1:28




1




1





What do your current efforts look like?

– tink
Jan 11 at 1:50





What do your current efforts look like?

– tink
Jan 11 at 1:50










1 Answer
1






active

oldest

votes


















1














grep alone and replace to me doesn't look like a usable combo. How do you feel about sed?



$ ls *pdf
Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf
Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf


And then a little for loop in bash:



$ for i in *pdf; do mv "$i" "$( echo $i | sed -r 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done


This iterates over all PDFs in the current directory; stores their name in $i for every iteration, echos the content through sed (and uses its output as the target of the mv). sed breaks up the file-name in three parts and rearranges these. I recommend replacing mv with echo mv for testing :)



Which gives us:



$ ls *pdf
T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf
T-987654987 - Guide to Iceland _ Chen-ping Wang itinerary .pdf





share|improve this answer























  • Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

    – Þorgeir Valur Ellertsson
    Jan 13 at 1:17











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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
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%2f493845%2fhow-to-use-a-bash-script-to-move-and-rename-files-with-grep%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














grep alone and replace to me doesn't look like a usable combo. How do you feel about sed?



$ ls *pdf
Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf
Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf


And then a little for loop in bash:



$ for i in *pdf; do mv "$i" "$( echo $i | sed -r 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done


This iterates over all PDFs in the current directory; stores their name in $i for every iteration, echos the content through sed (and uses its output as the target of the mv). sed breaks up the file-name in three parts and rearranges these. I recommend replacing mv with echo mv for testing :)



Which gives us:



$ ls *pdf
T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf
T-987654987 - Guide to Iceland _ Chen-ping Wang itinerary .pdf





share|improve this answer























  • Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

    – Þorgeir Valur Ellertsson
    Jan 13 at 1:17
















1














grep alone and replace to me doesn't look like a usable combo. How do you feel about sed?



$ ls *pdf
Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf
Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf


And then a little for loop in bash:



$ for i in *pdf; do mv "$i" "$( echo $i | sed -r 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done


This iterates over all PDFs in the current directory; stores their name in $i for every iteration, echos the content through sed (and uses its output as the target of the mv). sed breaks up the file-name in three parts and rearranges these. I recommend replacing mv with echo mv for testing :)



Which gives us:



$ ls *pdf
T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf
T-987654987 - Guide to Iceland _ Chen-ping Wang itinerary .pdf





share|improve this answer























  • Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

    – Þorgeir Valur Ellertsson
    Jan 13 at 1:17














1












1








1







grep alone and replace to me doesn't look like a usable combo. How do you feel about sed?



$ ls *pdf
Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf
Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf


And then a little for loop in bash:



$ for i in *pdf; do mv "$i" "$( echo $i | sed -r 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done


This iterates over all PDFs in the current directory; stores their name in $i for every iteration, echos the content through sed (and uses its output as the target of the mv). sed breaks up the file-name in three parts and rearranges these. I recommend replacing mv with echo mv for testing :)



Which gives us:



$ ls *pdf
T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf
T-987654987 - Guide to Iceland _ Chen-ping Wang itinerary .pdf





share|improve this answer













grep alone and replace to me doesn't look like a usable combo. How do you feel about sed?



$ ls *pdf
Guide to Iceland _ Chen-ping Wang itinerary T-987654987.pdf
Guide to Iceland _ T-123654875 Chen-ping Wang itinerary.pdf


And then a little for loop in bash:



$ for i in *pdf; do mv "$i" "$( echo $i | sed -r 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done


This iterates over all PDFs in the current directory; stores their name in $i for every iteration, echos the content through sed (and uses its output as the target of the mv). sed breaks up the file-name in three parts and rearranges these. I recommend replacing mv with echo mv for testing :)



Which gives us:



$ ls *pdf
T-123654875 - Guide to Iceland _ Chen-ping Wang itinerary.pdf
T-987654987 - Guide to Iceland _ Chen-ping Wang itinerary .pdf






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 11 at 5:50









tinktink

4,36111220




4,36111220












  • Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

    – Þorgeir Valur Ellertsson
    Jan 13 at 1:17


















  • Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

    – Þorgeir Valur Ellertsson
    Jan 13 at 1:17

















Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

– Þorgeir Valur Ellertsson
Jan 13 at 1:17






Thank you @tink sed is deffenetly the tool I should be using. There where one fault with the code to have it run in bash in my case sed use "-E" in stead of "-r" for regular expression. $ for i in *pdf; do mv "$i" "$( echo $i | sed -E 's/^(.*)(T-[0-9]9)(.+)$/2 - 13/' )"; done But I need help making changes to the code. I don't need to check if it is PDF and I use an app to run the script and it Use $1 to refer to the file being processed. Can you help me make the modification so it works.

– Þorgeir Valur Ellertsson
Jan 13 at 1:17


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493845%2fhow-to-use-a-bash-script-to-move-and-rename-files-with-grep%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

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

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay