Sponsored Content
Full Discussion: Finding and replacing links
Top Forums Shell Programming and Scripting Finding and replacing links Post 302466149 by DGPickett on Monday 25th of October 2010 04:49:07 PM
Old 10-25-2010
Well, if you mean sym-links (soft links), what about "find ... -type l" to find them, and either "ls -l" or the find '-ls' option if available (not on old hpux) to see the destination?

With relative pathed links, to resolve them to a hard path I use realpath, a wrapper for realpath():
Code:
$ cat mysrc/realpath.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

int main( int argc, char **argv ){
        char    buf[PATH_MAX+1];
        char    obuf[PATH_MAX+1];
        char    *bp ;
        char    *cp ;
        int     i = 1 ;
        int     both = 0 ;
        int     cmdl = 0 ;

        /* Process any arguments or offer help */

        while ( i < argc
             && argv[i][0] == '-' ){
                if ( !strcmp( argv[i], "-b" )){
                        both = 1 ;
                        i++ ;
                        continue ;
                 }
                if ( !strcmp( argv[i], "-f" )){
                        cmdl = 1 ;
                        i++ ;
                        break ; /* files beginning with - are made legal */
                }
                fputs(
"\n"
"Usage: realpath [ -b ] [ -f ] [ <file_name_1> . . . .<file_name_N> ]\n"
"\n"
"Calls function realpath() to get a no-symbolic-link absolute path for each\n"
"<file_name> and prints them to standard output.  File names can be provided\n"
"as lines on standard input or as command line arguments.  If -f is not\n"
"present, the first argument not starting with a '-' is the start of the file\n"
"list.\n"
"\n"
"The -b argument causes realpath to prefix each output line with the input\n"
"file name and a tab character.\n"
"\n"
"The -f argument causes realpath to interpret all following arguments as\n"
"file names, even if the first starts with '-', and if no arguments follow.\n"
"When -f is present, realpath does not read file names from standard input.\n"
"\n", stderr );
                exit ( 1 );
         }

        if ( !cmdl
          && i < argc ){
                cmdl = 1 ;
         }

        while (
            (  cmdl
            && i < argc
            && ( bp = argv[i++] ))
          ||
            (  !cmdl
            && ( bp = fgets( buf, sizeof( buf ), stdin )))
          ){
                if ( cp = strchr( bp, '\n' )){
                        *cp = NULL ;
                 }

                if ( !realpath( bp, obuf )){
                        fflush( stdout );
                        fputs( "realpath() failure: ", stderr );
                        perror( bp );
                        continue ;
                 }

                if ( ( both
                    && 0 > printf( "%s\t", bp ))
                  || 0 > printf( "%s\n", obuf )){
                        if ( ferror( stdout )){
                                perror( "stdout" );
                                exit( 1 );
                         }

                        exit( 0 );
                 }
         } /* end while args or stdin */

        if ( ferror( stdin )){
                fflush( stdout );
                perror( "stdin" );
                exit( 1 );
         }

        exit( 0 );
 } /* end main () */

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Finding symbolic links

How can I find all symbolic links across the network to a directory (2 Replies)
Discussion started by: mehtad
2 Replies

2. UNIX for Dummies Questions & Answers

shellscript for finding and replacing in DG-UNIX

Newby here..... Is there any way of doing a find & replace within a huge file other than using something like : " vi - FileForReplacing < /u1/excel/consultants " contents of consultants file looks like :1,$s/0000031/CON/g :1,$s/0001032/JONES/g :1,$s/0001355/SMITH/g... (3 Replies)
Discussion started by: Gerry405
3 Replies

3. Shell Programming and Scripting

finding links to a file

I am writing a shell script that needs to files that are links to on specific shell script. e.g. /usr/bin/a.sh /home/mydir/a.sh --> /usr/bin/a.sh /home/yourdir/a.sh --> /usr/bin/a.sh /home/hisdir/a.sh --> /usr/bin/a.sh /home/herdir/a.sh --> /usr/bin/a.sh I know I can set myself... (2 Replies)
Discussion started by: ohagar
2 Replies

4. Shell Programming and Scripting

help with finding & replacing pattern in a file

Hi everyone. Could u be so kind and help me with on "simple" shell script? 1. i need to search a file line by line for a pattern. example of a lines in that file 2947 domain = feD,id = 00 0A 02 48 17 1E 1D 39 DE 00 0E 00,Name Values:snNo = f10 Add AttFlag = 0 2. i need to find... (0 Replies)
Discussion started by: dusoo
0 Replies

5. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

6. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

7. Shell Programming and Scripting

Finding a String and Replacing it with an incremental value

Hi Friends, I have a text file which has about 200,000 records in it. we have a string which repeats in each and every record. So we have to write a script in ksh which finds that string on each line and replaces it with a new string(incremental value) for a set every four records. To be... (12 Replies)
Discussion started by: vpv0002
12 Replies

8. Homework & Coursework Questions

Finding/replacing text and redirection help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: What command would rename "sequentialInsert", in ~cs252/Assignments/commandsAsst/project/arrayops.h, to... (2 Replies)
Discussion started by: lothwen
2 Replies

9. Shell Programming and Scripting

Finding and replacing whole line using sed

Hi all, assume that i am having the following line in a file called file1. triumph and disaster must be treated same. I want to replace this line with. follow excellence success will chase you. is it possible to do this using sed. if possible kindly post me the... (2 Replies)
Discussion started by: anishkumarv
2 Replies

10. Shell Programming and Scripting

Finding a format in a file and replacing it

Hi, I need help in the following: I have a file in directory with mutiple comma seperated values. One of the value is a date and time format like 2012-04-10 xx:yy:zz I need to find that time format in the file and then replace it with xx:yy+1:zz and then save it as a new file and copy it to a... (3 Replies)
Discussion started by: rabh
3 Replies
CHGRP(1)						    BSD General Commands Manual 						  CHGRP(1)

NAME
chgrp -- change group SYNOPSIS
chgrp [-fhv] [-R [-H | -L | -P]] group file ... DESCRIPTION
The chgrp utility sets the group ID of the file named by each file operand to the group ID specified by the group operand. The following options are available: -H If the -R option is specified, symbolic links on the command line are followed. (Symbolic links encountered in the tree traversal are not followed). -L If the -R option is specified, all symbolic links are followed. -P If the -R option is specified, no symbolic links are followed. This is the default. -R Change the group ID for the file hierarchies rooted in the files instead of just the files themselves. -f The force option ignores errors, except for usage errors and doesn't query about strange modes (unless the user does not have proper permissions). -h If the file is a symbolic link, the group ID of the link itself is changed rather than the file that is pointed to. -v Cause chgrp to be verbose, showing files as the group is modified. The -H, -L and -P options are ignored unless the -R option is specified. In addition, these options override each other and the command's actions are determined by the last one specified. The group operand can be either a group name from the group database, or a numeric group ID. If a group name is also a numeric group ID, the operand is used as a group name. The user invoking chgrp must belong to the specified group and be the owner of the file, or be the super-user. DIAGNOSTICS
The chgrp utility exits 0 on success, and >0 if an error occurs. COMPATIBILITY
In previous versions of this system, symbolic links did not have groups. The -v option is non-standard and its use in scripts is not recommended. FILES
/etc/group group ID file SEE ALSO
chown(2), fts(3), group(5), passwd(5), symlink(7), chown(8) STANDARDS
The chgrp utility is expected to be IEEE Std 1003.2 (``POSIX.2'') compatible. BSD
March 31, 1994 BSD
All times are GMT -4. The time now is 05:32 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy