- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: List only filenames in a directory
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:25 AM
тАО06-21-2010 04:25 AM
I want to list only the files in a directory (so no subdirectories and just the filenames). The result should be in a new file called files.txt.
So far I have used the following statement :
ls -l | grep ^- | awk '{print $9}' > files.txt
In certain directories I have filenames like :
1 3.doc
The statement above only gives the following result :
1
I have also tried the following :
ls -l | grep ^- | awk '{for (i=9; i<=NF; i++) {printf("%s ", $i)} printf("\n")}' > files.txt
But now the result is :
1 3.doc
Is there a way to become the full filename with all the spaces in it(so 1 3.doc) ?
Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:29 AM
тАО06-21-2010 04:29 AM
Re: List only filenames in a directory
What about:
# find . -type f -exec ls -l {} \; | cut -c 59-
Cheers,
Raj.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:35 AM
тАО06-21-2010 04:35 AM
Re: List only filenames in a directory
If I use the statement you posted, I also see the directory-structure, so :
/mnt/appserv/ontwikkel/1 3.doc
I only need the filename.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:39 AM
тАО06-21-2010 04:39 AM
Re: List only filenames in a directory
Isn't that what you want? (If you are complaining about the trailing space, you must explicitly mention it, since we can't see them.)
>Is there a way to become the full filename with all the spaces in it (so 1 3.doc)?
Well, you can smarten up your awk script:
ll | awk '
/^-/ {
for (i=9; i < NF; ++i)
printf("%s ", $i)
printf("%s\n", $NF)
}' > files.txt
Or you can use find(1) with -prune:
find . ! -name . -prune -o -type -f -print
The names will have leading "./".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:56 AM
тАО06-21-2010 04:56 AM
Re: List only filenames in a directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 04:57 AM
тАО06-21-2010 04:57 AM
Re: List only filenames in a directory
The result with the awk command returns the filename with only 1 space.
The statement :
"find . ! -name . -prune -o -type -f -print"
returns an error
I also needs something to cut the directories away, so I only get the filename as a result.
Regards,
Brecht
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 05:10 AM
тАО06-21-2010 05:10 AM
Re: List only filenames in a directory
Right, you need to mention it or attach a file.
>The result with the awk command returns the filename with only 1 space.
You could smarten up awk by using index to find your $9 string and then use substr but $9 may not be unique.
>"find . ! -name . -prune -o -type -f -print" returns an error
Sorry, not -f:
find . ! -name . -prune -o -type f -print
>I also need something to cut the directories away
That's the easy part, for ".": :-)
find . ! -name . -prune -o -type f -print |
sed -e 's:^\./::'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 05:12 AM
тАО06-21-2010 05:12 AM
Re: List only filenames in a directory
Just wait until you have a file with more than the default field width for the filesize.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 05:31 AM
тАО06-21-2010 05:31 AM
Re: List only filenames in a directory
find . ! -name . -prune -o -type f -print |
sed -e 's:^\./::' is indeed a better statement.
I had to exclude the -print option. Otherwise it returned nothing ?
Also, when I replace the .(local dir) with the directory to search in, I don't get the filename but the directory itself, so :
find /mnt/appserv/ontwikkel ! -name . -prune -o -type f -print | sed -e 's:^\./::'
gives as a result :
/mnt/appserv/ontwikkel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-21-2010 05:42 AM
тАО06-21-2010 05:42 AM
Re: List only filenames in a directory
(Sorry, I can't get to my HP-UX system until tomorrow. From what I remember, you need that -print right there.
Which part returned nothing, the find or the whole pipeline?)
>when I replace the .(local dir) with the directory to search in, I don't get the filename but the directory itself, so:
find /mnt/appserv/ontwikkel ! -name . -prune -o -type f -print | sed -e 's:^\./::'
The monkey (cherchez le singe!) principle indicates you change all of the "." with /mnt/appserv/ontwikkel:
DIR=/mnt/appserv/ontwikkel
find $DIR ! -name $DIR -prune -o -type f -print | sed -e "s:^$DIR/::"
Or cd to that directory then use "find .".
- Tags:
- cherchez le singe