find Files in sub-directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find Files in sub-directory
# 1  
Old 11-04-2014
find Files in sub-directory

Hi

Just want to ask, Is it possible to find a file from a directory up to its sub-directories?

Thanks,
cmarzan

Last edited by cmarzan; 11-04-2014 at 12:06 AM.. Reason: typo
# 2  
Old 11-04-2014
Hello cmarzan,

Following may help you in same.

Code:
find -type d -name "test11"   ### For checking a directory
AND
find -type f -name "test12*"  ### For checking a file

Output will be as follows for both the commands.
Code:
1st command's output:
./XYZ/test11
 
2nd command's output:
./test123
./test1213
./test12121
./test1222
./test12
./test121
./XYZ/test121

Here above 1st command will check a directory named test11 in all the directories and sub directories of the present working directory, 2nd command will check all files which have names like test12 and etc in it's name to all sub directories too. If you want to limit the search in command you can use -maxdepth command with it. You can read man find for more information.

Hope this helps.


Thanks,
R. Singh

Last edited by RavinderSingh13; 11-04-2014 at 12:50 AM.. Reason: Adding comments to codes
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-04-2014
Hi R.Singh,

Thanks for you effort.

Here is the example of the structure of a folder

MainFolder

Sub-folder_1
Sub-folder_1.1
Sub-folder_1.2

Sub-folder_2
Sub-folder_2.1
Sub-folder_2.2 file.cmd

I want to confirm if the file.cmd is existing. The structure above shows that the file.cmd is inside MainFolder/Sub-folder_2/Sub-folder_2.2

does your given command can put in a IF statement?

Code:
IF [ find -type d -name "MainFolder" ]
then 
mv file.cmd  MainFolder/Sub-folder_2/Sub-folder_2.1
else
exit 0

Many Thanks,
cmarzan

Last edited by cmarzan; 11-04-2014 at 02:05 AM.. Reason: typo errors
# 4  
Old 11-04-2014
Hello camarzan,

Following may help you in same. I have made similar kind of folder structure and checked it worked for me, please check and let me know if this helps.

Code:
cat check_file_status.ksh
value=`find -type f -name "file.cmd"`
if [[ -n "$value" ]]
then
        echo "File present in directory/sub directories." $value
else
        echo "file NOT found."
fi

Output will be as follows.
Code:
File present in directory/sub directories. ./MainFolder/Sub-folder_2.2/file.cmd

As in this case file is present so it is showing same only, you can use any script to execute in place of echo statement in the script.
EDIT: Also want to add my present working directory was /tmp where MainFolder is present so only it is showing in output as ./MainFolder/Sub-folder_2.2/file.cmd.

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-04-2014 at 02:09 AM.. Reason: Added a comment about output for more clarification
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 11-04-2014
I think you could do that with just one call to find

Code:
find /path/to/MainFolder -type f -name "file.cmd" -exec mv -t /path/to/MainFolder/Sub-folder_2/Sub-folder_2.1 {} \;

Test it first with:
Code:
find /path/to/MainFolder -type f -name "file.cmd" -exec ls -l {} \;

This User Gave Thanks to Aia For This Post:
# 6  
Old 11-04-2014
Hi R.Singh,

Many thanks to you...

below is the result of the command you gave to me

cat: check_file_status.ksh: No such file or directory
File present in directory/sub directories. ./grids/portfolio/OAP_TEST2_SCREEN.cmd

It says that the file is existing but can you help me to know what happen here?

cat: check_file_status.ksh: No such file or directory

Thanks,
cmarzan
# 7  
Old 11-04-2014
Hello cmarzan,

You need not to put cat check_file_status.ksh in the script as this was my script name which I have created for the task you have requested for, sorry for same.
For showing you the script's content I have done cat to that script. You can try with following on same.

Code:
value=`find -type f -name "file.cmd"`
if [[ -n "$value" ]]
then
        echo "File present in directory/sub directories." $value
else
        echo "file NOT found."
fi

Kindly let me know if you have any queries please.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

2. UNIX for Dummies Questions & Answers

How to Find Files other than specified directory ?

Hi All, I am creating one script to Archive the older log files to Archive folder and deleting older files. For example below path contains different sub folders. So searching for log files older than 2 days then zip and moving to Archive directory in the same directory. Source files :-... (4 Replies)
Discussion started by: vadlamudy
4 Replies

3. UNIX for Dummies Questions & Answers

How to find and copy files from one directory to another

Ok i have three directories Destination - /u/dir1 (has subdirectories dir2 which also has subdirectory dir3) Source1 - /u/test/files/dir1/dir2/dir3 Source2 - /u/out/images/dir1/dir2/dir3 What i would like to do is copy everything from Source1 and Source2 into the Destination directory.... (3 Replies)
Discussion started by: ziggy25
3 Replies

4. UNIX for Advanced & Expert Users

How can I find out the open files in a directory

Hello all, Is there any other way of finding the open files in a directory apart from command 'lsof'. thanks (3 Replies)
Discussion started by: joshi123
3 Replies

5. Shell Programming and Scripting

Find files ONLY in current directory

Hello all, I am having a hard type in figuring out how to only gather certain files in the current directory without exploring its subdirectories. I tried: find . -name "*.ksh" -prune this also returns ksh files from lower subdirectories. I also tried find . -ls -name "*.ksh" This also... (8 Replies)
Discussion started by: gio001
8 Replies

6. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

7. UNIX for Dummies Questions & Answers

Need help to find the files under a directory

Hi, I wanted to delete all the files under a directory "/apps/tmp/" which are two weeks older. But i should not delete the sub-directories and the contents of sub-directories. I also have searched in forum and found the following command, find . \( ! -name . -prune \) -mtime +13 -print ... (8 Replies)
Discussion started by: Sheethal
8 Replies

8. UNIX for Dummies Questions & Answers

How to find a word in a all the files in a Directory??

I want to find a specific word present in all the files ina directory....Please tell me the command to be used?? Thanks (6 Replies)
Discussion started by: shikhakaul
6 Replies

9. Shell Programming and Scripting

Find files in directory

Hi all I want to find a particular file type lets say .abc under /home/oracle/, the file name is start with 'D' and followed by ddmmyyyy date format, the file name should look like this D19092008.abc To my question, how can i perform the searching from the date 19/09/2008 to 29/09/2008. The... (3 Replies)
Discussion started by: coldstarhk
3 Replies

10. Filesystems, Disks and Memory

find the 5o largest files in a directory

I'm trying to find the 50 largest file in a directory named /sasdb and its' subdirectories. I'm using the find command and a pipe to awk Not sure if I'm actually getting the largest files from this directory and its subdirectories. Here is the code I used... find /sasdb -ls | awk '{print... (8 Replies)
Discussion started by: igidttam
8 Replies
Login or Register to Ask a Question