Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Script to change/find/delete/install a specific file Post 302270264 by matrixmadhan on Saturday 20th of December 2008 01:23:42 PM
Old 12-20-2008
Quote:
Originally Posted by Great Uncle Kip
All the help so far has been cracking, cheers all!

One last thing i'm just wondering.....

Is there a command where I can check if a directory exists, before removing it? If it does, I want the script to remove it and the proceed to the reinstall. If not, I want it to create it and proceed to the install?

Regards
Kip
Code:
if  [  -d $dir_name ]
then
  echo "dir $dir_name is available"
else
  echo "dir $dir_name is not available"
fi

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change a specific character in a file

Hi, I have a data file with following structure: a|b|c|d|3|f1|f2|f3 a|b|c|d|5|f1|f2|f3|f4|f5 I want to change this data to: a|b|c|d|3|f1;f2;f3 a|b|c|d|5|f1;f2;f3;f4;f5 Data in column 5 tells the number of following fields. All fields delimiter after the 5th column needs to be... (6 Replies)
Discussion started by: sdubey
6 Replies

2. UNIX for Dummies Questions & Answers

Change Specific Line of a File

Hi everyone, I am attempting to do something that should be very simple. How do I replace a specific line of a file with different text, and then save that file to its original name? I believe I want to use the sed command with the c option, but I after trying many times, I can't get the right... (10 Replies)
Discussion started by: msb65
10 Replies

3. Shell Programming and Scripting

Change specific ip address in a file

I need to change a line from a xen cfg file using sed if it's possible. The original line is: vif = I want to change ONLY the IP address of the second part ==> ip=10.1.10.4 to another IP --> ip=192.222.11.6 The first one ip=123.456.789.123 keeps untouchable. My new line shoud... (9 Replies)
Discussion started by: iga3725
9 Replies

4. Shell Programming and Scripting

Shell script to find specific file name and load data

I need help as to how to write a script in Unix for the following: We have 3 servers; The mainframe will FTP them to a folder. In that folder we will need the script to look and see if the specific file name is there and load it to the correct table. Can anyone pls help me out with... (2 Replies)
Discussion started by: msrahman
2 Replies

5. Shell Programming and Scripting

how to change specific value for a entry in the file

Hello All, can someone please suggest me a one line command to change a specific value that is associated to an entry in the file. for example #more schedulefile quartz.job.manual.bonus.schedule=0 0 9 ? * * # it should be changed to #more schedulefile... (5 Replies)
Discussion started by: bobby320
5 Replies

6. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

7. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

8. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

9. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies
opendir(3)						     Library Functions Manual							opendir(3)

