This service will be undergoing maintenance at 00:00 UTC, 2017-10-23 It is expected to last about 30 minutes

Bug 153132

Summary: find needs better directory-stripping abilities in -printf
Product: [Fedora] Fedora Reporter: Brian "netdragon" Bober <netdragon>
Component: coreutilsAssignee: Tim Waugh <twaugh>
Status: CLOSED WONTFIX QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 3   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2005-04-18 09:53:48 EDT Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Description Brian "netdragon" Bober 2005-04-01 14:30:03 EST
find has a %f, which is the filename, and %h which is like basename, but it
would be nice if you could specify more control

For instance, for /home/user/foo.txt, I might want to show any of the following:

I might want to specify the number of leading parts to strip, or specify a
string to strip.

I suggest a new formatting flag %m.nb where m specifies the number of leading
parts to strip, and n specifies the number of end parts to strip

I also suggest a new formatting flag %B(regexp, regexp, ...) that says to remove
any occurence of regexp

I also suggest a new formatting flag that prints the pathname relative to
current directory (like pwd), along with %R(regexp, regexp, ...)

%RR and %BB would be like %R and %B respectively except they would only keep the
specified regular expressions

For instance:
file is /home/user/foo.txt
%f is foo.txt
%h is /home/user
%1.b is home/user/foo.txt
%2.b is user/foo.txt
%2.0b is user/foo.txt
%.1b is /home/user
%.2b is /home
%0.2b is /home
%2.1b is user
%B(^/home) is /user/foo.txt
%B(^/home,txt$) is /user/foo
If current directory is /home:
%r is ./user/foo.txt
If current directory is /home/user:
%r is ./foo.txt
If current directory is /home/user/data/documents:
%r is ../../foo.txt
%1.r is ../foo.txt
%.1r is ../..
%R(^../) is ../.txt
%R(../) is foo.txt
%R(/foo) is ../../.txt
%RR(foo.txt) is foo.txt
%RR(..) is ....
%RR(/,..) is ../../
%RR(../) is ../../
Comment 1 Sitsofe Wheeler 2005-04-02 05:53:31 EST
Hmm I wonder whether you are not better off using something like cut to modify a
filename so extensively rather than forcing find to do it all...
Comment 2 Tim Waugh 2005-04-18 09:53:48 EDT
Indeed -- I don't think this is really find's job.  The new -execdir flag
(upstream), on the other hand, might remove the need to do some of the things
you suggest altogether.