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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow 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.
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}"'
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/'
+
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.
Jul 6, 2013 at 14:36
ls
, and if your ls
version supports the -p
option, you can ls -lip | grep -v '/$'
Jul 6, 2013 at 14:49
+
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 :).
Another find alternative:
find -maxdepth 1 -type f -printf "%i %p\n"
Or yet another:
find -maxdepth 1 -type f -ls
maxdepth 1
to mimic ls
. Otherwise, this will find all files in all subfolders recursively.
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 "/$"
$ 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.