NAME
opendir, readdir, readdir_r, telldir, seekdir, rewinddir, closedir - Performs operations on directories LIBRARY
Standard C Library (libc) SYNOPSIS
#include <sys/types.h> #include <dirent.h> DIR *opendir ( const char *dir_name ); struct dirent *readdir ( DIR *dir_pointer ); int readdir_r ( DIR *dir_pointer, struct dirent *entry, struct dirent **result); long telldir ( DIR *dir_pointer ); void seekdir ( DIR *dir_pointer, long location ); void rewinddir ( DIR *dir_pointer ); int closedir ( DIR *dir_pointer ); The following function does not conform to current industry standards and is supported only for backward compatibility: int readdir_r ( DIR *dir_pointer, struct dirent *result ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: closedir(): POSIX.1, XPG4, XPG4-UNIX opendir(): POSIX.1, XPG4, XPG4-UNIX readdir(): POSIX.1c, XPG4, XPG4-UNIX readdir_r(): POSIX.1c rewinddir(): POSIX.1, XPG4, XPG4-UNIX seekdir(): POSIX.1, XPG4, XPG4-UNIX telldir(): POSIX.1, XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Names the directory. If the final component of dir_name names a symbolic link, the link will be traversed and pathname resolution will continue. Points to the dir structure of an open directory. Points to the storage location that will hold the entry. Specifies the num- ber of an entry relative to the start of the directory. Contains the next directory entry on return from the readdir_r() function. Set equal to entry upon successful completion or to NULL on error or end-of-directory. DESCRIPTION
The opendir() function opens the directory designated by the dir_name parameter and associates a directory stream with it. The directory stream is positioned at the first entry. The type DIR, which is defined in the dirent.h header file, represents a directory stream, which is an ordered sequence of all the directory entries in a particular directory. If a file descriptor is used, the FD_CLOEXEC flag will be set on that file descriptor. The opendir() function also returns a pointer to identify the directory stream in subsequent operations. The null pointer is returned when the directory named by the dir_name parameter cannot be accessed or when not enough memory is available to hold the entire stream. The type DIR, which is defined in the dirent.h header file, represents a directory stream, which is an ordered sequence of all the direc- tory entries in a particular directory. Directory entries represent files; files may be removed from a directory or added to a directory asynchronously to the operation of the readdir() function. The readdir() function returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the dir_pointer parameter, and positions the directory stream at the next entry. It returns a null pointer upon reaching the end of the directory stream. The dirent structure defined in the dirent.h header file describes a directory entry. The readdir() function will not return directory entries containing empty names. If entries for . (dot) or .. (dot-dot) exist, one entry will be returned for . (dot) and one entry will be returned for .. (dot-dot); otherwise, they will not be returned. The pointer returned by the readdir() function points to data which may be overwritten by another call to readdir() on the same directory stream. This data will not be overwritten by another call to readdir() on a different directory stream. If a file is removed from or added to the directory after the most recent call to the opendir() or rewinddir() function, whether a subse- quent call to the readdir() function returns an entry for that file is unspecified. The readdir() function may buffer several directory entries per actual read operation; the readdir() function marks for update the st_atime field of the directory each time the directory is actually read. When it reaches the end of the directory, or when it detects an invalid seekdir() operation, the readdir() function returns the null value. The telldir() function returns the current location associated with the specified directory stream. The seekdir() function sets the position of the next readdir() operation on the directory stream specified by the dir_pointer parameter to the position specified by the location parameter. If the value of the location parameter was not returned by a call to the telldir() function, or if there was an intervening call to the rewinddir() function on this directory stream, the effect is undefined. The new position reverts to the one associated with the directory stream when the telldir() operation was performed. An attempt to seek to an invalid location causes the readdir() function to return the null value the next time it is called. The position should be that returned by a previous telldir() function call. The rewinddir() function resets the position of the specified directory stream to the beginning of the directory. It also causes the directory stream to refer to the current state of the corresponding directory, as a call to the opendir() function would have done. If the dir_pointer parameter does not refer to a directory stream, the effect is undefined. The closedir() function closes a directory stream and frees the structure associated with the dir_pointer parameter. Upon return, the value of dir_pointer may no longer point to an accessible object of the type DIR. If a file descriptor is used to implement type DIR, that file descriptor will be closed. [POSIX] The readdir_r() function is the reentrant version of the readdir() function. The readdir_r() function stores the next directory entry at entry, and returns entry in result. On end-of-directory, NULL is stored in result and 0 (zero) is returned. NOTES
An open directory must always be closed with the closedir() function to ensure that the next attempt to open that directory is successful. The use of the seekdir() and telldir() functions is not recommended in the Tru64 UNIX operating system, as the results can be unpre- dictable. [POSIX] The readdir function is not supported for multithreaded applications. Instead, its reentrant equivalent, readdir_r, should be used with multiple threads. EXAMPLES
To search a directory for the entry name: len = strlen(name); dir_pointer = opendir("."); for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dir_pointer); return FOUND; } closedir(dir_pointer); return NOT_FOUND; RETURN VALUES
Upon successful completion, the opendir() function returns a pointer to an object of type DIR. Otherwise, null is returned and errno set to indicate the error. Upon successful completion, the readdir() function returns a pointer to an object of type struct dirent. When an error is encountered, a null pointer is returned and errno is set to indicate the error. When the end of the directory is encountered, a null pointer is returned and errno is not changed. Upon successful completion, the telldir() function returns the current location. Otherwise, -1 is returned. Upon successful completion, the closedir() function returns 0 (zero). Otherwise, -1 is returned. [POSIX] Upon successful completion or end-of-directory, the readd_r() function returns 0 (zero). Otherwise, an error number is returned. [Tru64 UNIX] Upon successful completion, the obsolete version of the readdir_r() function returns 0 (zero). Otherwise, -1 is returned. ERRORS
If the opendir() function fails, errno may be set to one of the following values: Search permission is denied for any component of dir_name or read permission is denied for dir_name. Too many links were encountered in translating dir_name. The length of the dir_name string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX. The dir_name parameter points to the name of a file that does not exist, or the parameter points to an empty string. A component of dir_name is not a directory. RELATED INFORMATION
Functions: close(2), lseek(2), open(2), read(2), scandir(3) Standards: standards(5) delim off opendir(3)
All times are GMT -4. The time now is 08:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy