How to Only Get File Name With Linux 'Find'

How to only get file name with Linux 'find'?

In GNU find you can use -printf parameter for that, e.g.:

find /dir1 -type f -printf "%f\n"

Have Find print just the filenames, not full paths

you can do it with:

find ..... |sed 's#.*/##'

however does it really make sense? if there are two files with same filename but located in different directories, how can you distinguish them?

e.g.

you are in /foo

/foo/a.txt
/foo/bar/a.txt

EDIT

edit the answer to gain some better text formatting.

As you described in comment, so you want to

  1. find some files,
  2. copy them to a dir,
  3. gzip them to an archive say a.gz
  4. remove copied files only if step 2 was successful

This could be done in one shot:

find ...|xargs tar -czf /path/to/your/target/a.gz 

this will find files, make a tar (a.gz) to your target dir.

How to list only files and not directories of a directory Bash?

Using find:

find . -maxdepth 1 -type f

Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.

Linux find file names with given string recursively

Use the find command,

find . -type f -name "*John*"

Get only file name from find command and mail

all attachments in single mail:

 find /home/cde -ctime -1 -name "Sum*pdf*" | while read name; do uuencode "$name" "${name##*/}"; done | mailx -s "subject" abc@example.com

to get separate mail:

find /home/cde -ctime -1 -name "Sum*pdf*" | while read name; do uuencode "$name" "${name##*/}"| mailx -s "subject" abc@example.com ; done

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)

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 ';'

Find all files with name containing string

Use find:

find . -maxdepth 1 -name "*string*" -print

It will find all files in the current directory (delete maxdepth 1 if you want it recursive) containing "string" and will print it on the screen.

If you want to avoid file containing ':', you can type:

find . -maxdepth 1 -name "*string*" ! -name "*:*" -print

If you want to use grep (but I think it's not necessary as far as you don't want to check file content) you can use:

ls | grep touch

But, I repeat, find is a better and cleaner solution for your task.

get only filename (exclude pathname) and record count in linux

Try sed!

find . -type f -name "*.xml" -exec wc -l {} \; | sed 's/\.\/.*\///g'

my output had . in the beginning but if yours does not than use

find . -type f -name "*.xml" -exec wc -l {} \; | sed 's/\/.*\///g'

EDIT: The sed basically replaces any occurrences of ./some/path/ with empty string, or if using the second example it replaces occurrences of /some/path/.

EDIT2: Here is a third example that works regardless if the output has a . or not in the front of the path:

find / -type f -name "*.xml" -exec wc -l {} \; | sed 's/\.*\/.*\///g' 


Related Topics



Leave a reply



Submit