LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,009
Blog Entries: 5

Rep: Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785Reputation: 785
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: 82
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
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,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
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: 82
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
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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.
 
  


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
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


All times are GMT -5. The time now is 07:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration