How to bypass rm command when there are no files to delete?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to bypass rm command when there are no files to delete?
# 1  
Old 01-17-2018
Tools How to bypass rm command when there are no files to delete?

Hi,

Below command works fine when we have other files apart from hello.txt
Code:
ls | ggrep -v hello* | xargs rm -rf

But, if there is only one file i.e hello.txt the rm command does not find anything to delete and the script hangs.

While there could be trivial ways to check using if conditions if there are other files than hello* only then trigger the rm command else do not.

I was looking for a better solution if anyone can suggest.
# 2  
Old 01-17-2018
Sure the ggrep command exists on your system (which, btw, you fail to mention)?

Any error message? On my system(s), rm with no files to remove doesn't hang but complains:
Code:
rm: missing operand
Try 'rm --help' for more information.

or
Code:
usage: rm [-f | -i] [-dIPRrvW] file ...
       unlink file

# 3  
Old 01-17-2018
Quote:
Originally Posted by mohtashims
Hi,

Below command works fine when we have other files apart from hello.txt
Code:
ls | ggrep -v hello* | xargs rm -rf

But, if there is only one file i.e hello.txt the rm command does not find anything to delete and the script hangs.

While there could be trivial ways to check using if conditions if there are other files than hello* only then trigger the rm command else do not.

I was looking for a better solution if anyone can suggest.
In
Code:
ls | ggrep -v hello* | xargs rm -rf

The ggrep command has an incorrect syntax. I suggest you try:
Code:
ggrep -v '^hello'

Also, better try using rm instead of rm -rf
# 4  
Old 01-18-2018
Depending on your version of xargs you may have the --no-run-if-empty GNU extension option available to you:

Code:
ls | ggrep -v hello* | xargs --no-run-if-empty rm -rf

# 5  
Old 01-18-2018
I also would be interested to know what operating system you're using. I don't see anything in the standards that would allow xargs to invoke commands after it has hit EOF on its standard input. Therefore, there shouldn't be any need for a --no-run-if-empty option since that should be the default behavior. On macOS version 10.13.2 with a BSD-based xargs utility, the following command when run in a directory where there is no filename containing the string NotFound:
Code:
ls | grep NotFound | xargs printf '"%s"\n'

returns a prompt for the next command almost immediately after invoking it with no output produced other than the prompt for the next command from my login shell.
# 6  
Old 01-18-2018
On Fedora 27 (and cygwin), both using xargs (GNU findutils) 4.6.0

I get the following result:

Code:
$ xargs echo "test" < /dev/null
test
$ xargs --no-run-if-empty echo "test" < /dev/null
$


Manual indicates it runs one or more times:

Code:
DESCRIPTION
       This manual page documents the GNU version of xargs.  xargs reads items
       from  the  standard  input, delimited by blanks (which can be protected
       with double or single quotes or a backslash) or newlines, and  executes
       the  command (default is /bin/echo) one or more times with any initial-
       arguments followed by items read from standard input.  Blank  lines  on
       the standard input are ignored.

# 7  
Old 01-19-2018
Is it too simple to suggest:-

Code:
if [ $(ls | ggrep -v "^Hello" | wc -l) -gt 0 ]
then
   ls | ggrep -v "^Hello" | xargs echo            # ....or whatever
fi


It might be a little inefficient because it reads the directory twice, but I'm hoping that there are not many files to list out. Just a thought.

Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please - Looking for a online command to delete files

I have list of files like below, I want to delete files older than 2 days except S0000000.LOG, I have command find /export/home/X_GZPQJK/out/file* -mtime +1 -exec rm {} \; but it is deleting S0000000.LOG, Can you please help me how to modify command to delete except S0000000.LOG. $ ls -ltr... (2 Replies)
Discussion started by: prince1987
2 Replies

2. Shell Programming and Scripting

Command to delete a word in all files

can anyone tell me what is the commands to delete the below particular word in the all files located in one particular file path files/ll>grep "/ftp/" test.kell ftp -m uskmc -d /ftp/ -i filename.zip output should be : ftp -m uskmc -d -i filename.zip (4 Replies)
Discussion started by: ramkumar15
4 Replies

3. Shell Programming and Scripting

Command to delete half of files in directory.

Hello Friends, I have directory called /tmp. which stores the log files. Whenever it becomes full, i want to delete half of files from all log files. even after deleting the files, if space is more than 90% then it should delete rest of half files. While deleting files, older files... (7 Replies)
Discussion started by: Nakul_sh
7 Replies

4. AIX

Bypass Read Line in profile through AIX command

Hi All, I have a complicated requirement where in I have a "root" user and a user named "xeadmin" I want to take sudo of "xeadmin" by command sudo su - xeadmin. Later i need to hit 2 enter keys as there are 2 read line commands inserted in profile of "xeadmin" and I reach command prompt, i need... (1 Reply)
Discussion started by: hiteshsathawane
1 Replies

5. Shell Programming and Scripting

Find command to delete the files

Hi All, I've created 2 files touch -t 201309101234 aa10 touch -t 201309111234 aa11 Exact 60 days before from today date is SEPT 12th . As per the following command as i gave +60 means the files which were created before sept12th should be deleted find /etc/logs/*aa* -type f -atime +60... (5 Replies)
Discussion started by: smile689
5 Replies

6. UNIX for Dummies Questions & Answers

Find command to delete old files

Hi, I want to delete all the log files that was created on year 2008. My command is not working. Any idea? find . -name '*.log' -mtime 1460 -exec ls -lt {} \; Thank you. (2 Replies)
Discussion started by: samnyc
2 Replies

7. Solaris

Need to know command to delete more than 3 million files from /var/spool/clientmqueue

Hi I need to delete more than 3 million files from /var/spool/clientmqueue. When I give the following command to delete the files, I get the error # pwd /var/spool/clientmqueue # rm -f * /usr/bin/rm: arg list too long Please tell me how can I delete the files (5 Replies)
Discussion started by: sb200
5 Replies

8. Shell Programming and Scripting

Need a command to delete all files apart from last file generated

Hi, I have a directory named (/output). this directory has files in the below format abc.* , xyz*, djj*, iwe*, weewe*, rier*, 3948903ddfgf* these files are generated at random. what i need to do is. delete all the files of all kinds but keep only the last generated file of... (5 Replies)
Discussion started by: dazdseg
5 Replies

9. Solaris

Could not able to delete the files through find command need expert advice

Hi Gurus I am facing a problem, there is a folder called /a where there are lots of files which are occupying space anything between 30 GB to 100 GB as I am not able to check the space occupied by that folder through "du -sh /a" command as I don't see any output after more than 1 hour of... (4 Replies)
Discussion started by: amity
4 Replies

10. Solaris

How to delete Directory and inside files using Find command

I am using the following Command to delete Directory with contents. But this command is deleting inside files only not directories. is there any change need in my command? find -type f -mtime +3 -exec rm -r {} \; Thanks (3 Replies)
Discussion started by: bmkreddy
3 Replies
Login or Register to Ask a Question