print summary of directory, and group all symbolic links


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print summary of directory, and group all symbolic links
# 1  
Old 03-06-2011
print summary of directory, and group all symbolic links

I am trying to get a summary of filetypes in a directory, but the total count of symbolic links is not working. I am stuck at the results of the file command. I have used the find command to confirm my expectations, but my bash function is not giving the results I want.

Here is my function:
Code:
function summ ()
{
	echo					# intentional blank line
	echo -en "[${HOSTNAME}:${PWD}]"; echo
	for FILE in * ; do			# intentionally not recursive
		FILETYPE=`file "$FILE" | cut -d ":" -f 2`
		echo $FILETYPE >> /tmp/.dir_summary 
	done
	cat /tmp/.dir_summary | sort | uniq -c
	\rm /tmp/.dir_summary		# skip alias='rm -Iv'
	echo -en "$[Total == "`/bin/ls $@|wc -l`" @"\
		`du -h --max-depth=0 2>/dev/null | cut -c -4`"]"; echo
	}

And here is my find command to confirm the number of symbolic links:
Code:
find . -maxdepth 1 -mindepth 1 -type l | wc -l

Result:
Code:
16

Here is my actual output of /etc:
Code:
[lap-fedora13:/etc]
      1 ASCII C program text
      1 ASCII C++ program text
     43 ASCII English text
      1 ASCII English text, with escape sequences
      2 ASCII Pascal program text
     51 ASCII text
      1 ASCII text, with very long lines
      2 data
    119 directory
      9 empty
      9 regular file, no read permission
      1 symbolic link to `../boot/grub/grub.conf'
      2 symbolic link to `fedora-release'
      1 symbolic link to `rc.d/init.d'
      1 symbolic link to `rc.d/rc'
      1 symbolic link to `rc.d/rc0.d'
      1 symbolic link to `rc.d/rc1.d'
      1 symbolic link to `rc.d/rc2.d'
      1 symbolic link to `rc.d/rc3.d'
      1 symbolic link to `rc.d/rc4.d'
      1 symbolic link to `rc.d/rc5.d'
      1 symbolic link to `rc.d/rc6.d'
      1 symbolic link to `rc.d/rc.local'
      1 symbolic link to `rc.d/rc.sysinit'
      1 symbolic link to `../sbin/rmt'
      1 symbolic link to `/usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png'
      1 timezone data, version 2, 5 gmt time flags, 5 std time flags, no leap seconds, 235 transition times, 5 abbreviation chars
      1 UTF-8 Unicode English text
[Total = 256 @ 26M ]

And the expected text:
Code:
[lap-fedora13:/etc]
      1 ASCII C program text
      1 ASCII C++ program text
     43 ASCII English text
      1 ASCII English text, with escape sequences
      2 ASCII Pascal program text
     51 ASCII text
      1 ASCII text, with very long lines
      2 data
    119 directory
      9 empty
      9 regular file, no read permission
     16 symbolic link 
      1 timezone data, version 2, 5 gmt time flags, 5 std time flags, no leap seconds, 235 transition times, 5 abbreviation chars
      1 UTF-8 Unicode English text
[Total = 256 @ 26M ]

I know that both file and du are reading the entire directory once apiece, so I am trying to avoid a third read (with find).

I have given some thought to pipe the output to a second /tmp/file, and somehow use the cut -d (delimiter) option to group the symbolic links, and then uniq -c (gasp) again, but in the example above, there are 2 symbolic links to `fedora-release' and my count would be off.
# 2  
Old 03-06-2011
You could replace
Code:
cut -d ":" -f 2

with
Code:
sed 's/[^:]*://; s/^ symbolic link to.*/ symbolic link/'

In all cases, it removes everything up to the first colon, inclusive. In the case of symbolic link, it'll strip away everything beginning with the space after "symbolic link".

Except for deleting the target of a symbolic link, it should yield output identical to your cut approach so that subsequent commands require no modification.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 3  
Old 03-06-2011
YES!!

That worked perfectly. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Symbolic links

Soft link,Hard link brief explanation (1 Reply)
Discussion started by: RAJU KAVATI
1 Replies

2. UNIX for Dummies Questions & Answers

Symbolic links in UNIX

Hi, I have a file with more than 1 layers of soft links for it. For ex. ls -la .profile .profile@ -> /home/act/.profile_abc ls -la /home/act/.profile_abc@ -> .profile_final I want to get the name of the last file (i.e. .profile_final) when I refer to .profile using shell script. I... (2 Replies)
Discussion started by: deo_kaustubh
2 Replies

3. UNIX for Dummies Questions & Answers

Permissioning for symbolic links

I've just started using UNIX in a Linux vmplayer. I'm trying to run the command: ln -s `pwd`/$1 `python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"`/$1 in a script from an online tutorial, but I keep getting an error message: ln: creating symbolic link... (1 Reply)
Discussion started by: ChipT
1 Replies

4. UNIX for Dummies Questions & Answers

Symbolic Links

Hi all, I have scoured the entire forum for this but to no avail unfortunately. Basically, I would like to remove my symbolic link from my folder name i.e. foldername -> /a/b/c/d/f where f is indeed a folder. I have tried rmdir but this does not work and in actual fact deletes the... (4 Replies)
Discussion started by: cyberfrog
4 Replies

5. UNIX for Dummies Questions & Answers

Symbolic links between directories

Hi all, lets consider 2 directories test1 and test2. I want to link test2 to point to test1, how do u do this? (4 Replies)
Discussion started by: Mr. Zer0
4 Replies

6. AIX

Symbolic Links

I am linking a directory as follows: ln -sf /home/xxx/userid/real_files/* /home/xxx/userid/linked_files This gives me symbolic links for all the files in the real_files directory in the linked_files directory. My question is, if I go and remove a file in the real_files directory and then go... (1 Reply)
Discussion started by: rcarnesiii
1 Replies

7. Programming

Ignoring symbolic/hard links while scanning through a directory

Hi, I am writing a unix system utility that is supposed to scan through a directory, collecting information about the files and subdirectories. That part is going well. The tricky part is that some files in the directory are hard links or symbolic links. I am supposed to IGNORE these links.... (4 Replies)
Discussion started by: Yifan_Guo
4 Replies

8. UNIX for Dummies Questions & Answers

Too many levels of symbolic links

Hi, Is there a limit to the number of symbolic links you can have? I tried to vi the symbolic link relating to a file and got the following error: "filename" Too many levels of symbolic links There is only one symbolic link to one file in this case, but there are >2000 other links to... (2 Replies)
Discussion started by: Bab00shka
2 Replies

9. UNIX for Dummies Questions & Answers

Symbolic Links for a File

how do i get the list of symbolic link names for a particular file programatically (4 Replies)
Discussion started by: b_u_n_1234
4 Replies

10. 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
Login or Register to Ask a Question