Can not remove file using rm command

I have two questions:

the first is I have a line of code:

 printf "What is the id of the patient getting GJB2 analysis  : "; read id 

that stores a user input in a variable $id in the python directory c:/Users/cmccabe/Desktop/Python27/$id.txt

Using rm I get the error cannot remove $id.txt no such file. The exact command is:

 
cd 'C:'
rm  c:/Users/cmccabe/Desktop/Python27/${id}.txt

I have also tried

 
cd 'C:'
rm  c:/Users/cmccabe/Desktop/Python27/"${id}".txt 

I will post the second question in a different theard. Thank you :slight_smile:

  1. what's the value of ${id} variable?
  2. If id=foo , does the file c:/Users/cmccabe/Desktop/Python27/foo.txt exist?

what is "cd c:"?

The value of ${id}.txt is snp, so snp.txt is the value and I see that in the python directory and can manually delete it, but using rm I can not. Thank you :slight_smile:

Python is in a windows directory so I do cd to c:/.

usually it has to do with the path and/or the file name being correct or permissions.
Add set -x to your shell script and see what shell script is trying to do.
Good luck.

1 Like

may be you should try del instead of rm? :wink:

1 Like

For some reason the value for the ${id} variable, in this case snp, isn't getting passed. I'm not sure why but in other functions I use the variable. Thank you :slight_smile:

Hi

At no point i see the file $id.txt created.
I only see you read the the user input into the variable id.
Just because there is a variable $id , doesnt mean there is a file $id.txt

Anway, was the "file not found" message regarding $id.txt or rm ?

gn8

${id}.txt is not found but I see the file in the directory, rm can't find it. Thank you :).

What, exactly, is the diagnostic message produced by rm ?

What is the name of the file you are trying to remove?

What output do you get from the command line?:

cd c:/Users/cmccabe/Desktop/Python27; ls | od -bc
1 Like

The error in rm is:

 Removing old files, please wait rm: cannot remove 'c:/Users/cmccabe/Desktop/Pyth
on27/.txt': No such file or directory 

The file name, in this case is del.txt and it is in the Python directory and I can manually remove it.

 
cd c:/Users/cmccabe/Desktop/Python27; ls | od -bc
0000000 144 145 154 056 164 170 164 012 104 114 114 163 012 104 157 143
          d   e   l   .   t   x   t  \n   D   L   L   s  \n   D   o   c
0000020 012 151 156 143 154 165 144 145 012 114 151 142 012 154 151 142
         \n   i   n   c   l   u   d   e  \n   L   i   b  \n   l   i   b
0000040 163 012 114 111 103 105 116 123 105 056 164 170 164 012 116 105
          s  \n   L   I   C   E   N   S   E   .   t   x   t  \n   N   E
0000060 127 123 056 164 170 164 012 157 165 164 056 164 170 164 012 160
          W   S   .   t   x   t  \n   o   u   t   .   t   x   t  \n   p
0000100 171 164 150 157 156 056 145 170 145 012 160 171 164 150 157 156
          y   t   h   o   n   .   e   x   e  \n   p   y   t   h   o   n
0000120 167 056 145 170 145 012 122 105 101 104 115 105 056 164 170 164
          w   .   e   x   e  \n   R   E   A   D   M   E   .   t   x   t
0000140 012 162 165 156 137 142 141 164 143 150 137 152 157 142 056 160
         \n   r   u   n   _   b   a   t   c   h   _   j   o   b   .   p
0000160 171 012 123 143 162 151 160 164 163 012 164 143 154 012 124 157
          y  \n   S   c   r   i   p   t   s  \n   t   c   l  \n   T   o
0000200 157 154 163 012 167 071 170 160 157 160 145 156 056 145 170 145
          o   l   s  \n   w   9   x   p   o   p   e   n   .   e   x   e
0000220 012
         \n
0000221 

Thank you :).

Obviously, $id is empty in above rm , and, unless $id were one of "del", "LICENSE", "NEWS", "out", or "README", rm wouldn't find it in that directory anyway.

$id has not been set here as can be seen from the part in red color.
It hasn't been set to "del".

The output that you posted shows that the following files are present in your python directory:

del.txt
DLLs
Doc
include
Lib
libs
LICENSE.txt
NEWS.txt
out.txt
python.exe
pythonw.exe
README.txt
run_batch_job.py
Scripts
tcl
Tools
w9xpopen.exe

So, yes, "del.txt" is there but because "id" hasn't been set to "del", your shell script cannot remove the file.

