LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > *BSD
User Name
Password
*BSD This forum is for the discussion of all BSD variants.
FreeBSD, OpenBSD, NetBSD, etc.

Notices


Reply
  Search this Thread
Old 01-20-2016, 05:56 AM   #1
Neville Hillyer
Member
 
Registered: Jul 2015
Posts: 57

Rep: Reputation: Disabled
Recursively listing files using bash builtins


I am using bash 3.2 on OS X.

As an exercise I have set myself the task of recursively listing files using bash builtins. I particularly don't want to use ls or find and I would prefer not to use setopt etc. The following appears to work:

Code:
g() { for k in "$1"/*; do # loop through directory
[[ -f "$k" ]] && { echo "$k"; continue; }; # echo file path
[[ -d "$k" ]] && { [[ -L "$k" ]] && { echo "$k"; continue; }; # echo symlinks but don't follow
g "$k"; }; # start over with new directory
done; }; g "/Users/neville/Desktop" # original directory
I am aware that replacing "$1"/* with "$1"/.* produces many directories such as: /Users/neville/Desktop/test dir/././././././././././.

I am also aware that the multiple directory issue can be overcome by using these terms: "$1"/.[^.]* and "$1"/..?* but I don't understand why it is necessary to totally excluded the UNIX dot files. Are the dot files acting like symlinks to directories and if so why is this not stopped by my original symlink trap? If I was happy to list all the UNIX dot and double dot files how should it be modified?
 
Old 01-20-2016, 06:13 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
. and .. are not symlinks but hardlinks. Therefore they cannot be detected by -L. You need to handle those two directories apart from the other entries.
Code:
[[ "$k" == "$1/." ]] && { ....
[[ "$k" == "$1/.." ]] && { ....
 
1 members found this post helpful.
Old 01-20-2016, 02:44 PM   #3
Neville Hillyer
Member
 
Registered: Jul 2015
Posts: 57

Original Poster
Rep: Reputation: Disabled
@pan64 - Many thanks for telling me they are hard links - this explains my difficulties - I am surprised that my earlier Google searches did not point me in the hard link direction. So much for the often stated: 'hard links don't point to directories' - clearly one of the exceptions - I wonder if there are more?
 
Old 01-21-2016, 12:43 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, usually hard links among directories are not allowed. This is the only exception I know about (. and ..).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] listing files recursively, sorted by time, limited head Harju Linux - Newbie 12 03-25-2011 10:51 AM
auditd and bash builtins Vypadkovyy Linux - Security 6 07-10-2009 11:37 AM
Bash builtins?????? adnankhawer Linux - Software 8 03-07-2008 02:24 PM
over rule bash builtins lynnevan Slackware 6 04-01-2007 01:19 PM
Recursively listing files over FTP? overbored Linux - Software 4 10-14-2004 12:12 PM

LinuxQuestions.org > Forums > Other *NIX Forums > *BSD

All times are GMT -5. The time now is 12:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration