LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-17-2008, 03:41 PM   #1
dexus
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
List file names in a directory WITHOUT the path [SOLVED]


Hey there, I'm trying to list all files inside a certain directory, but without the file paths.

For example,

Let's say we're in the following directory:

/media/folder

Inside /media/folder are the following Files:

File-Inc-0001
File-Inc-0002
...
File-Inc-0100

I'm trying to make a script that requires these files names in the command line. So I'm doing something like this:

Code:
#!/bin/bash

cd /media/nitrogen/

for file in `find /media/nitrogen *-Inc-* -type f`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
---

The problem with this is that $file is resolving to '/media/nitrogen/File-Inc-0001'. I need it to resolve to 'File-Inc-0001'. I can't have the path in the command line for it to work successfully.

Any ideas how to list files in a directory WITHOUT the path?

--

Solution from acid_kewpie:

Code:
for file in *-Inc-*; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done

Last edited by dexus; 11-17-2008 at 04:26 PM.
 
Old 11-17-2008, 03:58 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well you can just use basename to strip off a path, but it'd probably be technically better to not use find, and just do

Code:
for file in *-Inc-*; do
  blah
done
then there's no path in the first place.
 
Old 11-17-2008, 04:03 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just don't use find. Why not simply...?
Code:
for file in `ls /media/nitrogen/*-Inc-*`
do
  bscan -V "$file" -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
Edit: acid_kewpie beat me...

Last edited by colucix; 11-17-2008 at 04:05 PM.
 
Old 11-17-2008, 04:05 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Two things:

1) UNIX/Linux have a "basename" command that can be used any filepath name to get only the file name at the end.
e.g. typing "basename /usr/bin/ls" would result in just "ls".
Also you can pipe output through basename
e.g. "echo /usr/bin/ls |basename" would result in just "ls".

2) In your find you're specifying the full path to the files but instead you could use the short cut dot (".") which means "this directory.
So instead what you had you could have the following:
#!/bin/bash

cd /media/nitrogen/

for file in `find . *-Inc-* -type f`; do
bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
 
Old 11-17-2008, 04:06 PM   #5
dexus
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Just don't use find. Why not simply...?
Code:
for file in `ls /media/nitrogen/*-Inc-*`
do
  bscan -V "$file" -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
because $file is resolving to: /media/nitrogen/File-Inc-0006

I already tried that.
 
Old 11-17-2008, 04:06 PM   #6
irishbitte
Senior Member
 
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Blog Entries: 1

Rep: Reputation: 88
Change:

Code:
#!/bin/bash

cd /media/nitrogen/

for file in `find /media/nitrogen *-Inc-* -type f`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
to this:

Code:
#!/bin/bash

cd /media/nitrogen/

for file in `ls /media/nitrogen *-Inc-* -type f`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
I don't do alot of scripting, but I think that would work?
 
Old 11-17-2008, 04:12 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by dexus View Post
because $file is resolving to: /media/nitrogen/File-Inc-0006

I already tried that.
Yeah, correct. You can try as acid_kewpie suggested. If you previously change directory to /media/nitrogen, let the shell expand *-Inc-* with the name of the files inside the current working directory (ls command not really necessary).
 
Old 11-17-2008, 04:16 PM   #8
dexus
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by acid_kewpie View Post
well you can just use basename to strip off a path, but it'd probably be technically better to not use find, and just do

Code:
for file in *-Inc-*; do
  blah
done
then there's no path in the first place.
This worked perfectly.

Thanks!
 
Old 11-17-2008, 04:24 PM   #9
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by irishbitte View Post
Code:
#!/bin/bash

cd /media/nitrogen/

for file in `ls /media/nitrogen *-Inc-* -type f`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
This would have been almost the same as what acid_kewpie stated first except you don't have to specify the full path with your ls command since you already cd'd into the directory.

Should have looked like this:
Code:
#!/bin/bash

cd /media/nitrogen/

for file in `ls *-Inc-*`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
 
Old 11-17-2008, 04:29 PM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
uh huh, oh yeah... get busy...

sorry, it's late (for me)
 
Old 11-17-2008, 04:44 PM   #11
irishbitte
Senior Member
 
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Blog Entries: 1

Rep: Reputation: 88
Mr. Kewpie, quick on the draw there! trickykid, thanks for pointing that out. I'm not a scripter really, but did a bit in college, so kinda have an idea. Good going tho acid_kewpie!
 
Old 06-13-2009, 06:25 PM   #12
ocumo
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
find can easily return filenames without the file paths.

It is perfectly possible to output the filename without the leading path with the find command. There is no need to use basename or other hacks.

With find, all you need to do is to use the directives: -printf '%f \n'

The %f returns the File's name with any leading directories removed (only the last element).
The \n provides a new line character after every file name. Without the \n, all the filenames are output in the same line.

Note that when the output goes to a terminal, you should quote the directive "%f\n". If you don't quote, bash will "eat it up" and the result will not be correct.

Like this:

Code:
#!/bin/bash

cd /media/nitrogen/

for file in `find /media/nitrogen *-Inc-* -type f -printf "%f \n"`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
Check find's man pages for details.

Regards,
Ocumo

Last edited by ocumo; 06-13-2009 at 06:29 PM. Reason: Minor correction to the title of the post.
 
Old 06-13-2009, 07:08 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can expand the path name $file with "${file##*/}" to remove the directory part from a path name.
 
Old 06-13-2009, 07:22 PM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by ocumo View Post
Code:
for file in `find /media/nitrogen *-Inc-* -type f -printf "%f \n"`; do
  bscan -V $file -s -m -c /etc/bacula/bacula-sd.conf nitrogen
done
still, its better to use while read loop, instead of for loop (or change IFS) to take care of spaces in file names.
 
Old 11-09-2016, 01:42 PM   #15
jmedinar
LQ Newbie
 
Registered: Nov 2016
Posts: 4

Rep: Reputation: Disabled
Cool One Liner!

find <path to file> -type f -name '<regexp>' -exec basename {} \;
 
  


Reply



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
C++ List Files In Directory bendeco13 Programming 8 11-02-2010 12:08 PM
Script to list files in a given directory Azzath Programming 8 04-03-2008 07:02 AM
how to list how many files in a directory? malaka56 Linux - Software 8 09-02-2005 05:37 AM
How to list all the files in a directory Linh Programming 2 05-11-2004 10:09 AM
list only files in current directory xscousr Programming 8 09-22-2003 07:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:34 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