Howto get only filename from find command in Linux?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Howto get only filename from find command in Linux?
# 1  
Old 04-11-2011
Howto get only filename from find command in Linux?

Hi every body!

I would like to get only filename in the result of find command in Linux but I don't know howto. Tks so much for your helps.
# 2  
Old 04-11-2011
The basename utility can convert long paths into the filename.

Code:
FILE="`basename /path/to/file`"
echo $FILE

Depending on your system or shell you may be able to convert it with shell builtins, which would be more efficient. What are they?
# 3  
Old 04-11-2011
Quote:
Originally Posted by Corona688
The basename utility can convert long paths into the filename.

Code:
FILE="`basename /path/to/file`"
echo $FILE

Depending on your system or shell you may be able to convert it with shell builtins, which would be more efficient. What are they?
I write script from bash shell on Fedora14.
# 4  
Old 04-11-2011
I have a function (plagiarized) which separates the directory, the filename, and the extension.
Code:
	function dir_file_ext ()
{
	for FILE in $(find . -name "*.*"); do
		BASENAME="${FILE%.[^.]*}"
		DIRNAME="${BASENAME%/[^/]*}"
		FILENAME="${BASENAME:${#DIRNAME} + 1}"
		EXT="${FILE##*\.}"
		echo "${DIRNAME}/${FILENAME}.${EXT}"
	done
	}

And the last echo statement can put them back together again!

It works even if there are multiple dots/periods '.' in the filename.

Enjoy!
# 5  
Old 04-12-2011
ok tks so much. I got it!
# 6  
Old 04-12-2011
Hi nguyendu0102,

You can use printf option within find command too, as follow:
Code:
find . -type f -ls -printf "filename --> %f\n"

Or without printing paths given by "ls" output :
Code:
find . -type f -printf "filename --> %f\n"

Or simply:
Code:
find . -type f -printf "%f\n"

* find command as shown above will print only files ("-type f") in current folder and within its subfolders. If you only want
to print files within current folder (not subfolders) you must add "- maxdepth 1" option after dot.
Code:
find . -maxdepth 1 -type f -printf "%f\n"


Hope it helps.

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. UNIX for Beginners Questions & Answers

Find command with Metacharacter (*) Should match exact filename

Hi, Below is list of files in my directory. -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:58 12345_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12346_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12347_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59... (2 Replies)
Discussion started by: Balraj
2 Replies

3. AIX

Howto find the data and management IP address

Can anyone help me to find the data and management IP address on a IBM unix server without looking at the /etc/hosts file. sometimes the hosts file may not make it obvious between the data & mgmt ip addresses. thanks (5 Replies)
Discussion started by: amerjit.jhalley
5 Replies

4. Shell Programming and Scripting

Print filename/dir name while executing aclput using find command

Running below command , but unable to print the filename , is there way to print filename/dirname using -print option find . -type f -exec aclput -i fileacl.template {} \; (5 Replies)
Discussion started by: lalitpct
5 Replies

5. Shell Programming and Scripting

Find command error having space in filename

Hi, I am working in solaris.I am using below script to copy the files from /usr/tmp to /usr/gm But while running this it is not considering the files list after the filename having space in them. Example:- compile_custom_pll.sh conv_data_sqlload.sh conv_sqlload.sh Copy of... (5 Replies)
Discussion started by: millan
5 Replies

6. Shell Programming and Scripting

Only filename from find command

Hi All When we use find command command in Unix we get the result as /home/user/folder/filename1 /home/user/folder/filename2 /home/user/folder/filename3 Is it possible that i only get the file name The expected output when using find command is filename1 filename2 filename3 I am... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

7. Shell Programming and Scripting

how to get only filename in a recursively find command

Hi i would like to ask on how to accomplish the FF: I want to execute a find command recursively and only get the filename something like i want only the last field set if is used ever the fieldvset as an redirection from the output of the find command For example: dir1/dir2/filename1... (2 Replies)
Discussion started by: jao_madn
2 Replies

8. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

9. Windows & DOS: Issues & Discussions

windows howto find

Hi all! How would you do the equivalent of find ! -type f..... under windows? Meaning, how under a directory could you find all files except those named *.mp3 for example? the command under linux would be: find $DIR ! -name "*.mp3" what would be that under windows? Thanx for any ideas... (3 Replies)
Discussion started by: penguin-friend
3 Replies

10. UNIX for Dummies Questions & Answers

search for hardlinks based on filename via find command

I am using command substitution into a find command in a script where I have built a menu to do a bunch of tasks within my unix account. When I choose the options for to find a file/files that have the same inode of the entered filename, ie hardlinks, nothing shows up. When I choose the appropiate... (2 Replies)
Discussion started by: hunternjb
2 Replies
Login or Register to Ask a Question