Testing for subdirectories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Testing for subdirectories
# 1  
Old 01-24-2011
Testing for subdirectories

Hello,

Can anyone help me figure out how to test if the item in the directory is a subdirectory?

I'm writing a code to copy all the contents of directory1 to directory2, but I want to skip all the subdirectories.

Thanks!
# 2  
Old 01-24-2011
Code:
 $ ruby -r"fileutils" -e 'Dir["*"].each{|x| !test(?d,x) and FileUtils.copy(x,"/destination") }'

# 3  
Old 01-24-2011
Sorry I forgot to mention that I'm writing in C =]
# 4  
Old 01-25-2011
If no space in file name:
Code:
cd directory1
ls -l |grep -v ^d |awk '{print $NF}' |xargs -i cp {} /directory2

If your find support maxdepth:

Code:
find /directory1 -maxdepth 1 -type f -exec cp {} /directory2 \;

# 5  
Old 01-25-2011
Quote:
Originally Posted by l flipboi l
Sorry I forgot to mention that I'm writing in C =]
You can determine the type of file from a C programme by using the stat family of system calls, and then using the POSIX S_ISDIR(m) macro to test the st_mode field to determine if the file is a directory. All of the information that you will need to implement this can be found in man stat.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed through all subdirectories?

I found this awesome sed script here: https://www.unix.com/shell-programming-scripting/48228-perl-search-string-line-then-search-replace-text.html sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename Question though to save me manually doing this. How do I do this from a root directory... (6 Replies)
Discussion started by: Astrocloud
6 Replies

2. Linux

Stats on subdirectories

Please help me with a shell script to get the stats on many subdirectories (sub1), (sub2) etc under a mother directory (big) /big | |_______sub1 |_______sub2 |_______sub3 --------- I want to know 1. What is the last file accessed in each subdirectory with date and by whom 2.... (2 Replies)
Discussion started by: digipak
2 Replies

3. Shell Programming and Scripting

diff different subdirectories

I have 2 directories a/ and b/, they have different subdirectories, how to diff with missing file, or missing subdirectory and if i have in a/ directory "a/ACD/DEF" DEF is a file, but in b/ directory "b/ACD/DEF is a SUBDIRECTORY, how to diff it, thanks my solution for directories, ... (7 Replies)
Discussion started by: knajta
7 Replies

4. Shell Programming and Scripting

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (3 Replies)
Discussion started by: ksgreen
3 Replies

5. UNIX for Dummies Questions & Answers

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (2 Replies)
Discussion started by: ksgreen
2 Replies

6. UNIX for Dummies Questions & Answers

looping through subdirectories

Hi, How to loop through all the subdirectories in a directory, merge the files present in it to a single file in that subdirectory itself and remove the original files? Please advise. (5 Replies)
Discussion started by: er_ashu
5 Replies

7. UNIX for Dummies Questions & Answers

how to see all the subdirectories easily?

Suppose I have two directories a and b. Each directory has a few subdirectories, a1 a2 a3 and b1, b2, b3 respectively. Using ls, I can see a and b. Then I need cd a, ls, cd ../b, ls to see all the subdirectories. How to see all the directories and subdirectories easily, say using just one... (2 Replies)
Discussion started by: fld2007
2 Replies

8. Solaris

/home Subdirectories

Hello: Could someone please explain to me how to create a subdirectory in the /home directory. I have tried creating a new user but the default path for a new user is /export/home. I am running Unix 5.8 on a Sun Blade 100. Thanks. (8 Replies)
Discussion started by: mawalton
8 Replies

9. UNIX for Dummies Questions & Answers

How to search all subdirectories?

Dear All, I want to write the Unix command that searches through all subdirectories, finds the files named ''core'' and deletes them. I will very much appreciate your help. David (4 Replies)
Discussion started by: david_wang
4 Replies
Login or Register to Ask a Question