search all files and only empty directories using find command
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.
# 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.
oh sorry, forgot
Code:
tar --help
Code:
Compression options:
-a, --auto-compress use archive suffix to determine the compression
program
-I, --use-compress-program=PROG
filter through PROG (must accept -d)
-j, --bzip2 filter the archive through bzip2
-J, --xz filter the archive through xz
--lzip filter the archive through lzip
--lzma filter the archive through xz
--lzop
--no-auto-compress do not use archive suffix to determine the
compression program
-z, --gzip, --gunzip, --ungzip filter the archive through gzip
-Z, --compress, --uncompress filter the archive through compress
so
Code:
tar jc . -f Archive.tar.bz2
tar Jc . -f Archive.tar.xz
...
I really dislike the idea of using tools like sed for processing filenames in bulk. It's a sloppy and unsafe practice.
If you want to use the output of find in another program, it's better to load the filenames into an array first. This will allow you to safely work with both the list as a whole and the individual entries in it.
Loading the array should be done with null separators for maximum safety (-print0 instead of -print).
unset array #make sure the array starts empty, if necessary
while IFS='' read -r -d '' fname; do
array+=( "$fname" )
done < <( find . -type f -print0 ) #simplified find example
Using them afterwards generally just requires using the "@" index expansion, which produces the entire list as a series of separate items. Be sure to quote it! That will ensure that any spaces and reserved characters are escaped.
It's also possible to modify the contents of every entry with any of the standard parameter substitution forms at the same time, although I don't think it's needed in this case.
Code:
tar -cpjvf "${array[@]}"
The main possible drawback with arrays is that there's a possibility for the expanded command to be longer than your system's ARG_MAX, the maximum length a command can be. In which case you'd have to break it up into multiple commands with a loop or xargs.
The pattern on the left hand side is considered a regular expression, so characters like * and ? are special there. But the right hand side treats everything as simple text, except for a few special backslash-escape patterns. So the above command will simply replace spaces with question marks.
However, you should always* enclose your grep/sed/awk/whatever expressions in single quotes, otherwise they are subject to parsing by the shell before execution.
Code:
sed 's/ /?/g'
*Unless you know what you are doing and have a special need for it, such as including variables in the expression.
However, you should always* enclose your grep/sed/awk/whatever expressions in single quotes, otherwise they are subject to parsing by the shell before execution.
Code:
sed 's/ /?/g'
*Unless you know what you are doing and have a special need for it, such as including variables in the expression.
Very true, I get lazy with the short stuff sometimes
'' can be used if you want to include variables
example
Code:
Foo=Foo
Bar=Bar
echo $Foo | sed 's/$Foo/$Bar/'
echo $Foo | sed 's/'$Foo'/'$Bar'/'
Except that this leaves the variables completely unprotected, so word-splitting and glob expansion will still be done on their contents.
The usual rule of thumb is to simply enclose the longest string possible with the necessary quotes. And remember that you can concatenate multiple quoted strings together too. Strings that don't have shell reserved characters in them don't have to be quoted at all.
So any of these would be better:
Code:
echo "$Foo" | sed "s/$Foo/$Bar/"
echo "$Foo" | sed 's/'"$Foo"'/'"$Bar"'/'
echo "$Foo" | sed s/"$Foo"/"$Bar"/
The first one is the cleanest and easiest, obviously.
Actually, though, once a string is stored in a variable, you can usually just use parameter substitution, which is much faster.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.