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
# 1  
Old 08-23-2016
Remove original file from directory after bash executes

The below bash works great, except I can not seem to delete the original file $f from the directory. Thank you Smilie

For example, after the bash executes there are 8 files in the directory:
Code:
123.txt (original file)
123_remove.txt
123_index.txt
123_final.txt
456.txt (original file)
456_remove.txt
456_index.txt
456_final.txt

Bash
Code:
for f in /home/cmccabe/Desktop/microarray/*.txt; do
     bname=$(basename "$f")
     pref=${bname%%.txt}
     sed /^#/d "$f" > /home/cmccabe/Desktop/microarray/"${pref}"_remove.txt # strip off all lines with #
     awk -F'\t' -v OFS='\t' '{$0=((NR==1) ? "R_Index" : (NR - 1)) OFS $0} 1' /home/cmccabe/Desktop/microarray/"${pref}"_remove.txt > /home/cmccabe/Desktop/microarray/"${pref}"_index.txt # add R_Index
     awk -F'\t' 'NR==1{Q=NF;print} NR>1{for(i=1;i<=Q;i++){if(!$i){$i="."}};print}' OFS="\t" /home/cmccabe/Desktop/microarray/"${pref}"_index.txt > /home/cmccabe/Desktop/microarray/"${pref}"_final.txt # replace null values with .
done

cd /home/cmccabe/Desktop/microarray
find . -type f -iname \*.txt -delete (removes all .txt files)
find . -type f -iname \*_remove.txt -delete
find . -type f -iname \*_index.txt -delete

desired output in directory
Code:
123_final.txt
456)final.txt


Last edited by cmccabe; 08-23-2016 at 06:46 PM.. Reason: fixed format
# 2  
Old 08-23-2016
You're missing the point. The two files you haven't removed with your script are named 123.txt (original file) and 456.txt (original file). Those names end with the string file) not with the string .txt.

To get rid of them, try:
Code:
rm -f *'.txt (original file)'

or:
Code:
rm -f *'.txt '*

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 08-23-2016
The file names are 123.txt and 456.txt, I only added the (original file) to show what was not being removed. Thank you Smilie.
# 4  
Old 08-23-2016
Adding comments to what appears to be output from the command ls -1 (that is a digit 1, not the letter lowercase el) and adding comments only confuses those of us trying to help you.

Please show us the output you get from the commands:
Code:
ls -1

and:
Code:
ls -1|od -bc

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-23-2016
You should be careful of:

Code:
find . -type f -iname \*.txt -delete

As this will also remove 123_final.txt and 345_final.txt files as they match the \*.txt pattern.

perhaps this is closer to what you are after:

Code:
find . -type f -iname \*.txt ! -iname \*_final.txt -delete

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 08-24-2016
As usual - what's the real result of that operation and what be the error messages?

You may know - or suspect at least - that your approach is overly complicated?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 08-24-2016
Here is the output of the commands @Don Cragun. Thank you Smilie.

ls -1
Code:
123_final.txt
123_index.txt
123_remove.txt
123.txt
456_final.txt
456_index.txt
456_remove.txt
456.txt

ls -1|od -bc
Code:
0000000 061 062 063 137 146 151 156 141 154 056 164 170 164 012 061 062
          1   2   3   _   f   i   n   a   l   .   t   x   t  \n   1   2
0000020 063 137 151 156 144 145 170 056 164 170 164 012 061 062 063 137
          3   _   i   n   d   e   x   .   t   x   t  \n   1   2   3   _
0000040 162 145 155 157 166 145 056 164 170 164 012 061 062 063 056 164
          r   e   m   o   v   e   .   t   x   t  \n   1   2   3   .   t
0000060 170 164 012 064 065 066 137 146 151 156 141 154 056 164 170 164
          x   t  \n   4   5   6   _   f   i   n   a   l   .   t   x   t
0000100 012 064 065 066 137 151 156 144 145 170 056 164 170 164 012 064
         \n   4   5   6   _   i   n   d   e   x   .   t   x   t  \n   4
0000120 065 066 137 162 145 155 157 166 145 056 164 170 164 012 064 065
          5   6   _   r   e   m   o   v   e   .   t   x   t  \n   4   5
0000140 066 056 164 170 164 012

---------- Post updated at 08:15 AM ---------- Previous update was at 08:13 AM ----------

I will give the
Code:
find . -type f -iname \*.txt ! -iname \*_final.txt -delete

Thank you @Chubler_XL Smilie

@RudiC there was no error as the bash did execute just didn't remove all the desired files, but I will try out the posted command. You are probably right in that there is less complicated/more optimal approach to this, but as I am a scientist I still have much too learn about shell scripting and being more efficient, but I am learning. Thank you Smilie.

Last edited by cmccabe; 08-24-2016 at 10:22 AM.. Reason: added details
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