Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I list all files but not directories in a given directory and show the inode-numbers.

Something like ls -li | grep ^- does not work since the inode number is shown in the beginning of the line.

share|improve this question
    
Possible duplicate: unix.stackexchange.com/questions/61533/… –  slm Jul 6 '13 at 14:35
    
Why the downvote? –  student Jul 12 '13 at 9:45
    
I'm guessing but I think someone thinks your Q is too simple and/or you didn't include any examples/info about what you've tried so far. Generally people like to see that the OP (you) put some level of effort into solving their own Q before asking the community. –  slm Jul 12 '13 at 11:22
add comment

4 Answers 4

There are multiple solutions. Assuming you do not have a filename starting with -

ls -li | grep " -"
ls -li | awk '/ -/'

If a directory contains -, it can be fixed by

ls -li | grep "[0-9][0-9]* -"
ls -li | awk '$2 ~ "-.{9}"'
share|improve this answer
    
This is looking for a space followed by a -. Directories have a space followed by d. –  unxnut Jul 6 '13 at 14:31
    
True, I missed the space. Still, it will also list links, including links to directories (because the link is listed as foo -> bar when using -l), and directories whose name contains ' -'. –  terdon Jul 6 '13 at 14:33
    
It will not list symbolic links because they start with l. Yes, it will list directories that contain ` -`. That can be tweaked by a regular expression though. –  unxnut Jul 6 '13 at 14:35
    
It will list links (I updated my previous comment explaining why), as well as all files whose name contains ` -, not only those whose name starts with -`. –  terdon Jul 6 '13 at 14:37
    
Just fixed my answer to account for that. @terdon: You are a good critic :-) –  unxnut Jul 6 '13 at 14:38
show 3 more comments

You could use find:

find . -maxdepth 1 -type f -exec ls -li '{}' \;

or, to get ls-like output:

find . -maxdepth 1 -type f  -printf '%i %M %n %u %g %kK %Tc %p\n'

Parsing ls is a bad idea since it can often lead to trouble.

If you really want to use ls directly, you could do this:

ls -li | gawk '$2!~/d/'
share|improve this answer
    
You might want to use + instead of \; in your first example (if your version of find supports is) so as not to fork too much on ls. Might also want to add \! -name '.*' so as not to list hidden files. –  gniourf_gniourf Jul 6 '13 at 14:36
1  
Oh, and if you want to parse the output of ls, and if your ls version supports the -p option, you can ls -lip | grep -v '/$' –  gniourf_gniourf Jul 6 '13 at 14:49
    
@gniourf_gniourf since the + is not always available and we are only running ls which is not too hard on the machine I don't think it is worth it. As for hidden files, I consider listing them a feature not a bug :). –  terdon Jul 6 '13 at 14:49
add comment

Another find alternative:

find -maxdepth 1 -type f -printf "%i %p\n"

Or yet another:

find -maxdepth 1 -type f -ls
share|improve this answer
    
You might want to use maxdepth 1 to mimic ls. Otherwise, this will find all files in all subfolders recursively. –  terdon Jul 6 '13 at 14:31
add comment

Here's an alternative way using the commands tree and grep. Grep is used to filter out the directory entries:

$ tree --inodes -f -F|grep -v "/$"

Example

$ tree --inodes -f -F|grep -v "/$"|less
.
|-- [10370679]  ./a
|-- [10359494]  ./a.bash*
|   |-- [10359495]  ./alsa/alsa-info.sh*
|   `-- [10370145]  ./alsa/alsa-info.txt.v8hSmCT2Rf
|   |   |   |-- [11147371]  ./apps/apache-maven-2.0.9/bin/m2*
|   |   |   |-- [11147367]  ./apps/apache-maven-2.0.9/bin/m2.bat
|   |   |   |-- [11147368]  ./apps/apache-maven-2.0.9/bin/m2.conf
|   |   |   |-- [11147372]  ./apps/apache-maven-2.0.9/bin/mvn*
|   |   |   |-- [11147369]  ./apps/apache-maven-2.0.9/bin/mvn.bat
|   |   |   |-- [11147373]  ./apps/apache-maven-2.0.9/bin/mvnDebug*
|   |   |   `-- [11147370]  ./apps/apache-maven-2.0.9/bin/mvnDebug.bat
|   |   |   `-- [11147378]  ./apps/apache-maven-2.0.9/boot/classworlds-1.1.jar
|   |   |   `-- [11147374]  ./apps/apache-maven-2.0.9/conf/settings.xml
|   |   |   `-- [11147376]  ./apps/apache-maven-2.0.9/lib/maven-2.0.9-uber.jar
|   |   |-- [11147363]  ./apps/apache-maven-2.0.9/LICENSE.txt
|   |   |-- [11147364]  ./apps/apache-maven-2.0.9/NOTICE.txt
|   |   `-- [11147365]  ./apps/apache-maven-2.0.9/README.txt

The above incorporates the directory hierarchy into the lines for each file, and also makes use of the -F switch so that tree appends a trailing / to each line that's a directory. Utilizing that feature, we're able to grab any lines that now have this trailing / and omit them.

References

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.