1

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.

1

4 Answers 4

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}"' 
0
2

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/' 
4
  • 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. Commented Jul 6, 2013 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 '/$' Commented Jul 6, 2013 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 :). Commented Jul 6, 2013 at 14:49
  • @student I rejected your edit because the -execdir option, while great, isn't portable and I don't want this answer to be specific to GNU find. Note that in my rejection message I incorrectly claimed the + is also not portable, but that is wrong. The + is in POSIX, but -execdir is not. Commented Mar 20 at 10:21
1

Another find alternative:

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

Or yet another:

find -maxdepth 1 -type f -ls 
1
  • You might want to use maxdepth 1 to mimic ls. Otherwise, this will find all files in all subfolders recursively. Commented Jul 6, 2013 at 14:31
1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.