You may want to add a "set -x" or "set -ex" at the top of your Bash script right below the shebang line to trace its execution.

1 Like

del.txt is the file to be removed but the ${id} has not been set and I don't know why. Thank you :).

rm c:/Users/cmccabe/Desktop/Python27/"${id}".txt
rm: cannot remove �c:/Users/cmccabe/Desktop/Python27/.txt�: No such file or directory

---------- Post updated at 08:24 AM ---------- Previous update was at 08:23 AM ----------

I will add the set -x or set -ex

---------- Post updated at 08:36 AM ---------- Previous update was at 08:24 AM ----------

set -x

 
+ printf 'Removing old files, please wait '
Removing old files, please wait 
+ rm c:/Users/cmccabe/Desktop/Python27/.txt
rm: cannot remove 'c:/Users/cmccabe/Desktop/Python27/.txt': No such file or dire
ctory
+ cd C:
+ rm c:/Users/cmccabe/Desktop/Python27/out_name.txt

Looks likr rm cant find the file that is the variable but can find the other file. Thank you :slight_smile:

I'd recommend to trace the script portion before rm , where id is supposed to be set.

It looks like the variable $id is being read but not set by the line in bold.... that might be the problem. How do I read and set this variable? Thank you :).

 gjb2() {
    printf "\n\n"
    printf "What is the id of the patient getting GJB2 analysis  : "; read id
	 	
    printf "Enter variant(s): "; IFS="," read -a variant
        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf "NM_004004.5:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/$id.txt
        done
	add2text ${id}.txt
	cd 'C:' C:/Users/cmccabe/Desktop/Python27/
	printf "NM_004004.5:%s\n" "${variant}" >> c:/Users/cmccabe/Desktop/Python27/$id.txt >> c:/Users/cmccabe/Desktop/Python27/out.txt
	additionalg
} 

Check if id is empty?

printf "What is the id of the patient getting GJB2 analysis  : "; read id
[ -z "$id" ] && echo "Must provide an ID to work with!" && exit 1

hth

1 Like
+ printf 'What is the id of the patient getting GJB2 analysis  : '
What is the id of the patient getting GJB2 analysis  : + read id
del
+ '[' -z del ']' 

Looks like id has a value but would read and set the id?:

printf "What is the id of the patient getting GJB2 analysis  : "; read id
[ -z "$id" ] && echo -n >> cd 'C:' C:/Users/cmccabe/Desktop/Python27/
[ -z "$id" ] && echo "Must provide an ID to work with!" && exit 1 

Thank you :).

OK, trace everything from reading $id until the rm .

I can do everything except for the rm and run the perl , but that may be related. Thank you :).

+ cd 'C:\Users\cmccabe\Desktop\annovar'
+ echo -n ''
+ cd C: C:/Users/cmccabe/Desktop/Python27/
+ echo -n ''
+ menu
+ true
+ printf '\n Welcome to target gene annotation, please make a selection from the
 MENU \n
        ==================================\n\n
        \t 1  GJB2 analysis\n
        \t 2  MECP2 analysis\n
        \t 3  Phox2B analysis\n
        \t 4  Exit\n\n
        ==================================\n\n'

 Welcome to target gene annotation, please make a selection from the MENU

        ==================================


                 1  GJB2 analysis

                 2  MECP2 analysis

                 3  Phox2B analysis

                 4  Exit


        ==================================

+ printf '\t Your choice: '
         Your choice: + read menu_choice
1
+ case "$menu_choice" in
+ gjb2
+ printf '\n\n'


+ printf 'What is the id of the patient getting GJB2 analysis  : '
What is the id of the patient getting GJB2 analysis  : + read id
del
+ '[' -z del ']'
+ printf 'Enter variant(s): '
Enter variant(s): + IFS=,
+ read -a variant
c.79G>A
+ '[' -z del ']'
+ '[' del = end ']'
+ (( i=0 ))
+ (( i<1 ))
+ printf 'NM_004004.5:%s\n' 'c.79G>A'
+ (( i++ ))
+ (( i<1 ))
+ add2text del.txt
+ cd 'C:\Users\cmccabe\Desktop\annovar'
+ echo del.txt
+ cd C: C:/Users/cmccabe/Desktop/Python27/
+ printf 'NM_004004.5:%s\n' 'c.79G>A'
+ additionalg
+ printf '\n\n'


