Find command take too long


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find command take too long
# 1  
Old 08-22-2015
Hammer & Screwdriver Find command take too long

Hi,

I use the below script that will find n record the entire directory structure for a given folder.

Code:
bash-3.2$ more mkdir.sh
find  . -type d |while read rec
do
  echo "mkdir $rec"
  echo "chmod -R 777 $rec"
#done
done >> moht.sh

The problem is the folder i m running this script under where i want it to record all the directory structure has millions of files. This causes the below command to run for 4 hrs now with no results.

Apart from the slowness issue I see that my script above does not record all but only some of the folders structure under the directory it runs

Code:
bash-3.2$ ls -ltrd ServiceV2
drwxrwx---   5 user1 admin      1024 Nov  8  2014 ServiceV2
bash-3.2$ grep -i "ServiceV2" moht.sh
No results Found !!

Can you help fix this script ?

Last edited by mohtashims; 08-22-2015 at 06:56 AM..
# 2  
Old 08-22-2015
If you want to record a list of the directories in the file hierarchy rooted in your current working directory, the command you want would be:
Code:
find . -type d > directory_list

If you are trying to change the mode of every file (including all of the directories and all of the regular files) in the file hierarch rooted in your current working directory, the command you want would be:
Code:
chmod -R 777 .

(but that would be a TERRIBLE thing to do from a security standpoint).

