How to list the content of a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to list the content of a directory
# 1  
Old 05-18-2011
How to list the content of a directory

Hi

I want to write a script that reads a directory name, checks if the directory exists and lists the content of that directory.

That's what I have at the moment.

Code:
function listDirectory 
{    
    echo "Give in a directory name"
    read name
    
    #Here I want to check if the directory exitst and then 
    #print the content of that directory (if exists off course)


    #If directory doesn't exists:
    echo "Directory doesn't exists. Closing the application..."
    exit 0   

}
listDirectory

# 2  
Old 05-18-2011
Code:
...
read name

if [ -d "$name" ]; then
  echo yep
  ls "$name"
else
  echo nope
  exit
fi

Read the man page for: test
# 3  
Old 05-18-2011
Hi,

Just some clue to help you find the answers :

Testing if it's a directory : [ -d $name ]
Print the content of a directory : ls $name

Good luck Smilie

Edit: damn i was too slow to answer Smilie

Last edited by Chirel; 05-18-2011 at 07:30 PM.. Reason: too slow
# 4  
Old 05-20-2011
Thanks guys!
# 5  
Old 05-20-2011
Quote:
Originally Posted by scottn
Code:
...
read name

if [ -d "$name" ]; then
  echo yep
  ls "$name"


There's no need for ls:
Code:
 printf "%s\n" "$name/"*

Quote:
Code:
else
  echo nope
  exit
fi

Read the man page for: test
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

3. UNIX for Dummies Questions & Answers

move a file content to another directory

my script is: /u/user/orginal/:#! /bin/ksh find . -name "aur_prog*" -exec grep -il "error" > test.out #awk command to destination directory exit test.out file contain: ./aur_prog1.log ./aur_prog2.log ... (5 Replies)
Discussion started by: roughwal
5 Replies

4. UNIX for Dummies Questions & Answers

Move content from directory to another

I have in root directory a folder A and a Folder B. I want to copy or move all content (many files) from A to B. How do I do that UNIX style? Thanks! (6 Replies)
Discussion started by: Matsakii
6 Replies

5. Shell Programming and Scripting

Help with Compare and search content in directory

Hello, How to search in directory by comparing some string with the content of directory. Ex: I want to compare abhi string with the content of backup directory. i.e want to check that is there any file in backup directory having name ... (3 Replies)
Discussion started by: AbhijitIT
3 Replies

6. UNIX for Dummies Questions & Answers

How to maintain the content of array in any directory I go?

Hi all, I have this scenario where:- The file that I want to save its name into array df is my.08120323.trx which is located in the dir as below: $ pwd /u01/abc/def/SRC_datafiles $ ls *trx my.08120323.trx $ df=*"trx" ##keeping the filename... (1 Reply)
Discussion started by: luna_soleil
1 Replies

7. UNIX for Dummies Questions & Answers

Print the content of a directory in jpg file

Is there any possible way to print the contents of a directory to a .jpg file? I have a list of thumbnails (e-books) which I want to share (+500) but I don't know how to make this. I would appreciate a lot any comments regarding this issue. (4 Replies)
Discussion started by: agasamapetilon
4 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. Shell Programming and Scripting

I want to record the difference in the content of files in different directory

Hi All I am very new to the Unix shell scripting ,, could you pleae help me to generate the output file having the filename and path which files having the difference in the contents in the two directory. all files in both directory have the same name and format. input directory /edc/input1/ ... (3 Replies)
Discussion started by: singhald007
3 Replies

10. UNIX for Advanced & Expert Users

Can't view directory content - SUNOS

Hi, The app I support has a directory used for debugg files. It has just come to light there are no files in there. Or is there :confused: Changing into the directory and completing an ls -lta you get no files. This is truly not right for the application. If I go up one directory the... (2 Replies)
Discussion started by: nhatch
2 Replies
Login or Register to Ask a Question