LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-13-2013, 04:09 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
search all files and only empty directories using find command


Hi All,
I wanted to search all empty, non empty files and only empty directories.
Tried.
Code:
find /path/of/dir -type d -empty -type f
But did not work.
So how to search using single find command.
Please help.
 
Old 06-13-2013, 04:14 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 173Reputation: 173
have you tried this?
Quote:
find /path/of/dir -empty
 
Old 06-13-2013, 04:38 AM   #3
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by yooy View Post
have you tried this?
Thanks for the reply, but you did not understand my question.
The above command is listing only empty files and directories. But I wanted to search all files and also only empty directories using find command at one time. So please help.
 
Old 06-13-2013, 04:51 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Can you explain in more detail; your request doesn't make sense to me
'only empty dirs' conflicts with searching files. If a file is there, its obviously in a non-empty dir.
See also ' search all empty, non empty files' which is a contradiction as written....
 
Old 06-13-2013, 05:29 AM   #5
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Tnanks for the reply chrism01.
I am asking that is there a way to search in a single command by putting multiple types instead of searching as below by putting &&
Code:
touch -t 201306131200 /tmp/timestamp

find . -newer /tmp/timestamp -type d -empty >> /tmp/searchlist && find . -newer /tmp/timestamp -type f >> /tmp/searchlist
 
Old 06-13-2013, 12:41 PM   #6
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Can any body please help for the above?
 
Old 06-13-2013, 02:00 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Please have some patience. The people who can help you can come at any time.

It also helps to explain very clearly what you want to do. Give some background information about your goal, so we can better understand it.


So you just want to combine multiple tests and actions? Well, find has \(..\) grouping brackets, so perhaps something like this?

Code:
find . \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \)  >> /tmp/searchlist
BTW, recent versions of gnu find have a -newerxy option that can replace -newer, allowing you to specify a time stamp directly.

Code:
find . \( -newermt  201306131200 -type d -empty -print \) -o \( -newermt 201306131200 -type f -print \) >> /tmp/searchlist
Here are a couple of good links about using find:
http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html

Last edited by David the H.; 06-13-2013 at 02:02 PM. Reason: corrections
 
1 members found this post helpful.
Old 06-13-2013, 02:21 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Sorry but I.m not sure I understand what you are after.

but here is a try.

you want a list of empty dirs or any file which are newer than T?


Code:
find . -newer /tmp/timestamp -type d  -empty -o -newer /tmp/timestamp -type f
-o is Or
-a and

Note, -a is implied when you have two expr
e.g.
Code:
find . -newer /tmp/timestamp -a -type d -a -empty -o -newer /tmp/timestamp -a -type f
see man find for more details

I still don't understand why you want empty dirs and all files

if it is for this
http://www.linuxquestions.org/questi...me-4175465857/

and you want to exclude empty stuff from tar

Code:
tar -c `find . -newer /tmp/timestamp -type f ! -empty | sed s/\ /?/g` -f YourTar.tar
You get the idea
I think I half understand the empty dir thing, nearly.
 
1 members found this post helpful.
Old 06-13-2013, 02:27 PM   #9
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Please have some patience. The people who can help you can come at any time.
Thanks BASH Guru.
Code:
# find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \)
/data/cli/grub.cfg
/data/cli/etc/sane.d/dll.d/L i n u x
/data/cli/etc/hosts
/data/cli/etc/vlc/lua/test.exe
/data/cli/etc/X11/xinit/This is an empty directory
/data/cli/etc/pam.d/vsftpd
Excellent command.
I just wanted to backup using only tar whatever it found i.e even for those files/directories even if they have white space in their names. Please see the below.
Code:
# ls -l /data/cli/etc/sane.d/dll.d/L\ i\ n\ u\ x 
-rw-rw-r-- 1 cli cli 11 Jun 13 19:02 /data/cli/etc/sane.d/dll.d/L i n u x

