I'm using find to all files in directory, so I get a list of paths. However, I need only file names. i.e. I get ./dir1/dir2/file.txt and I want to get file.txt

share|improve this question
up vote 208 down vote accepted

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

find /dir1 -type f -printf "%f\n"
share|improve this answer
4  
Clearly the answer, but it lacks detail. – Jason McCreary Sep 20 '13 at 15:31
15  
this is for GNU only – Osama F Jan 9 '14 at 9:35
    
This doesn't work for me when I use multiple file types (-o switch) – Urchin Feb 13 '14 at 23:56
4  
find: -printf: unknown primary or operator – holms May 15 '14 at 14:05
    
@Urchin No reason it shouldn't so long as you have correct logic (i.e. -o has lower precedence than implied -a, so you will often want to group your -o arguments) – BroSlow Jul 29 '14 at 5:00

If your find doesn't have a -printf option you can also use basename:

find ./dir1 -type f -exec basename {} \;
share|improve this answer
12  
Quoting the semicolon is another way to disambiguate: ... {} ';' – davidchambers Dec 27 '12 at 22:13
7  
works on osx, finally ! – holms May 15 '14 at 14:06
    
this is more portable & also works under busybox – Stuart Cardall Jan 3 '17 at 22:44

If you are using GNU find

find . -type f -printf "%f\n"

Or you can use a programming language such as Ruby(1.9+)

$ ruby -e 'Dir["**/*"].each{|x| puts File.basename(x)}'

If you fancy a bash (at least 4) solution

shopt -s globstar
for file in **; do echo ${file##*/}; done
share|improve this answer
    
I was inspired by your answer to help sleske : serverfault.com/a/745968/329412 – Nolwennig Aug 11 '16 at 7:26

Use -execdir which automatically holds the current file in {}, for example:

find . -type f -execdir echo '{}' ';'

You can also use $PWD instead of . (on some systems it won't produce extra dot in the front).

-execdir utility [argument ...] ;

The -execdir primary is identical to the -exec primary with the exception that utility will be executed from the directory that holds the current file.

When used + instead of ;, then {} is replaced with as many pathnames as possible for each invocation of utility. In other words, it'll print all filenames in one line.

share|improve this answer
    
I'm getting ./filename instead of filename. Depending on your needs, it may or may not be fine. – user276648 Jan 5 '17 at 5:38
    
@user276648 Try with $PWD instead of .. – kenorb Jan 5 '17 at 12:17

If you want to run some action against the filename only, using basename can be tough.

For example this:

find ~/clang+llvm-3.3/bin/ -type f -exec echo basename {} \; 

will just echo basename /my/found/path. Not what we want if we want to execute on the filename.

But you can then xargs the output. for example to kill the files in a dir based on names in another dir:

cd dirIwantToRMin;
find ~/clang+llvm-3.3/bin/ -type f -exec basename {} \; | xargs rm
share|improve this answer
    
Just what I was looking for! Thanks! – JS. Jul 27 '16 at 21:41

I've found a solution (on makandracards page), that gives just the newest file name:

ls -1tr * | tail -1

(thanks goes to Arne Hartherz)

I used it for cp:

cp $(ls -1tr * | tail -1) /tmp/
share|improve this answer
1  
This doesn't answer the question at all. – kenorb Oct 23 '15 at 10:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.