I'm looking to get a list of just file names (without the rest of the path) when executing the find command from a terminal. How do I accomplish this on the mac?
Mac – How to return only file names from the find command
findmacterminal
Related Solutions
In its simplest form, find
takes one parameter: the path. In its actually useful form, it takes the path followed by narrowing criteria.
Thus, you want:
find
(the program)/
(the path), and-name abc.dmg
(the criteria).
find / -name abc.dmg
OSX find
has no -printf
action. The +: command not found
error is because your command is enclosed in back ticks (`` ), so the shell is treating the results of the
findcommand as a command and attempting to execute them, specifically it is trying to execute
+` which is the first thing printed by the command you ran. You will get the same error if your run
`echo -n "+"`
Back ticks are used to save the results of a command to a variable, so the above gives an error but this does not:
foo=`echo -n "+"`
You do not say what your desired output is. Based on your question, I assume you want to get a list of all folders in a given directory that start with a .
and print their names on the same line, quoted and preceded by a +
. If so, you can do something like this:
find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;
Sample output:
+'/home/terdon/.mail/fastmail/.bar' +'/home/terdon/.mail/fastmail/.foo'
To pass the output of this command as input to another program (mailbox
for example), do this:
mailbox `find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;`
or
mailbox $(find ~/.mail/fastmail -maxdepth 1 -type d -name ".*" -exec echo -n "+'{}' " \;)
In response to OP's comment:
If you just want all folders, you don't need -name
, to remove the quotes, just don't quote {}
. I will also assume that you don't want the parent folder (fastmail
), hence -mindpeth 1
:
find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d -exec echo -n "+{} " \;
Removing the path is slightly more complex because, contrary to what you might expect, you can't just use basename
in the -exec
call. You need to get creative, here are a few choices:
Parse with
awk
mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d | awk -F"/" '{printf "+%s ",$NF}'`
-F"/"
tellsawk
to use/
as the field delimiter and then print+
followed by the last field ($NF
) which will be the folder name.Use a
for
loop (assuming that your folder names have no strange characters or spaces)mailbox `for dir in $( find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d ); do echo -n "+$(basename $dir) "; done`
If your folder names contain spaces or strange characters, use this instead:
mailbox `find ~/.mail/fastmail -maxdepth 1 -mindepth 1 -type d | while IFS= read -r dir; do echo -n "+$(basename $dir) "; done`
Best Answer
With basename:
find . -type f -exec basename {} \;