# ls -ld /data/cli/etc/X11/xinit/This\ is\ an\ empty\ directory/
drwxrwxr-x 2 cli cli 4096 Jun 13 18:58 /data/cli/etc/X11/xinit/This is an empty directory/
But I am unable to take backup using tar
Code:
# tar -cpjvf $(find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \))
tar: Removing leading `/' from member names
tar: /data/cli/etc/sane.d/dll.d/L: Cannot stat: No such file or directory
tar: i: Cannot stat: No such file or directory
tar: n: Cannot stat: No such file or directory
tar: u: Cannot stat: No such file or directory
tar: x: Cannot stat: No such file or directory
/data/cli/etc/hosts
/data/cli/etc/vlc/lua/test.exe
tar: /data/cli/etc/X11/xinit/This: Cannot stat: No such file or directory
tar: is: Cannot stat: No such file or directory
tar: an: Cannot stat: No such file or directory
tar: empty: Cannot stat: No such file or directory
tar: directory: Cannot stat: No such file or directory
/data/cli/etc/pam.d/vsftpd
tar: Exiting with failure status due to previous errors
So is there a way either we can take backup of such files & directories without sending to output file or by sending to output file and then making list of paths into the array which can take backup. The same I have posted here and still I am stuck in this.
Once again thanks a lot and waiting for your kind reply.
 
Old 06-13-2013, 02:45 PM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
David's find piped through sed

Code:
tar -c $(find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \)|sed s/\ /?/g) -f YourTar.tar
Not perfect as ? will match any char

This will be better , escape hell
Code:
tar -c $(find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \)|sed s/\ /\\\\\ /g) -f YourTar.tar

Scratch that, not working

Last edited by Firerat; 06-13-2013 at 02:52 PM.
 
1 members found this post helpful.
Old 06-13-2013, 03:03 PM   #11
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
I still don't understand why you want empty dirs and all files

if it is for this
http://www.linuxquestions.org/questi...me-4175465857/
Yes.
Thanks a lot Firerat.
Code:
# find /data/cli/ -newer /tmp/timestamp -type d  -empty -o -newer /tmp/timestamp -type f
/data/cli/grub.cfg
/data/cli/etc/sane.d/dll.d/L i n u x
/data/cli/etc/hosts
/data/cli/etc/vlc/lua/test.exe
/data/cli/etc/X11/xinit/This is an empty directory
/data/cli/etc/pam.d/vsftpd
Now I wanted to take backup all of the above.
As you can see in above output, the colored file & directory respectively which are having white spaces in their names also should be backed up.
Code:
# tar -c `find /data/cli -newer /tmp/timestamp -type f ! -empty | sed s/\ /?/g` -f /tmp/mytar.tar
tar: Removing leading `/' from member names
Excellent, For this only I was stuck since yesterday. Thanks a lot. But
Code:
# tar -tvf /tmp/mytar.tar 
-r--r--r-- cli/cli      125907 2013-06-13 23:50 data/cli/grub.cfg
-rw-rw-r-- cli/cli          11 2013-06-13 19:02 data/cli/etc/sane.d/dll.d/L i n u x
-rw-r--r-- cli/cli        7580 2013-06-13 19:06 data/cli/etc/hosts
-rw-rw-r-- cli/cli      581484 2013-06-13 18:57 data/cli/etc/vlc/lua/test.exe
-rw-r--r-- cli/cli         319 2013-06-13 18:54 data/cli/etc/pam.d/vsftpd
It is backed up only files not directory(i.e "This is an empty directory")
And also I wanted it to be in tar.bz2 format only. So could you please provide a command for tar.bz2?
If you don't mind could please explain "sed s/\ /?/g". Since I am newbie in sed I did not understand that for what it is.
 
