Linux - NewbieThis 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.
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.
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
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
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).
#!/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
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.