LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   search all files and only empty directories using find command (https://www.linuxquestions.org/questions/linux-newbie-8/search-all-files-and-only-empty-directories-using-find-command-4175465815/)

cli 06-13-2013 03:09 AM

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.

yooy 06-13-2013 03:14 AM

have you tried this?
Quote:

find /path/of/dir -empty

cli 06-13-2013 03:38 AM

Quote:

Originally Posted by yooy (Post 4970833)
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.

chrism01 06-13-2013 03:51 AM

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....

cli 06-13-2013 04:29 AM

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


cli 06-13-2013 11:41 AM

Can any body please help for the above?

David the H. 06-13-2013 01:00 PM

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

Firerat 06-13-2013 01:21 PM

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.

cli 06-13-2013 01:27 PM

Quote:

Originally Posted by David the H. (Post 4971160)
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.

Firerat 06-13-2013 01:45 PM

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 :D
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

cli 06-13-2013 02:03 PM

Quote:

Originally Posted by Firerat (Post 4971177)
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.

cli 06-13-2013 02:12 PM

Quote:

Originally Posted by Firerat (Post 4971198)
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.:cool:
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.

Firerat 06-13-2013 02:12 PM

Quote:

Originally Posted by cli (Post 4971206)
<..>
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

Firerat 06-13-2013 02:20 PM

Quote:

Originally Posted by cli (Post 4971206)
<..>
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

cli 06-13-2013 02:30 PM

Quote:

Originally Posted by Firerat (Post 4971216)
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.


All times are GMT -5. The time now is 05:38 PM.