Remove original file from directory after bash executes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove original file from directory after bash executes
# 8  
Old 08-24-2016
As Chubler_XL already pointed out, the
Code:
find . -type f -iname \*.txt -delete

will delete ALL .txt files INCLUDING the original ones, UNLESS e.g. permissions get into the way, which in turn would result in error messages. BTW, the find ... for "_remove" and "_index" are unnecessary when above is run first.
This User Gave Thanks to RudiC For This Post:
# 9  
Old 08-24-2016
Try
Code:
awk -F'\t' -v OFS='\t' '

/^#/            {next
                }

FNR == 1        {Q = NF
                 if (FN) close (FN)

                 print "echo rm " FILENAME

                 FN = FILENAME
                 sub (/\./, "_final.", FN)
                }

                {for (i=1; i<=Q; i++) if (!$i) $i="."
                 print FNR==1?"R_Index":FNR-1, $0 > FN
                }
' OFS="\t" /home/cmccabe/Desktop/microarray/*.txt  | sh

in lieu of your original bash script in post#1. Unfortunately, I can't test it, but it should create the desired output files (..._final...) with the desired index in front of every single line, and delete the original files (if the "echo" is removed).

Please report on success or failure...
This User Gave Thanks to RudiC For This Post:
# 10  
Old 08-24-2016
@RudiC the awk works great, thank you Smilie. I am more-or-less seeing how it works, much more efficient.
# 11  
Old 08-24-2016
Also untested, but with no need for find (unless you also want to process files in subdirectories) or awk:
Code:
for file in /home/cmccabe/Desktop/microarray/*.txt
do	[ "${file%_final.txt}" != "$file" ] && continue
	echo rm "$file"
done

or, more efficiently,
Code:
cd /home/cmccabe/Desktop/microarray/ &&
for file in *.txt
do	[ "${file%_final.txt}" != "$file" ] && continue
	echo rm "$file"
done

And, in both case remove the echo if the list of commands displayed matches your expectations.

Last edited by Don Cragun; 08-24-2016 at 03:29 PM.. Reason: Add caveat on the need for using the find utility.
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 08-25-2016
Thank you @Don Cragun for another learning approach, I appreciate it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Cant remove directory on bash script

hi all, this is my script and as you can see in the screenshot i attach its not deleting the directory in the source folder #!/bin/bash cd /vol/cha-work/_ARCHIVE/to_be_archived/audio/robert_test temp=/mnt/robert_test/temp dest=/vol/cha-archive/audio echo "is this archive for an... (2 Replies)
Discussion started by: robertkwild
2 Replies

2. Shell Programming and Scripting

Scan and remove if file infected using bash

The below bash runs clamav on all files in DIR and produces virus-scan.log. My question is the portion in bold is supposed to move the infected files, lines not OK, to /home/cmccabe/quarantine. Does the bash look correct? Thank you :). virus-scan.log Mon Jan 16 14:39:05 CST 2017... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Bash executes first part of script but not second

My bash below verifies the integrity of all .bam in a directory and writes the out output to a .txt file. That is part one of the script that works. The second part of the bash searches each one of the .txt files for a string "(SUCCESS)" and if found display a message and if it is not found... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

How to remove '^[[00m' in bash file?

Hi, This should be a simple one: I run the following commands in bash and ksh respectively but got differenant results: # ls -l /var/log > /tmp/a # vi /tmp/a In bash shell, I got: ^ In ksh, I got: total 828552 -rw-r----- 1 root root 189 Aug 9 00:00 acpid -rw-r----- 1 root... (7 Replies)
Discussion started by: aixlover
7 Replies

5. Shell Programming and Scripting

Remove duplicates and update last 2 digits of the original row with 0's

Hi, I have a requirement where I have to remove duplicates from a file based on the first 8 chars (It is fixed width file of 10 chars length) and whenever a duplicate row is found, its original row's last 2 chars should be updated to all 0's. I thought of using sort -u -k 1.1,1.8... (4 Replies)
Discussion started by: farawaydsky
4 Replies

6. UNIX for Dummies Questions & Answers

Remove part of file names in a directory

Hi, i have a directory full of pictures, .jpg files. Half of them begin with "beach_" (for ex beach_123_123456.jpg) i'm looking for a command to remove the "beach_" from all files in the directory. thanks (4 Replies)
Discussion started by: robertmanalio
4 Replies

7. UNIX for Advanced & Expert Users

rsync deletes original directory

Hi I have a strange problem. Sometimes when I execute the below command something wierd happens. rsync -avz -e ssh travegre@travegre.net: ../travegre.net/ the folder named "hm" that is held in travegre.net and is coppied along with all the other folders and data at travegre.net, gets deleted... (4 Replies)
Discussion started by: travegre
4 Replies

8. Solaris

Remove the zfs snapshot keeping the original volume and clone

I created a snapshot and subsequent clone of a zfs volume. But now i 'm not able to remove the snapshot it gives me following error zfs destroy newpool/ldom2/zdisk4@bootimg cannot destroy 'newpool/ldom2/zdisk4@bootimg': snapshot has dependent clones use '-R' to destroy the following... (7 Replies)
Discussion started by: fugitive
7 Replies

9. UNIX for Dummies Questions & Answers

Remove n lines from every file in a directory

Hello, I would like to remove n lines from all the files in a given directory. If it were one file, I can use the sed 'start, finish d' command. How can I extend this to an entire directory of files? Thanks, Gussi. (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

10. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies
Login or Register to Ask a Question