51

I'm using the find command in a ksh script, and I'm trying to retrieve just the filenames, rather than the full path. As in, I want it to return text.exe, not //severname/dir1/dir2/text.exe.

How would I go about getting that? To clarify, I know the directory the files are in, I am just grabbing the ones created before a certain date, so the pathname doesn't matter.

3

6 Answers 6

126

If you're using GNU find, then

find path -printf "%f\n"

will just print the file name and exclude the path.

3
  • Best answer IMHO. Jan 13, 2020 at 9:17
  • 1
    You should use '%f\0'to handle file names with line brakes when the output does not go to terminal. Who uses line brakes in file names anyway?
    – jarno
    Mar 1, 2021 at 9:57
  • The only illegal characters for a file name are the directory separator / and the null byte \0. Mar 1, 2021 at 21:07
26
find ... -exec basename {} \; 

will also do the trick .. but as @Kent asks, why do you want this?

5
  • 1
    Using -exec like in this answer runs an external program for every single file found. @glennjackman's answer using -printf option can be much faster, depending on the number of files and the operating system. Nov 15, 2016 at 12:46
  • 2
    @StéphaneGourichon This is a good answer for non-GNU systems. There is no -printf in OpenBSD find
    – sheepdog
    Sep 21, 2017 at 4:35
  • 1
    Also (but not necessarily applicable to non-GNU), there's a basename -a option that allows passing in multiple args, and a find option that allows passing in multiple args to the -exec, so if performance actually is an issue, this might help: find ... -exec basename -a {} + (note the + instead of \;)
    – michael
    Nov 7, 2017 at 6:10
  • 3
    There are many reasons that someone might want to do this. Don't assume that you know everything.
    – SArcher
    Apr 9, 2018 at 3:44
  • I found this question because I have a directory with hashed filenames /XX/MD5HASH and I am absolutely sure there is no duplicates.
    – wiktor
    Nov 27, 2018 at 13:29
15

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.

10
  • Well, I'm using find because I'm trying to grab all the files created after a certain date, using -mtime. Is there a better way?
    – Steve
    Feb 8, 2012 at 22:37
  • @Steve what are you gonna do with those files you found by 'find'? is there a particular requirement of removing the directory info?
    – Kent
    Feb 8, 2012 at 23:14
  • 1
    yeah I'm going to be copying them to another directory and then doing stuff with them, so all I want is the actual filename. Otherwise, I'd have to run Find again.
    – Steve
    Feb 9, 2012 at 14:27
  • @Steve what is "doing stuff" ? if it is something like archiving to tar, you could find..|xargs to tar then mv to your anotherDIR. anyway, the |sed... in my answer will cut the path info for you.
    – Kent
    Feb 9, 2012 at 14:36
  • 1
    @evil, you are right about the xargs. then find -exec or find..-print | tar ...*.gz --files-from - should work. thx for pointing this out.
    – Kent
    Feb 9, 2012 at 17:19
7

Here's another answer.

find | awk -F/ '{print $NF}'    
1
  • 1
    NF is the number of fields; $NF is the value of the last field. Its like doing $3 if you knew there were 3 fields.
    – danwyand
    Nov 16, 2018 at 17:39
5

GNU find natively supports this using -printf so all that you need to do is

find ... -printf '%f\n'

Update: Oops... this was already covered in the answer by @glenn-jackman which I somehow missed to see.

0

The other answers have it covered but I'll give an example for a more complicated usecase that I didn't find a straightforward answer for.

In case you need both the full path and just the file name inside -exec, then you need to do this:

find ./k8s/charts/secrets/files/ \
    -type f \
    -name "*.yaml" \
    -exec sh -c \
    'vault decrypt "$1" --output "./k8s/charts/secrets/templates/$(basename "$1")"' _ {} \;

Here $1 will get the full path and $(basename "$1") will get just somefile.yaml. You have to do it inside sh -c because bash will expand once before find runs, not per file found.

https://www.shellcheck.net/wiki/SC2014

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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