-1

In a Bash script, I would like to remove everything after".xml" of all the file in the directory for e.g.

i have two files for now but can be more:

- EXP_GP_00001586_48729648.xml_81234551334_48821189
- EXP_GP_00001454_170375138.xml_48821189

could appreciate some quick help

antoine
  • 1,653
  • 11
  • 23
  • could you clarify the context in which you want to do so (for example bash script, python, C++...) ? because I doubt you're expecting an answer like "rename them manually" :-). – antoine May 21 '20 at 12:57
  • it is bash script , actually the whole issue is i am running a loop which check for some conditions and if it is true then it should move the file to a particular directory with the name of above file example EXP_GP_00001454_170375138.xml and so on till xml only. – Rohit Shamdasani May 21 '20 at 13:05
  • i tried some commands but does not seems to be working like f1=`basename $file | cut -f1 -c"xml"` – Rohit Shamdasani May 21 '20 at 13:15

1 Answers1

0

In a bash script, your problem looks like removing the extension of a file. And that case have been treated here : Extract filename and extension in Bash

You can easily adapt it to your problem :

FILE=EXP_GP_00001586_48729648.xml_81234551334_48821189
mv $FILE ${FILE%.xml*}.xml
antoine
  • 1,653
  • 11
  • 23
  • for file in /home/insahr27/*EXP_GP* do if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ] && [ -f "$file" ]; then mv $file /home/outbound/ mv $file ${file%.xml*}.xml else echo "no files to collect" fi done i tried this but it say no such file in directory mv: cannot stat '/home/outbound/EXP_GP_00001454_170375138.xml_48821189': No such file or directory – Rohit Shamdasani May 21 '20 at 14:40
  • above is the loop i have used and checked for files modification time and then trying to move the file with new name as you showed but it seems some error – Rohit Shamdasani May 21 '20 at 14:44
  • you've got an error because you are trying to move the file twice in `mv $file /home/outbound/ && mv $file ${file%.xml*}.xml`, the first `mv` really move the file, and the second one is failing because the file doesn't exist anymore (because you moved it). You can do both in one call : `mv $file /home/outbound/${file%.xml*}.xml` – antoine May 22 '20 at 07:55
  • Thanks it worked, sorry i am new but when running in a loop first run does it all properly when in continuation it run second time it shows the message – Rohit Shamdasani May 26 '20 at 09:25
  • stderr=mv: '/home/outbound/EXP_GP_00001600_49334826.xml_820541123234' and '/home/outbound/ftp/EXP_GP_00001600_49334826.xml' are the same file – Rohit Shamdasani May 26 '20 at 09:25
  • could you please help to know how to avoid this message – Rohit Shamdasani May 26 '20 at 09:25
  • I'm sorry but I can't help you more without more information, could you post a code snippet ? because the 2 files you mention are obviously not the same (they are not in the same folder) – antoine May 26 '20 at 14:23
  • or maybe `/home/outbound/ftp`is a symlink to `/home/outbound` – antoine May 26 '20 at 14:47
  • #!/bin/bash #$1 - Order number cd /home/outbound rm *$1* #for the remaining files check and process further: for file in /home/outbound/*EXP_GP* do f1=`basename $file` if [ -f "$file" ];then if [ `stat --format=%Y $file` -le $(( `date +%s` - 1800 )) ]; then mv $file /home/outbound/ftp/${f1%.xml*}.xml else echo "no files to collect" fi else echo "no files to collect" fi done – Rohit Shamdasani May 26 '20 at 18:41
  • how do we compare file under a bunch of files present in a directory and if match found rename the file found for e.g if file name of the match file is abc.xml-1 then it should rename it to abc.xml-2 and again if same same file found for second iteration it should rename it to abc.xml-3 – Rohit Shamdasani May 31 '20 at 07:55
  • if you have another question, please post a new one. and don't forget to mark this one as "resolved" by accepting one of the answers, if you think your question has been answered. – antoine Jun 01 '20 at 07:56
  • if this answer helped you, don't forget to vote up or even accept it if you think it is answering your question – antoine Jun 04 '20 at 18:46