How to find out what is a file and what is a directory

1

The question is as simple as the logic I have here.

Logic:

Assuming that files should always contain your .png .txt suffix etc ...

While folders / directories take nothing but their plain name

The idea is to get these referenced names and set a type icon: if it is a directory, set a folder icon . Now, if it's a file with an extension, set a file icon .

Could someone please kindly show me how I can write a bash shell script in order to detect what's inside a directory: file and folder

    
asked by anonymous 13.03.2018 / 17:20

2 answers

3

Bash has a built-in test that may be what you are looking for.

In a terminal, in any folder, type:

for i in * ; do if [ -d "$i" ] ; then echo "diretório - $i" ; else echo "  arquivo - $i" ; fi ; done

More details, see the "bash" manual.

    
13.03.2018 / 17:32
2

I post here as a record for future reference

Detect files and directories / folders via header:

All:

file *

Folders:

file * | grep -i directory

Files:

file * | grep -i text

Detect only directories / folders using the ls command:

ls -alp1 | grep -i "^d" | awk '{print $7}'

ls -d */

.. and it is done with find tranzing subfolders:

find ./ -type d

For . files:

ls -alp1 | grep -i "^-" | awk '{print $7}'

Now detect symbolic links (shortcut):

ls -alp1 | grep -i "^l" | awk '{print $7}'
    
13.03.2018 / 20:40