-1

I have this code, which has to find all .txt files and remove them

#!/bin/bash
find /home/user_name -name "*.txt" | xargs rm

Is it correct ? How can I archive folder after that ?

  • Don't use `xargs` unless you understand the consequences. `find` has a delete option: `find /home/user_name -name "*.txt" -delete`. Use it! The best way to find out how to archive the folder is to google "How to archive folder linux" – hek2mgl Jun 28 '17 at 08:41

1 Answers1

1
#!/bin/bash
find /home/user_name -type f -name "*.txt" -exec rm {} \;
# for your specific use case, you could also do
# find /home/user_name -type f -name "*.txt" -delete
# as @hek2mgl pointed out.
# You can have a more restricted search by using -type f
# You can archive the folder using the zip command like below
zip -r user_name.zip /home/user_name

Note: Read the manpage of zip to see the usage of -r parameter

sjsam
  • 21,411
  • 5
  • 55
  • 102