- 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
Forums
Discussions
Discussions
Discussions
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 06:14 AM
06-21-2010 06:14 AM
Re: List only filenames in a directory
The following statement gives the expected result :
find $DIR -type f | sed -e "s:^$DIR/::"
Actually, I don't seem to need the -prune and the -name options (if I include them I only get to see the directory without the filename).
Regards,
Brecht
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 06:27 AM
06-21-2010 06:27 AM
Re: List only filenames in a directory
The -prune option was in case you had subdirectories.
>if I include them I only get to see the directory without the filename.
I'll do some testing when I get back to make sure it isn't something obvious:
Ah, if you don't have ".", you have to use:
find $DIR ! -path $DIR -prune -o -type f -print | sed -e "s:^$DIR/::"
Or: -name $(basename $DIR)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 06:46 AM
06-21-2010 06:46 AM
Re: List only filenames in a directory
Actually I can have subdirectories, but I don't need to see them.
The statement
find $DIR ! -path $DIR -prune -o -type f | sed -e "s:^$DIR/::"
gives a nice result, but if there are some subdirectories onder the $DIR directory, they are also listed.
How can they be left away ?
For example : under $DIR, I have :
1 file (test.dat) and one not-empty directory (testdir). If I issue the above statement, I get :
test.dat
testdir
However I only want test.dat to be displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 06:57 AM
06-21-2010 06:57 AM
Re: List only filenames in a directory
That -prune should skip those.
>find $DIR ! -path $DIR -prune -o -type f | sed -e "s:^$DIR/::"
>gives a nice result, but if there are some subdirectories under the $DIR directory, they are also listed. How can they be left away?
>I only want test.dat to be displayed.
That's what the "-print" on the end should do.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 11:02 PM
06-21-2010 11:02 PM
Re: List only filenames in a directory
If I add the -print option again, I don't get any results ?
Regards,
Brecht
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2010 11:28 PM
06-21-2010 11:28 PM
Solutionls -aF | grep -v /$ | sed 's|[*/=>@|]$||' >files.txt
Or a variant. (Some tools might combine the grep and sed, for instance...)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 12:24 AM
06-22-2010 12:24 AM
Re: List only filenames in a directory
What is the exact command you are using so I can try to duplicate it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 12:36 AM
06-22-2010 12:36 AM
Re: List only filenames in a directory
find $DIR ! -path $DIR -prune -o -type f -print | sed -e "s:^$DIR::"
Regards,
Brecht
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 01:08 AM
06-22-2010 01:08 AM
Re: List only filenames in a directory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 01:59 AM
06-22-2010 01:59 AM
Re: List only filenames in a directory
Elmar, your statement seems to work fine !
Raj, If I try your statement, I lose some spaces in filenames with more than 1 space.
For example filename 1spacespacespace2.dat
return 1space2.dat.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 02:02 AM
06-22-2010 02:02 AM
Re: List only filenames in a directory
Can you please explain the [*/=>@|]$ part in your sed statement ?
Regards,
Brecht
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 07:06 PM
06-22-2010 07:06 PM
Re: List only filenames in a directory
(You are missing a "/" in sed.)
Ok, I found my problem. "-o" does shortcut evaluation and so the files show up on the left of the OR and never get to the -print. So we either need to swap the two predicates or select only directories on the left:
find $DIR -type f -print -o ! -path $DIR -prune | sed -e "s:^$DIR/::"
find $DIR ! -path $DIR -prune -type d -o -type f -print | sed -e "s:^$DIR/::"
>Can you please explain the [*/=>@|]$ part in your sed statement?
When you use ls -F, you get the files "formatted" by a trailing special char. If "/", directories are excluded.
The sed part just strips off the other special trailing formatting chars as not needed:
* (executable); | (pipe); / (directory, already gone); @ (symlink)
(Not sure about "=" or ">" for HP-UX.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2010 11:13 PM
06-22-2010 11:13 PM