If you are planning to run the shell script created by you current script (moht.sh), then be aware that all of the mkdir commands will fail (since the directories already exist),
and each chmod -R directory after the first one (chmod -R .) is duplicating work done by the first one (and the 1st one will take a LONG time if there are millions of files and subsequent chmod commands (repeating the work already done by the first one) will waste time proportional to the number of files in the hierarchy rooted in that subdirectory. And if any of your existing directory names contain space, tab, or newline characters; your mkdir commands will create new (presumably unwanted) directories (because you aren't properly quoting the arguments you're passing to mkdir).
# 3  
Old 08-22-2015
The requirement is to create a similar directory only structure on a different server and there are more than 1000 folders and million files.

So i run my script which gives me moht.sh. I then take to moht.sh to the second server and simply run it to create similar directory & sub-directory structure on the other box.

But like i said 1. my script takes unreasonable time to complete and 2. it is missing to record a few directories / sub-directories like i posted in the OP.
# 4  
Old 08-22-2015
mohtashims,
Is this the same system you had problems with November last year (Find command takes too long [too] complete)?
Are there any symbolic links in the hierarchy? These can be found with:
Code:
find . -type l -print

Also, check the manpage for find on your system, the -print was not always enabled by default.

To "record" the directory structure, why not use:
Code:
find . -type d -depth -print | cpio -ov > file

To duplicate the structure use:
Code:
cpio -idvu < file

# 5  
Old 08-22-2015
find . -type l -print does not yields any output meaning i have no soft links.

The below seems to have helped to an extent that i guess i can see more folders that I was not able to in my OP script.

Code:
find . -type d -depth -print |while read rec
do
  echo "mkdir $rec"
  echo "chmod -R 777 $rec"
#done
done >> tryx.sh

However, one problem with this script is that it records the mkdir sub-directory before the mkdir parent-directory in the tryx.sh making it to fail.

So basically it mkdir hello/customer before mkdir hello making it fail.

Quote:
more tryx.sh

mkdir ./hello/customer
chmod -R 777 "hello/customer"
mkdir ./hello
chmod -R 777 "hello"
I did not try the cpio option as i m not familiar with it and not sure if it will really help in the problem mentioned in this post.
# 6  
Old 08-22-2015
Quote:
Originally Posted by mohtashims
Code:
find . -type d -depth -print |while read rec
do
  echo "mkdir $rec"
  echo "chmod -R 777 $rec"
#done
done >> tryx.sh

I have no means to try that with a really big filesystem but as a wild guess I'd say the reason why this is slow is because of the shell-loop to do the writing to the file.

I suggest you do it this way:

Code:
find . -type d -print | sed  's/.*/mkdir -p &/; $ s/$/; chmod -R 777 */' > script.sh

This writes a "mkdir -p $dir" for every dir found and then a "chmod -R 777 *" as the last command. You didn't say that but i supposed you start the directory recreation process in an otherwise empty directory.

I hope this helps.

bakunin
# 7  
Old 08-24-2015
When piped to a text processor like sed then the file name should be in quotes:
Code:
find . -type d -print | sed "s/.*/mkdir -p '&'/"

Same for the shell processor:
Code:
echo "chmod -R 777 '$rec'"

Two others that might restore faster:
Code:
find . -type d -exec printf "mkdir '%s'\n" {} +

Code:
find . -depth -type d -print | awk -F/ 'NF>=prev {printf "mkdir -p '%s'\n",$0} {prev=NF}'

If you want to copy the permissions, then cpio is the efficient solution, as was proposed by derekludwig.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command takes long

Hi, I am trying to search for a Directory called "mont" under a directory path "/opt/app/var/dumps" Although "mont" is in the very parent directory called "dumps" i.e "/opt/app/var/dumps/mont" and it can never be inside any Sub-Directory of "dumps"; my below find command which also checks... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Advanced & Expert Users

Find command takes too long to complete

Hi, Below is my find command find /opt/app/websphere -name myfolder -perm -600 | wc -l At time it even takes 20 mins to complete. my OS is : SunOS mypc 5.10 Generic_150400-09 sun4v sparc SUNW,T5440 (10 Replies)
Discussion started by: mohtashims
10 Replies

3. Shell Programming and Scripting

find only 6 char long

find /tmp -type f -mtime +180 I have this script get the list to clean up files older than 180 days under /tmp. But, I want to make sure to grep only a type of files, which have only 6 character long. .... LT3hqa dRMoya ... (16 Replies)
Discussion started by: Daniel Gate
16 Replies

4. Shell Programming and Scripting

find 3 character long text

I was trying to do some experiment with "sed". I want to find the filenames which are three characters. So, this is what I have done to search for it, using sed. sed -n -e '/^\{3\}$/p' test This returns the correct output for characters. But if I make change, let's say i create 2 more... (4 Replies)
Discussion started by: aksijain
4 Replies

5. Solaris

How to find out bottleneck if system is taking long time in gzip

Dear All, OS = Solaris 5.10 Hardware Sun Fire T2000 with 1 Ghz quode core We have oracle application 11i with 10g database. When ever i am trying to take cold backup of database with 55GB size its taking long time to finish. As the application is down nobody is using the server at all... (8 Replies)
Discussion started by: yoojamu
8 Replies

6. Shell Programming and Scripting

Find and rename long file names (html)

Hi Guys, I need a help. I have 1130 zip files. Each one of them has files including 1 html file with long file name (includes special charactors, Alphabetic and numbers). I have copied all 1130 zip files to my linux system and extracted using below command. Find . -name "*.zip" -exec... (7 Replies)
Discussion started by: Rajmani
7 Replies

7. UNIX for Dummies Questions & Answers

Long listing of files using find command on remote server via SSH

Hi , I am trying to find some files on a remote machine using the find command. >ssh -q atukuri@remotehostname find /home/atukuri/ -name abc.txt /home/atukuri/abc.txt The above command works fine and lists the file, but if I want to do a long listing of files (ls -l) its not working . ... (2 Replies)
Discussion started by: atukuri
2 Replies

8. Shell Programming and Scripting

Help with find command and list in a long format each found file

The purpose of those comands are to find the newest file in a directory acvrdind to system date, and it has to be recursively found in each directory. The problem is that i want to list in a long format every found file, but the commands i use produce unexpected results ,so the output lists in a... (5 Replies)
Discussion started by: alexcol
5 Replies

9. Shell Programming and Scripting

Find/Change long text

If I have very large text file *************** *************** *************** *************** *************** ABC-sdfsdf BBB-xk[ptr'; CCC-sdfolb ABC-dltg'fl;l My aim: -> tail last 10 lines from large text. ***** Ok ***** -> Change first 3 charactors which begin with "ABC" to "abc".... (1 Reply)
Discussion started by: aungomarin
1 Replies

10. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies
Login or Register to Ask a Question