Old 06-13-2013, 03:12 PM   #12
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
David's find piped through sed
Code:
tar -c $(find /data/cli/ \( -newer /tmp/timestamp -type d -empty -print \) -o \( -newer /tmp/timestamp -type f -print \)|sed s/\ /?/g) -f YourTar.tar
Thanks a lot BASH Guru & Firerat, worked great and now it is bakcing up diectories as well.
Code:
# tar -tvf YourTar.tar 
-r--r--r-- cli/cli      125907 2013-06-13 23:50 data/cli/grub.cfg
-rw-rw-r-- cli/cli          11 2013-06-13 19:02 data/cli/etc/sane.d/dll.d/L i n u x
-rw-r--r-- cli/cli        7580 2013-06-13 19:06 data/cli/etc/hosts
-rw-rw-r-- cli/cli      581484 2013-06-13 18:57 data/cli/etc/vlc/lua/test.exe
drwxrwxr-x cli/cli           0 2013-06-13 18:58 data/cli/etc/X11/xinit/This is an empty directory/
-rw-r--r-- cli/cli         319 2013-06-13 18:54 data/cli/etc/pam.d/vsftpd
Could you please provide me the same command for tar.bz2 format.

Last edited by cli; 06-13-2013 at 03:21 PM.
 
Old 06-13-2013, 03:12 PM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by cli View Post
<..>
And also I wanted it to be in tar.bz2 format only. So could you please provide a command for tar.bz2?
If you don't mind could please explain "sed s/\ /?/g". Since I am newbie in sed I did not understand that for what it is.
Sure, my find was exluding empty stuff, as I thought thats what you needed, see above post which uses David's find which is what you want

the sed is quite simple
sed s/\ /?/g
sed is substituting ' ' with '?' , all of them ( group )
sed s/\ /?/
would result in just the first ' ' being substituted, the g at the end processes all the matches

man sed
or more info
https://startpage.com/do/search?cmd=...arch&query=sed

sed ( stream editor ) is a big subject
 
1 members found this post helpful.
Old 06-13-2013, 03:20 PM   #14
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Quote:
Originally Posted by cli View Post
<..>
And also I wanted it to be in tar.bz2 format only. So could you please provide a command for tar.bz2?
If you don't mind could please explain "sed s/\ /?/g". Since I am newbie in sed I did not understand that for what it is.
Sure, my find was exluding empty stuff, as I thought thats what you needed, see above post which uses David's find which is what you want

the sed is quite simple
sed s/\ /?/g
sed is substituting ' ' with '?' , all of them ( group )
sed s/\ /?/
would result in just the first ' ' being substituted, the g at the end processes all the matches

? is a bit like *, but ? is treated as any single character,
which is why it is not perfect
Code:
touch "this is an example"
touch "this_is_an_example"
touch "thismismanmexample"
ls this?is?an?example

man sed
or more info
https://startpage.com/do/search?cmd=...arch&query=sed

sed ( stream editor ) is a big subject
 
1 members found this post helpful.
Old 06-13-2013, 03:30 PM   #15
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Code:
touch "this is an example"
touch "this_is_an_example"
touch "thismismanmexample"
I understood it now.
Code:
# ls -l this?is?an?example
-rw-r--r-- 1 root root 0 Jun 14 00:53 this is an example
-rw-r--r-- 1 root root 0 Jun 14 00:54 this_is_an_example
-rw-r--r-- 1 root root 0 Jun 14 00:54 thismismanmexample
Thanks a lot. And waiting for your kind reply form BASH Guru & Firerat for tar.bz2 format.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: How to locate and delete empty files and directories LXer Syndicated Linux News 0 03-24-2013 01:40 PM
[SOLVED] find with root permissions to search for all files except for specified directories gjwalsh Linux - Server 5 01-08-2012 12:21 PM
using find to only search specific directories isaaclw Linux - Software 5 01-09-2010 09:01 AM
Search tools (Affinity, Tracker Search Tool, etc.) not working - don't find any files Adamantus Linux - Newbie 1 03-30-2009 12:21 AM
how to search for and delete empty directories? BrianK Programming 4 06-03-2005 08:24 AM


All times are GMT -5. The time now is 06:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration