Listing directories and ${1:+$1/}*


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Listing directories and ${1:+$1/}*
# 1  
Old 01-23-2005
Listing directories and ${1:+$1/}*

Hi
I have 2 questions:

Q1 - What does
${1:+$1/}*
mean? I guess it lists all files in current directory - Could any one explain how this expression works?

Q2 - I am trying to list directories only in current path - I know that ls could be used but I thought I'd give find a try. I need to list only high level directories (no sub directories or files)

find has option -type d however, the following command always lists sub-directories:

find . -type d

I tried to use maxdepth as in

find . -type d maxdepth 0

but that gives me syntax error.

So my Q2 is:
How to use fiind command to list top level directories only?

Thanks much.
# 2  
Old 01-24-2005
Regarding ${1:+1\}*

In Korn shell you can use expressions such as this to set default values for variables. The above statement checks to see that if $1 (the first input variable) is set, if it is then $1\* is substituted. Can you post the complete line in your script where this is used? Also, there are other builtins, please see the botton of this thread

Regarding listing directories using find. Well thats sort of using a hammer to break an egg Smilie Plus, by default, find is recursive in nature. Try using ls | grep ^d or something like that.
# 3  
Old 01-24-2005
Please navigate:
our home page -> Answers to Frequently Asked Questions -> advanced/complex uses of the find command
# 4  
Old 01-24-2005
Quote:

Q1 - What does
${1:+$1/}*
mean? I guess it lists all files in current directory - Could any one explain how this expression works?
Above expression is trying to list files (with relative path including paramter passed as first arg ) of the directory passed as argument.
If argument is not passed it lists the files/directories in the current directory.

See below ...

$ cat x.sh
echo ${1:+$1/}*

$ ./x.sh 1234
1234/subdir1 1234/subdir2 1234/subdir3 1234/subdir4 1234/subdir5

$ ls 1234
subdir1 subdir2 subdir3 subdir4 subdir5
$ ./x.sh
1234
# 5  
Old 01-24-2005
Thanks

Thanks for your help guys.

As per google's inquiry about the context, it appeared in a ksh script that goes through files and directories and does operations on them, it looked like this:

for filename in ${1:+$1/}*; do
....
done

I personlay think that this form of coding in ksh is not comfortable. It is more compact but then again not instantly readable...
# 6  
Old 01-24-2005
Quote:
I personlay think that this form of coding in ksh is not comfortable. It is more compact but then again not instantly readable
Builtins such as that are definitely useful and you will undoubtedly see them again. You may not be too comfortable with them now, but as you learn the language you will find that they provide quite a lot of value. Happy coding.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

listing directories with more than x files

How can I find and print the directories on a server that have more than 5,000 files? There's some spam emails and I'm trying to find all directories that have a lot of spam The file count should just be the files directly under that directory, not like the total from all nested directories ... (1 Reply)
Discussion started by: vanessafan99
1 Replies

2. Shell Programming and Scripting

listing directories and sub directories with time and name options

Hello all! I'm looking to list directories and sub-directories of a path, on this forum I found this command: find $path -type d -exec ls -ld {} \; The issue I have is that with a simple ls, the list is listed by name, and using -t I get it by time. How could I list directories and sub... (5 Replies)
Discussion started by: nomadvisuals
5 Replies

3. Shell Programming and Scripting

Listing non empty directories

Hi Gurus, How to list directories that are non-empty and non-hidden Thanks in advance (2 Replies)
Discussion started by: kinny
2 Replies

4. Shell Programming and Scripting

Listing directories and subdirectories

How can list the directories and the subdirectories in a folder. It is possible with a single UNIX command. For example i have a folder named "archive" and another folder named "0808" and then multiple folders are there ... Can i list all the directories and subdirectories in the folder... (6 Replies)
Discussion started by: karansachdeva
6 Replies

5. Shell Programming and Scripting

listing directories alone

ls lists all files and sub directories in the current directory but how to list only the sub directories and not the files? (2 Replies)
Discussion started by: bbala
2 Replies

6. UNIX for Dummies Questions & Answers

listing of directories with / at end...without ls -F

Hi When im listing (ls -al ) its listing directories without / at the end of directories dir1 dir2 dir3 and i need to list directories with dir1/ dir2/ dir3/ and this should not be made by command ls -F / should be embedded at the last since one of the scripts reads... (10 Replies)
Discussion started by: vasanthan
10 Replies

7. UNIX for Advanced & Expert Users

listing of directories with / ...not to use ls -F

Hi When im listing (ls -al ) its listing directories without / at the end of directories dir1 dir2 dir3 and i need to list directories with dir1/ dir2/ dir3/ and this should not be made by command ls -F / should be embedded at the last since one of the scripts reads directories... (1 Reply)
Discussion started by: vasanthan
1 Replies

8. UNIX for Dummies Questions & Answers

Listing Unique directories.

Hi, Simple question. If i could find a file been listed in many dirctories, how to get the unique list of directories. find / -name "abc*" --> will give me list of directories. /home/abc.txt /home/ddd/abcd.txt /home/ddd/abcde/txt So how to get /home /home/ddd Thanks in... (2 Replies)
Discussion started by: deepakwins
2 Replies

9. Shell Programming and Scripting

Listing Deleted Files and Directories

Please provide me a shell script so that i can list which file or directory has been deleted, by which user and at what time. The script should take date as argument and should list out name of the file/directory, which user had deleted them and at what time since that particular date. Kindly post. (1 Reply)
Discussion started by: raviviolet13
1 Replies

10. UNIX for Dummies Questions & Answers

files and directories listing

Hello: I need to list some files into some directories ordered by size. i need help! BEst regards Alberto (1 Reply)
Discussion started by: bbolson
1 Replies
Login or Register to Ask a Question

Featured Tech Videos