Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Like AngTheo789 said: The dot is to search from the dir you started the command, but you can also put a "hard" directory in its place (/home or / or /tmp). You cannot, to my knowledge use 2 different starting points in one find statement.
Like AngTheo789 said: The dot is to search from the dir you started the command, but you can also put a "hard" directory in its place (/home or / or /tmp). You cannot, to my knowledge use 2 different starting points in one find statement.
Hope this helps.
search from root directory , it is work , but it also consume many resource and need time to process the search , is there any method to find the file in full path ? thx
That depends on where you suspect the files are that you are looking for.
For example if you know they are in /var and/or in /opt, you need to start from / (or 2 find statements, one from /var and one from /opt).
If you know that the files you are looking for are in one of the home directories you can start in /home (find /home -name ....). Zoom in as far as possible to reduce the time and resources needed by the find statement.
There is a possible alternative: locate. But you do need an up-to-date locate database for that (updatedb, as root to update that database).
That depends on where you suspect the files are that you are looking for.
For example if you know they are in /var and/or in /opt, you need to start from / (or 2 find statements, one from /var and one from /opt).
If you know that the files you are looking for are in one of the home directories you can start in /home (find /home -name ....). Zoom in as far as possible to reduce the time and resources needed by the find statement.
There is a possible alternative: locate. But you do need an up-to-date locate database for that (updatedb, as root to update that database).
Hope this helps.
thx reply,
Yes , I know the directory that the files are located , you suggest to use 2 find statement , is this the only method that I can use ? can I use single statement to do it ? thx
for FILE in `find /tmp -mtime +3 -name "abc" -exec ls {} \; `
do
echo $FILE
cp $FILE /tmp/backup/$FILE
done
it can not moved to /tmp/backup as the variable $FILE is /tmp/abc , so it can not copy to /tmp/backup//tmp/abc , can advise if I just want to copy the file to /tmp/backup , what can i do ? thx
It is bad practice to use a backup-directory inside a directory you are backing up. Make sure your target and destination are not part of each other.
It looks like you are trying out things and use /tmp "just in case", you could use /var/tmp and /tmp, one as destination for the backup and one the target.
find /tmp -mtime +3 -name "abc" -exec cp {} /var/tmp \; (The {} in the find command is replaced by what is found.)
As you can see you do not need the for loop, you can use cp in the find command. If you want to see what find has found as well (the echo statement in your loop):
It is bad practice to use a backup-directory inside a directory you are backing up. Make sure your target and destination are not part of each other.
It looks like you are trying out things and use /tmp "just in case", you could use /var/tmp and /tmp, one as destination for the backup and one the target.
find /tmp -mtime +3 -name "abc" -exec cp {} /var/tmp \; (The {} in the find command is replaced by what is found.)
As you can see you do not need the for loop, you can use cp in the find command. If you want to see what find has found as well (the echo statement in your loop):
your method works , but as I need to use the variable $FILE to do something , how can I keep the do , done statement in my case ? is it possible to do that ? thx
You don't need a filename in the destination. You could use a directory target. "-exec cp '{}' /path/to/targetdir/ \;"
To remove the directory part of a pathname you could use ${file#*/}
You can also use the `basename' command to remove the directory part.
Also, find will recurse subdirectories by default unless you limit the depth of the search with the `-maxdepth' argument. Maxdepth needs to be after the directory and before other arguments like `-name'. You can combine -maxdepth and -mindepth to limit the depth within a range.
I would recommend scanning the info manual for the find command. It is a very flexible command. I only recently discovered the -delete and -ls find commands.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.