Unix shell help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix shell help
# 1  
Old 06-29-2007
Unix shell help

I have some files in a directory. The file names is as file### (Where #### is a number sequence). I want to delete all those files which have sequence less than or equal to 333 in their name.
I created a file which stores the name by using this command

ls -1r file* > list

after this I created this script to delete the files (script is pasted below)

{ while read myline;
do
varnum=$myline | cut -c5-8
echo "File name is : " $varnum
if [ "$varnum" -lt 333 ]; then
echo "Deleting : " $myline
rm $myline

fi
if [ "$varnum" -eq 333 ]; then
echo "Deleting : " $myline
rm $myline
fi

done
} < list

problem is that it is deleting all files with file###, even files which have sequence number greater tha 333. I am new to unix, may be when I am getting the sequence number from the filename, i need to cast that from string to number. If this is the case, i don't know how i can cast that. Your help will be highly appreciated. thanks
# 2  
Old 06-29-2007
Code:
varnum=`echo $myline | cut -c5-8`

# 3  
Old 06-29-2007
Code:
rm $( ls file* | awk ' substr($0,5) <= 333 ' )

# 4  
Old 06-29-2007
With zsh:

Code:
rm file<-333>

# 5  
Old 06-29-2007
Code:
rm file[012]*
rm file3[012]*
rm file33[0123]

# 6  
Old 06-29-2007
Quote:
Originally Posted by Shell_Life
Code:
rm file[012]*
rm file3[012]*
rm file33[0123]

Code:
$ touch file1 file111 file11111
$ ls file[012]*
file1  file111  file11111
$ ls file<-333>
file1  file111

# 7  
Old 06-29-2007
Quote:
touch file1 file111 file11111
Quote:
I have some files in a directory. The file names is as file### ...
Thus, as for the original specification, 'file11111' is not present in the directory.

In any event, if there are files in the same directory not meeting the requirement:
Code:
rm file[012]??
rm file3[012]?
rm file33[0123]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Passing variables from UNIX to Ansible to UNIX shell

I m passing a variable stringg from Unix shell which has value 'Good Day' to ansible and from ansible to a second shell script where it print only Good instead of 'Good Day' passing the variable stringg from unix shell script1.sh echo $stringg ansible-playbook install.yml -i... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

How can i run sql queries from UNIX shell script and retrieve data into text docs of UNIX?

Please share the doc asap as very urgently required. (1 Reply)
Discussion started by: 24ajay
1 Replies

3. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

4. Shell Programming and Scripting

FTP from windows to unix server using unix shell script

Hi, Is it possible to ftp a huge zip file from windows to unix server using unix shell scripting? If so what command i need to use. thanks in advance. (1 Reply)
Discussion started by: Shri123
1 Replies

5. Shell Programming and Scripting

Connecting to multiple unix server from unix server using shell script

Hi Gurus, I'm a unix newbie and I would like to connect to multiple unix servers from unix server using shell script i.e from server a to server b,c,d etc. I want to copy the files from unix server a to server b, c, d. I can access staright using ssh without the need to have password and user... (5 Replies)
Discussion started by: sexyTrojan
5 Replies

6. Shell Programming and Scripting

How to connect unix server to unix server through shell scripts

Hi, I would like to connect UNIX server to UNIX server through shell scripts and i have some concepts . But i am totally confused how to connect UNIX server to UNIX server throuth running a script. It will be greatful to me if any buddy will help me. with simple example please. Thanks in... (2 Replies)
Discussion started by: phatan
2 Replies

7. Shell Programming and Scripting

How to connect unix server to unix server through shell scripts

Hi, I would like to connect UNIX server to UNIX server through shell scripts and i have some concepts . But i am totally confused how to connect UNIX server to UNIX server throuth running a script. It will be greatful to me if any buddy will help me. Thanks in advance. Phatan:) (2 Replies)
Discussion started by: phatan
2 Replies

8. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies
Login or Register to Ask a Question