+ printf 'Are there additonal GJB2 patients to be analyzed?  Y/N '
Are there additonal GJB2 patients to be analyzed?  Y/N + read match_choice
n
+ case "$match_choice" in
+ id=del
+ gjb2name
+ printf '\n\n'


+ cd C:
+ C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27
/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Des
ktop/Python27/out_name.txt NameChecker
+ check
+ printf '\n\n'


+ awk 'NR>1 { if ($2 ~ /^\(/ ) {$1=""; print "Found error: ", $0} else { sub(/.*
:/, "", $1); sub(/.*:/, "", $7); print "No error: " $1 "," $7}}' C:/Users/cmccab
e/Desktop/Python27/out_name.txt
No error: c.79G>A,p.(Val27Ile)
+ printf 'Is the variant correct?  Y/N '
Is the variant correct?  Y/N + read match_choice
y
+ case "$match_choice" in
+ id=del
+ position
+ printf '\n\n'


+ cd C:
+ C:/Users/cmccabe/Desktop/Python27/python.exe C:/Users/cmccabe/Desktop/Python27
/run_batch_job.py C:/Users/cmccabe/Desktop/Python27/out.txt C:/Users/cmccabe/Des
ktop/annovar/out_position.txt PositionConverter
+ parse
+ printf '\n\n'


