Oct 232010
 

zshIn a previous article we saw how to install and configure zsh, today we’ll see in detail the options offered by the zsh for globbing or how the shell use some special characters to auto-complete our commands.

Please note: most command you’ll see here could be accomplished with particular switch of the command, for example ls with -r do a recursive search, but the goal of this article is to just use the shell, so our goal will be to accomplish the same result but just with the power of zsh globbing.



Task 1 show all files with extension .pdf in current directory


ls *.pdf

That’s a common command to show all files with a given extension.

Example of output

┌┌(zsh-laptop)┌(103/pts/8)┌(08:39pm:10/23/10)┌-
└┌(%:~)┌- ls *.pdf

file1.pdf
fil2.pdf

Task 2 show all files with extension .pdf in current directory and all subdirectory


ls **/*.pdf

That’s another common command, with the ** before the / you tell to zsh to expand the command to the current directory and also to all the sub-directories

Example of output

┌┌(zsh-laptop)┌(103/pts/8)┌(08:39pm:10/23/10)┌-
└┌(%:~)┌- ls *.pdf

file1.pdf
fil2.pdf
dir1/file3.pdf
dir1/dir2/file4.pdf
dir3/file5.pdf

Using qualifiers
Glob qualifiers are another nice addition to Zsh: it has the ability to select types of files by using some flags in parentheses at the end of the globbing pattern.
You can use (.) for regular files only, (/) for directories, (*) for executable files, (@) for symlinks, (=) for sockets, (p) for named pipes, (%) for device files, (%b) for block files and (%c) for character files.

In the same way, you can test for file permissions: (r), (w) and (x) for files that are readable, writeable and executable by the owner, (R), (W) and (X) for those with world permissions, and (A), (I) and (E) for group permissions

Task 3 show all regular files with the word “test” in their name in current directory and all subdirectory


ls **/*test*(.)

With the final addiction of the characters (.) we tell to the shell to select only the regular files.

Example of output

┌┌(zsh-laptop)┌(103/pts/8)┌(08:39pm:10/23/10)┌-
└┌(%:~)┌- ls **/*test*(.)
testata
test
dir1/testone
die2/testina

Task 4 find all executable files in current directory and all subdirectory


ls -l **/*(.x)

With the final addiction of the characters (.x) we tell to the shell to select only the file that are executable by the owner.

Example of output

┌┌(zsh-laptop)┌(103/pts/8)┌(08:39pm:10/23/10)┌-
└┌(%:/bin)┌- ls -l **/*(.x)
-rwxr-xr-x 1 root root 801808 2010-08-10 21:58 bash
-rwxr-xr-x 3 root root 26064 2010-09-11 15:48 bunzip2
-rwxr-xr-x 1 root root 1322856 2010-10-04 18:04 busybox
-rwxr-xr-x 3 root root 26064 2010-09-11 15:48 bzcat
-rwxr-xr-x 1 root root 2140 2010-09-11 15:48 bzdiff
.....

Task 5 find all files that don’t have the write permission to group in current directory and all subdirectory


ls **/*(.:g-w:)

In this example we have used the permission in the “chmod style” to use that add : at the start and at the end of the condition you want to test, so in our task this become :g-w:

User and Group
Another useful glob qualifier tests for the user or group that owns a file. To test for your own user or group, just use (U) or (G), respectively. For other users, you have to add the user or group ID to (u) or (g)

Task 6 find all files that have your primary groupy


ls -l **/*(.G)

In this example the use of G select all files that have our primary group as group.

Task 7 find all files of user tomcat


ls -l **/*(u:tomcat:)

usually with the flag u you shoud use the userid, but if you want to use the username instead you have to use the : with the syntax :username:

Modification and access time
Zsh can also pick out files by modification or access time with (m) or (a), respectively. You can search for exact times, or periods before (-) or after (+) your chosen point. This is given in days by default, but can also be measured in months, (M), weeks, (w), hours, (h), minutes, (m) or seconds, (s).

Task 8 find all files you accessed within the last month:


ls **/*(.aM-1)

with this command you’ll see the list of files that you have accessed
in zsh glob: (.aM-1)

File size
File size glob qualifiers work in the same way: (L) refers to the file size, which is measured in bytes by default, but can also be measured in kilobytes (k), megabytes (m), or 512‑byte blocks (p).

Task 9 find all files with size larger than 10 megabytes


ls **/*(.Lm+10)

With this command the shell will show only the files with size larger than 10 megabytes

These glob qualifiers can also be combined at will. The results may be lengthy, but if you know the basic qualifiers you can make sense of them.You can use ^ before a qualifier to negate it or , for or. See the man page for even more glob qualifiers, including how to sort the output in different ways.

Final task: find all files owned by root (u0), world-writable (W), more than 10k in size (Lk+10) and modified during the last hour (m0)


ls **/*(u0WLk+10m0)

Popular Posts:

flattr this!

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>