Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have Find print just the filenames, not full paths

Tags:

shell

ksh

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.

like image 777
Steve Avatar asked Feb 08 '12 22:02

Steve


People also ask

How do I return only file names from the find command?

Normally find command will retrieve the filename and its path as one string. If you want to display only the filename, you can use basename command. find infa/bdm/server/source/path -type f -iname "source_fname_*. txt"

How do I get the full path of a filename?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).


2 Answers

find ... -exec basename {} \;  

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

like image 44
evil otto Avatar answered Nov 29 '22 21:11

evil otto


If you're using GNU find, then

find path -printf "%f\n" 

will just print the file name and exclude the path.

like image 165
glenn jackman Avatar answered Nov 29 '22 22:11

glenn jackman