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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
11-17-2008, 03:41 PM
|
#1
|
LQ Newbie
Registered: Nov 2008
Posts: 3
Rep:
|
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.
|
|
|
11-17-2008, 03:58 PM
|
#2
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
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.
|
|
|
11-17-2008, 04:03 PM
|
#3
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
11-17-2008, 04:05 PM
|
#4
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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
|
|
|
11-17-2008, 04:06 PM
|
#5
|
LQ Newbie
Registered: Nov 2008
Posts: 3
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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.
|
|
|
11-17-2008, 04:06 PM
|
#6
|
Senior Member
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Rep:
|
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?
|
|
|
11-17-2008, 04:12 PM
|
#7
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by dexus
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).
|
|
|
11-17-2008, 04:16 PM
|
#8
|
LQ Newbie
Registered: Nov 2008
Posts: 3
Original Poster
Rep:
|
Quote:
Originally Posted by acid_kewpie
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!
|
|
|
11-17-2008, 04:24 PM
|
#9
|
LQ Guru
Registered: Jan 2001
Posts: 24,149
|
Quote:
Originally Posted by irishbitte
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
|
|
|
11-17-2008, 04:29 PM
|
#10
|
Moderator
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417
|
uh huh, oh yeah... get busy...
sorry, it's late (for me)
|
|
|
11-17-2008, 04:44 PM
|
#11
|
Senior Member
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Rep:
|
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!
|
|
|
06-13-2009, 06:25 PM
|
#12
|
LQ Newbie
Registered: Sep 2004
Posts: 2
Rep:
|
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.
|
|
|
06-13-2009, 07:08 PM
|
#13
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
You can expand the path name $file with "${file##*/}" to remove the directory part from a path name.
|
|
|
06-13-2009, 07:22 PM
|
#14
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by ocumo
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.
|
|
|
11-09-2016, 01:42 PM
|
#15
|
LQ Newbie
Registered: Nov 2016
Posts: 4
Rep: 
|
One Liner!
find <path to file> -type f -name '<regexp>' -exec basename {} \;
|
|
|
All times are GMT -5. The time now is 10:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|