+ cd 'C:\Users\cmccabe\Desktop\annovar'
+ perl -ne 'next if $. == 1;
            while (/\t*NC_(\d+)\.\S+g\.(\d+)(\S+)/g) {
                  # conditional parse
                ($num1, $num2, $common) = ($1, $2, $3);
                $num3 = $num2;
                if    ($common =~ /^([A-Z])>([A-Z])$/)   { ($ch1, $ch2) = ($1, $
2) }              # SNP
                elsif ($common =~ /^del([A-Z])$/)        { ($ch1, $ch2) = ($1, "
-") }             # deletion
                elsif ($common =~ /^ins([A-Z])$/)        { ($ch1, $ch2) = ("-",
$1) }             # insertion
                elsif ($common =~ /^_(\d+)del([A-Z]+)$/) { ($num3, $ch1, $ch2) =
 ($1, $2, "-") }  # multi deletion
                elsif ($common =~ /^_(\d+)ins([A-Z]+)$/) { ($num3, $ch1, $ch2) =
 ($1, "-", $2) }  # multi insertion
                printf ("%d\t%d\t%d\t%s\t%s\n", $num1, $num2, $num3, $ch1, $ch2)
;                 # output
                map {undef} ($num1, $num2, $num3, $common, $ch1, $ch2);
            }
           ' out_position.txt
+ annovar
+ printf '\n\n'


+ printf 'How many patients would you like to annotate  : '
How many patients would you like to annotate  : + read id
1
+ '[' -z 1 ']'
+ '[' 1 = end ']'
+ cd 'C:\Users\cmccabe\Desktop\annovar'
+ awk '{close(fname)} (getline fname<f)>0 {print>fname}' f=target.txt out_parse.
txt
++ perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -p
rotocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -opera
tion g,f,f,f,f,f -otherinfo")'
-----------------------------------------------------------------
NOTICE: Processing operation=g protocol=refGene

NOTICE: Running with system command <annotate_variation.pl -geneanno -buildver h
g19 -dbtype refGene -outfile del.txt.refGene -exonsort del.txt humandb/>
NOTICE: Reading gene annotation from humandb/hg19_refGene.txt ... Done with 251
transcripts (including 5 without coding sequence annotation) for 106 unique gene
s
NOTICE: Reading FASTA sequences from humandb/hg19_refGeneMrna.fa ... Done with 1
 sequences
NOTICE: Finished gene-based annotation on 1 genetic variants in del.txt
NOTICE: Output files were written to del.txt.refGene.variant_function, del.txt.r
efGene.exonic_variant_function
-----------------------------------------------------------------
NOTICE: Processing operation=f protocol=popfreq_all

NOTICE: Running system command <annotate_variation.pl -filter -dbtype popfreq_al
l -buildver hg19 -outfile del.txt del.txt humandb/ -otherinfo>
NOTICE: the --dbtype popfreq_all is assumed to be in generic ANNOVAR database fo
rmat
NOTICE: Variants matching filtering criteria are written to del.txt.hg19_popfreq
_all_dropped, other variants are written to del.txt.hg19_popfreq_all_filtered
NOTICE: Processing next batch with 1 unique variants in 1 input lines
NOTICE: Database index loaded. Total number of bins is 2816654 and the number of
 bins to be scanned is 1
NOTICE: Scanning filter database humandb/hg19_popfreq_all.txt...Done
-----------------------------------------------------------------
NOTICE: Processing operation=f protocol=common

NOTICE: Running system command <annotate_variation.pl -filter -dbtype common -bu
ildver hg19 -outfile del.txt del.txt humandb/>
NOTICE: the --dbtype common is assumed to be in generic ANNOVAR database format
NOTICE: Variants matching filtering criteria are written to del.txt.hg19_common_
dropped, other variants are written to del.txt.hg19_common_filtered
NOTICE: Processing next batch with 1 unique variants in 1 input lines
NOTICE: Scanning filter database humandb/hg19_common.txt...Done
-----------------------------------------------------------------
NOTICE: Processing operation=f protocol=clinvar

NOTICE: Running system command <annotate_variation.pl -filter -dbtype clinvar -b
uildver hg19 -outfile del.txt del.txt humandb/>
NOTICE: the --dbtype clinvar is assumed to be in generic ANNOVAR database format

NOTICE: Variants matching filtering criteria are written to del.txt.hg19_clinvar
_dropped, other variants are written to del.txt.hg19_clinvar_filtered
NOTICE: Processing next batch with 1 unique variants in 1 input lines
NOTICE: Scanning filter database humandb/hg19_clinvar.txt...Done
-----------------------------------------------------------------
NOTICE: Processing operation=f protocol=clinvarsubmit

NOTICE: Running system command <annotate_variation.pl -filter -dbtype clinvarsub
mit -buildver hg19 -outfile del.txt del.txt humandb/>
NOTICE: the --dbtype clinvarsubmit is assumed to be in generic ANNOVAR database
format
NOTICE: Variants matching filtering criteria are written to del.txt.hg19_clinvar
submit_dropped, other variants are written to del.txt.hg19_clinvarsubmit_filtere
d
NOTICE: Processing next batch with 1 unique variants in 1 input lines
NOTICE: Scanning filter database humandb/hg19_clinvarsubmit.txt...Done
-----------------------------------------------------------------
NOTICE: Processing operation=f protocol=clinvarreference

NOTICE: Running system command <annotate_variation.pl -filter -dbtype clinvarref
erence -buildver hg19 -outfile del.txt del.txt humandb/>
NOTICE: the --dbtype clinvarreference is assumed to be in generic ANNOVAR databa
se format
NOTICE: Variants matching filtering criteria are written to del.txt.hg19_clinvar
reference_dropped, other variants are written to del.txt.hg19_clinvarreference_f
iltered
NOTICE: Processing next batch with 1 unique variants in 1 input lines
NOTICE: Scanning filter database humandb/hg19_clinvarreference.txt...Done
-----------------------------------------------------------------
NOTICE: Multianno output file is written to del.txt.hg19_multianno.txt
+ printf 'The annotation is complete, would you like analyze additional target g
ene patients? Y/N '
The annotation is complete, would you like analyze additional target gene patien
ts? Y/N + read match_choice
n
+ case "$match_choice" in
+ id=
+ remove
+ printf '\n\n'


+ printf 'Removing old files, please wait '
Removing old files, please wait + rm 'C:\Users\cmccabe\Desktop\annovar\out_posit
ion.txt'
+ rm 'C:\Users\cmccabe\Desktop\annovar\out_parse.txt'
+ cd C:
+ rm c:/Users/cmccabe/Desktop/Python27/.txt
rm: cannot remove 'c:/Users/cmccabe/Desktop/Python27/.txt': No such file or dire
ctory
+ cd C:
+ rm c:/Users/cmccabe/Desktop/Python27/out_name.txt
+ printf '\n Old files removed, formatting for matrix '

 Old files removed, formatting for matrix + matrix
+ cd 'C:\Users\cmccabe\Desktop\annovar'
+ 'C:\Users\cmccabe\Desktop\annovar\matrix.pl'
c:\cygwin\home\cmccabe\bashparse.sh: line 157: .txt.hg19_multianno.txt: No such
file or directory
+ printf 'Process complete and new file saved in, Are there additional target ge
ne patients? Y/N '
Process complete and new file saved in, Are there additional target gene patient
s? Y/N + read match_choice

---------- Post updated at 10:12 AM ---------- Previous update was at 09:50 AM ----------

The below is what I am trying to remove. Is it not $id.txt in rm ?

printf "NM_004004.5:%s\n" ${variant[$i]} >> c:/Users/cmccabe/Desktop/Python27/$id.txt