How to Get Only Filenames Without Path by Using Grep

How to get only filenames without Path by using grep

Yet another simpler solution:

grep -l whatever-you-want | xargs -L 1 basename

or you can avoid xargs and use a subshell instead, if you are not using an ancient version of the GNU coreutils:

basename -a $(grep -l whatever-you-want)

basename is the bash straightforward solution to get a file name without path. You may also be interested in dirname to get the path only.

GNU Coreutils basename documentation

grep without showing path/file:line

No need to find. If you are just looking for a pattern within a specific directory, this should suffice:

grep -hn FOO /your/path/*.bar

Where -h is the parameter to hide the filename, as from man grep:

-h, --no-filename

Suppress the prefixing of file names on output. This is the default
when there is only one file (or only standard input) to search.

Note that you were using

-H, --with-filename

Print the file name for each match. This is the default when there is
more than one file to search.

How can I use grep to show just filenames on Linux?

The standard option grep -l (that is a lowercase L) could do this.

From the Unix standard:

-l
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of (standard input) will
be written, in the POSIX locale. In other locales, standard input may be
replaced by something more appropriate in those locales.

You also do not need -H in this case.

To show only file name without the entire directory path

ls whateveryouwant | xargs -n 1 basename

Does that work for you?

Otherwise you can (cd /the/directory && ls) (yes, parentheses intended)

ag grep tool - How to print only the log lines without file name/path in the output?

ag --nofilename "test|temp"

How can I grep for a filename instead of the contents of a file?

You need to use find instead of grep in this case.

You can also use find in combination with grep or egrep:

$ find | grep "f[[:alnum:]]\.frm"

Extract filename and extension in Bash

First, get file name without the path:

filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:

filename="${fullfile##*/}"

You may want to check the documentation :

  • On the web at section "3.5.3 Shell Parameter Expansion"
  • In the bash manpage at section called "Parameter Expansion"

Using 'find' to return filenames without extension

To return only filenames without the extension, try:

find . -type f -iname "*.ipynb" -execdir sh -c 'printf "%s\n" "${0%.*}"' {} ';'

or (omitting -type f from now on):

find "$PWD" -iname "*.ipynb" -execdir basename {} .ipynb ';'

or:

find . -iname "*.ipynb" -exec basename {} .ipynb ';'

or:

find . -iname "*.ipynb" | sed "s/.*\///; s/\.ipynb//"

however invoking basename on each file can be inefficient, so @CharlesDuffy suggestion is:

find . -iname '*.ipynb' -exec bash -c 'printf "%s\n" "${@%.*}"' _ {} +

or:

find . -iname '*.ipynb' -execdir basename -s '.sh' {} +

Using + means that we're passing multiple files to each bash instance, so if the whole list fits into a single command line, we call bash only once.


To print full path and filename (without extension) in the same line, try:

find . -iname "*.ipynb" -exec sh -c 'printf "%s\n" "${0%.*}"' {} ';'

or:

find "$PWD" -iname "*.ipynb" -print | grep -o "[^\.]\+"

To print full path and filename on separate lines:

find "$PWD" -iname "*.ipynb" -exec dirname "{}" ';' -exec basename "{}" .ipynb ';'

How to grep only show path/filename?

grep -l will output JUST the names of files which matched, without showing the actual match.



Related Topics



Leave a reply



Submit