An Issue with the script which used to remove a file from the current directory.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting An Issue with the script which used to remove a file from the current directory.
# 1  
Old 10-05-2010
An Issue with the script which used to remove a file from the current directory.

Hello forum members,

I am writing a script to two tasks.
1: displaying the list of the files in the current directory.
2: removing the specifed file from the list.
I have written a sample script ,so can u please verfiy and correct.

echo Enter list of files
ls *.txt
read textfile
rm -f $textfile
if [ $textfile ]
then
echo failed to remove file.
else
echo removed sucessfully.
fi

I am looking forward from you.......ASAP.Smilie

Advance thanks,
Siva Ranganath CH.
# 2  
Old 10-05-2010
This is rather simple ...
try below:
Code:
#!/bin/ksh
src=`pwd`
cd $src
rc=`ls -lrt|rm -f $1`
if [[ $rc -eq 0 ]]
then
echo "$1 successfully removed"
else
echo "could not remove $1"
fi

you have to pass teh file name that you want to delete as a parameter to this script and it will be removed
# 3  
Old 10-05-2010
Re-ordering the commands and adding more validation.

Code:
echo "List of files"
ls *.txt 2>/dev/null
echo ""
echo "Enter filename of file to remove"
read textfile

if [ ! -f "${textfile}" ]
then
    echo "File does not exist: ${textfile}"
    exit
fi

rm -f "${textfile}"

if [ -f "${textfile}" ]
then
    echo "Failed to remove file"
else
    echo "Removed sucessfully"
fi

# 4  
Old 10-05-2010
An Issue with the script which used to remove a file from the current directory.

ls -1 *.txt| while read line
do
rm -f $line
if [ -s $line ] ; then
echo "failed to remove"
else
echo "removed successfully"
fi
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display current directory for a running process for script

I'm trying to create a ksh script to do the following: 1) Ask the user what process they want to search for. 2) Have the script read the input as a variable and use it to search for the process. 3) Display the current time & date, the working directory of the process, and finally display the... (6 Replies)
Discussion started by: seekryts15
6 Replies

2. UNIX for Dummies Questions & Answers

Delete .txt file from current directory

I have created few text file during run time by redirecting the txt file echo USER_LIST_"$(date '+%d%h%Y')".csv > report_location.txt report_location.txt is creating in the same location where I run script home/script When I try to remove the txt file at the end of the... (3 Replies)
Discussion started by: stew
3 Replies

3. UNIX for Dummies Questions & Answers

Viewing file size in current directory

What is the command line for viewing the sizes(lines and bytes)of all the files in your present working directory? Is it >ls -la (2 Replies)
Discussion started by: Payton2704
2 Replies

4. Programming

help with printing file names in current directory for C

How do I print all the file names in current directory in C? (6 Replies)
Discussion started by: omega666
6 Replies

5. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies

6. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

7. UNIX for Dummies Questions & Answers

How to find the list of 5 largest file in current directory?

Hello, How to find the list of 5 largest(size wise) file in current directory?i tried using ls -l | sort -t " " -r +5 -6 -n | head -5 but this is not giving correct output as ls -l command gives 1 extra line of output that is how many total files are there!so by running the above... (4 Replies)
Discussion started by: salman4u
4 Replies

8. Solaris

check the current date file in directory

Hi, I am making a script which check the directory and if there is today date file, it is showing message file is there for today date . 1) filename is accessline.win.$timestamp example ;-accessline.win.200712211004 2) On monday i have recieved two file in this directory with current... (2 Replies)
Discussion started by: pallvi
2 Replies

9. Shell Programming and Scripting

Unable to see all file in a current directory

Hi, I am unable to see all files in a current directory when use "ls -lrt" command it is giving error message as below ( I think this current directory is having about 500 files) <CONTROL /home/ckanth/sri>ls -lrt UX:ls: ERROR: Out of memory: Insufficient or invalid memory But when i... (3 Replies)
Discussion started by: srikanthus2002
3 Replies

10. Programming

Finding largest file in current directory?

I was hoping to get some assistance with this C program I am working on. The goal is to find the largest file in the current directory and then display this filename along with the filesize. What I have so far will display all the files in the current directory. But, how do I deal with "grabbing"... (1 Reply)
Discussion started by: AusTex
1 Replies
Login or Register to Ask a Question