LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help with rm command (https://www.linuxquestions.org/questions/linux-newbie-8/help-with-rm-command-4175459471/)

proudpak 04-24-2013 03:09 PM

Help with rm command
 
Hi,

I am running following command to process some files and move them into separate directory for dos2unix purpose. I am unable to remove file without prompt

Command used

for a in /mnt/chroot/emr/OUT/Log/* ; do zip /mnt/chroot/emr/OrderProcessed/ReceivedFiles.zip $a ; dos2unix -n $a /mnt/chroot/emr/REJ/`basename $a` ; rm –f $a ; done

I am getting following resulted

adding: mnt/chroot/emr/OUT/Log/LAB_LAB01332_20130418_10.HL7 (deflated 62%)
dos2unix: converting file /mnt/chroot/emr/OUT/Log/LAB_LAB01332_20130418_10.HL7 to file /mnt/chroot/emr/REJ/PROGNOCIS_LAB_LAB01332_20130418_10.HL7 in UNIX format ...
rm: cannot lstat `.f': No such file or directory
rm: remove regular file `/mnt/chroot/emr/OUT/Log/LAB_LAB01332_20130418_10.HL7'? n
adding: mnt/chroot/emr/OUT/Log/LAB_LAB01333_20130418_10.HL7 (deflated 62%)
dos2unix: converting file /mnt/chroot/emr/OUT/Log/LAB_LAB01333_20130418_10.HL7 to file /mnt/chroot/emr/REJ/LAB_LAB01333_20130418_10.HL7 in UNIX format ...
rm: cannot lstat `.f': No such file or directory
rm: remove regular file `/mnt/chroot/emr/OUT/Log/LAB_LAB01333_20130418_10.HL7'? n

Please help.

chrism01 04-24-2013 06:14 PM

Easier to read if you use code tags https://www.linuxquestions.org/quest...do=bbcode#code and multiple lines
Code:

for a in /mnt/chroot/emr/OUT/Log/*
do
    zip /mnt/chroot/emr/OrderProcessed/ReceivedFiles.zip $a
    dos2unix -n $a /mnt/chroot/emr/REJ/`basename $a`
    rm –f $a      # my version: rm -f
done

You've copied and pasted that from the web; its got a long hyphen; contrast with the length of my version.
When interpreted by bash its comes out as '.', basically a font issue.

Also, best to use $(...) instead of backquotes.
http://wiki.bash-hackers.org/syntax/expansion/cmdsubst

David the H. 04-25-2013 01:30 PM

In addition, the variables are unquoted, so word-splitting will take place on the values, if they contain whitespace.

And there's no need for basename at all, when we can use parameter substitution.

I also think it's best to use clear variable names whenever possible.

Code:

for fname in /mnt/chroot/emr/OUT/Log/* ; do

    zip /mnt/chroot/emr/OrderProcessed/ReceivedFiles.zip "$fname"

    dos2unix -n "$fname" "/mnt/chroot/emr/REJ/${fname##*/}"

    rm -f "$fname"

done

Frankly though, I'm not sure I quite understand the command flow here. First you archive the files, then you convert them into unix line ending format (while copying them to a new directory), and finally you remove the originals? I guess it's ok, but it seems a bit odd to me.


Actually, it seems to me that the loop is unnecessary. You should be able to use direct globbing with all the commands.

Code:

filepath='/mnt/chroot/emr/OUT/Log'
copypath='/mnt/chroot/emr/REJ'
zipfile='/mnt/chroot/emr/OrderProcessed/ReceivedFiles.zip'

zip "$zipfile" "$filepath/"*
cp "$filepath/"* "$copypath"
rm -f "$filepath/"*
dos2unix "$copypath/"*

The only things this assumes are that "$copypath" only holds the copied files (or that it's safe to run dos2unix on the other files in it), and that the number of files is low enough not to overflood the system's ARG_MAX value.

As demonstrated too, it's generally better to store fixed data strings in variables first, if this isn't just a one-off.


All times are GMT -5. The time now is 06:52 AM.