LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2013, 03:09 PM   #1
proudpak
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 04-24-2013, 06:14 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 04-25-2013, 01:30 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command/Script required to send an email alert in case command will not respond rajaniyer123 Linux - General 1 05-19-2012 01:12 PM
Executing shell command from JSP file with command line arg from URL orcusomega Programming 2 01-13-2012 03:38 PM
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 01:31 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 02:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration