LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-01-2015, 05:49 AM   #1
arshpreet
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
Need help with find command


I am making a script that will show the list of JPG files and saving
that list with xargs for further processing. When I am using echo to
get the file names it adds "./" in the start of every filename.

find . -type f -iname '*.JPG' -print0 |
xargs -0 -n 1 -P 1 -I {} \
echo The file is \'{}\'!

It is showing following output:

The file is './DSC_7555.JPG'!

How to remove "./" in the start of every file name?
 
Old 02-01-2015, 06:41 AM   #2
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Quote:
Originally Posted by arshpreet View Post
How to remove "./" in the start of every file name?
Code:
find . -type f -iname '*.JPG' -print0 |
xargs -0 -n 1 -P 1 -I {} \
echo The file is \'{}\'!
Something like this should work:
Code:
find . -type f -iname '*.JPG' -print0 | sed 's/\.\///' |
xargs -0 -n 1 -P 1 -I {} \
echo The file is \'{}\'!

Last edited by manu-tm; 02-01-2015 at 07:05 AM.
 
Old 02-01-2015, 09:44 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,716

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
Instead of "-print0" use
Code:
find . -type f -iname '*.JPG' -printf '%P\0'
The "%P" directive in printf prints the path without the command line argument under which it was found.
 
1 members found this post helpful.
Old 02-01-2015, 01:58 PM   #4
arshpreet
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Wink printf '%P\0' worked

Code:
find . -type f -iname '*.JPG' -printf '%P\0'
Thanks it worked!
 
Old 02-01-2015, 06:03 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599Reputation: 3599
Quote:
Originally Posted by arshpreet View Post
How to remove "./" in the start of every file name?
Note that (unless you've got an exeeding path length or only need bare file names or process files in your search path or $PWD directly) while using a relative path may seem convenient it does force you to "anchor" to the same path again before using the output.


Quote:
Originally Posted by arshpreet View Post
I am making a script that will show the list of JPG files and saving that list with xargs for further processing.
That's overkill because 'find' can print whatever you want all by itself and echoing the file name doesn't make sense (unless you're debugging or using stdout) and even then 'xargs' can also print what it's processing with "-t". So please use them tools like they should be used:
Code:
# Full path, stdout, not escaped:
find $PWD -type f -iname "*.jpg" -printf "%p\n"
# Full path, 0-terminated and verbose xargs cp:
find $PWD -type f -iname "*.jpg" -print0|xargs -0 -t -iX cp 'X' /tmp/
# Full path, 0-terminated and printed to file:
find $PWD -type f -iname "*.jpg" -fprint0 /some/file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with find command and rar/unrar command line krakatoa1 *BSD 6 09-11-2012 03:21 PM
[SOLVED] Find a file in directories without using find command sikanders Linux - Newbie 14 08-06-2010 08:47 PM
Find/grep/wc command to find matching files, print filename and word count dbasch Linux - Newbie 10 09-14-2009 05:55 PM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 04:45 PM
can't find my find command! how to replace? dave247 Debian 4 11-19-2008 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration